Skip to content

Commit 59e1219

Browse files
aimuzgopherbot
authored andcommitted
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: 904d624 GitHub-Pull-Request: #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 parent a20a3e2 commit 59e1219

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

message/message.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,21 +138,20 @@ func (p *Printer) Printf(key Reference, a ...interface{}) (n int, err error) {
138138

139139
func lookupAndFormat(p *printer, r Reference, a []interface{}) {
140140
p.fmt.Reset(a)
141-
var id, msg string
142141
switch v := r.(type) {
143142
case string:
144-
id, msg = v, v
143+
if p.catContext.Execute(v) == catalog.ErrNotFound {
144+
p.Render(v)
145+
return
146+
}
145147
case key:
146-
id, msg = v.id, v.fallback
147-
default:
148-
panic("key argument is not a Reference")
149-
}
150-
151-
if p.catContext.Execute(id) == catalog.ErrNotFound {
152-
if p.catContext.Execute(msg) == catalog.ErrNotFound {
153-
p.Render(msg)
148+
if p.catContext.Execute(v.id) == catalog.ErrNotFound &&
149+
p.catContext.Execute(v.fallback) == catalog.ErrNotFound {
150+
p.Render(v.fallback)
154151
return
155152
}
153+
default:
154+
panic("key argument is not a Reference")
156155
}
157156
}
158157

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy