Skip to content

Commit

Permalink
fmt: add ascii fast path for decoding verbs
Browse files Browse the repository at this point in the history
name                    old time/op  new time/op  delta
SprintfSlowParsingPath   108ns ± 4%   103ns ± 4%  -4.53%  (p=0.000 n=18+18)

Change-Id: I174463f303d1857e8d5b8a6283c025b3546e7b39
Reviewed-on: https://go-review.googlesource.com/44450
Run-TryBot: Martin Möhrmann <moehrmann@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
  • Loading branch information
martisch committed Aug 14, 2017
1 parent 6661cf6 commit 629b5e7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/fmt/fmt_test.go
Expand Up @@ -1201,6 +1201,14 @@ func BenchmarkSprintfTruncateString(b *testing.B) {
})
}

func BenchmarkSprintfSlowParsingPath(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Sprintf("%.v", nil)
}
})
}

func BenchmarkSprintfQuoteString(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Expand Down
7 changes: 5 additions & 2 deletions src/fmt/print.go
Expand Up @@ -1067,8 +1067,11 @@ formatLoop:
break
}

verb, w := utf8.DecodeRuneInString(format[i:])
i += w
verb, size := rune(format[i]), 1
if verb >= utf8.RuneSelf {
verb, size = utf8.DecodeRuneInString(format[i:])
}
i += size

switch {
case verb == '%': // Percent does not absorb operands and ignores f.wid and f.prec.
Expand Down

0 comments on commit 629b5e7

Please sign in to comment.