message: optimize lookupAndFormat function for better performance

This commit optimizes the lookupAndFormat function to reduce code redundancy
and improve performance. Benchmarks show a significant performance increase
across various Sprintf and Fprint functions.

benchstat old.txt new.txt
goos: darwin
goarch: arm64
pkg: golang.org/x/text/message
                         │   old.txt   │               new.txt               │
                         │   sec/op    │   sec/op     vs base                │
SprintfPadding-12          333.0n ± 1%   216.0n ± 1%  -35.15% (p=0.000 n=10)
SprintfEmpty-12            307.3n ± 1%   170.9n ± 1%  -44.38% (p=0.000 n=10)
SprintfString-12           313.5n ± 1%   178.9n ± 2%  -42.91% (p=0.000 n=10)
SprintfTruncateString-12   315.6n ± 1%   182.3n ± 2%  -42.23% (p=0.000 n=10)
SprintfQuoteString-12      320.1n ± 0%   198.6n ± 1%  -37.96% (p=0.000 n=10)
SprintfInt-12              325.0n ± 1%   203.1n ± 1%  -37.51% (p=0.000 n=10)
SprintfIntInt-12           340.2n ± 1%   233.2n ± 2%  -31.47% (p=0.000 n=10)
SprintfPrefixedInt-12      335.2n ± 1%   226.2n ± 1%  -32.54% (p=0.000 n=10)
SprintfFloat-12            330.1n ± 1%   209.7n ± 1%  -36.48% (p=0.000 n=10)
SprintfComplex-12          365.1n ± 1%   276.0n ± 2%  -24.41% (p=0.000 n=10)
SprintfBoolean-12          311.8n ± 0%   178.6n ± 3%  -42.72% (p=0.000 n=10)
SprintfHexString-12        328.1n ± 1%   204.8n ± 2%  -37.56% (p=0.000 n=10)
SprintfHexBytes-12         334.0n ± 2%   216.4n ± 1%  -35.20% (p=0.000 n=10)
SprintfBytes-12            340.8n ± 7%   213.9n ± 1%  -37.25% (p=0.000 n=10)
SprintfStringer-12         662.5n ± 3%   453.2n ± 1%  -31.59% (p=0.000 n=10)
SprintfStructure-12        373.8n ± 6%   275.7n ± 1%  -26.23% (p=0.000 n=10)
ManyArgs-12                485.1n ± 4%   425.4n ± 1%  -12.30% (p=0.000 n=10)
FprintInt-12               265.8n ± 2%   262.0n ± 1%   -1.41% (p=0.011 n=10)
FprintfBytes-12            348.4n ± 1%   258.8n ± 1%  -25.70% (p=0.000 n=10)
FprintIntNoAlloc-12        262.0n ± 2%   261.2n ± 2%        ~ (p=0.565 n=10)
geomean                    342.4n        233.7n       -31.75%

Change-Id: Id9999469f3fd0ca0290a5cb81f42fff81277b451
GitHub-Last-Rev: 904d624b132ac1a49cdd908e9b95ff2b16a5f7a0
GitHub-Pull-Request: golang/text#51
Reviewed-on: https://go-review.googlesource.com/c/text/+/584095
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
1 file changed
tree: 9a821def52f9599a46817845fc557765431cd114
  1. cases/
  2. cmd/
  3. collate/
  4. currency/
  5. date/
  6. encoding/
  7. feature/
  8. internal/
  9. language/
  10. message/
  11. number/
  12. runes/
  13. search/
  14. secure/
  15. transform/
  16. unicode/
  17. width/
  18. .gitattributes
  19. .gitignore
  20. codereview.cfg
  21. CONTRIBUTING.md
  22. doc.go
  23. gen.go
  24. go.mod
  25. go.sum
  26. LICENSE
  27. PATENTS
  28. README.md
README.md

Go Text

Go Reference

This repository holds supplementary Go libraries for text processing, many involving Unicode.

CLDR Versioning

It is important that the Unicode version used in x/text matches the one used by your Go compiler. The x/text repository supports multiple versions of Unicode and will match the version of Unicode to that of the Go compiler. At the moment this is supported for Go compilers from version 1.7.

Download/Install

The easiest way to install is to run go get -u golang.org/x/text. You can also manually git clone the repository to $GOPATH/src/golang.org/x/text.

Contribute

To submit changes to this repository, see http://golang.org/doc/contribute.html.

To generate the tables in this repository (except for the encoding tables), run go generate from this directory. By default tables are generated for the Unicode version in core and the CLDR version defined in golang.org/x/text/unicode/cldr.

Running go generate will as a side effect create a DATA subdirectory in this directory, which holds all files that are used as a source for generating the tables. This directory will also serve as a cache.

Testing

Run

go test ./...

from this directory to run all tests. Add the “-tags icu” flag to also run ICU conformance tests (if available). This requires that you have the correct ICU version installed on your system.

TODO:

  • updating unversioned source files.

Generating Tables

To generate the tables in this repository (except for the encoding tables), run go generate from this directory. By default tables are generated for the Unicode version in core and the CLDR version defined in golang.org/x/text/unicode/cldr.

Running go generate will as a side effect create a DATA subdirectory in this directory which holds all files that are used as a source for generating the tables. This directory will also serve as a cache.

Versions

To update a Unicode version run

UNICODE_VERSION=x.x.x go generate

where x.x.x must correspond to a directory in https://www.unicode.org/Public/. If this version is newer than the version in core it will also update the relevant packages there. The idna package in x/net will always be updated.

To update a CLDR version run

CLDR_VERSION=version go generate

where version must correspond to a directory in https://www.unicode.org/Public/cldr/.

Note that the code gets adapted over time to changes in the data and that backwards compatibility is not maintained. So updating to a different version may not work.

The files in DATA/{iana|icu|w3|whatwg} are currently not versioned.

Report Issues / Send Patches

This repository uses Gerrit for code changes. To learn how to submit changes to this repository, see https://golang.org/doc/contribute.html.

The main issue tracker for the image repository is located at https://github.com/golang/go/issues. Prefix your issue with “x/text:” in the subject line, so it is easy to find.