Skip to content

Optimize Format for common verbs without flags#219

Open
cuiweixie wants to merge 2 commits into
holiman:masterfrom
cuiweixie:optimize-format
Open

Optimize Format for common verbs without flags#219
cuiweixie wants to merge 2 commits into
holiman:masterfrom
cuiweixie:optimize-format

Conversation

@cuiweixie

Copy link
Copy Markdown
Contributor

Summary

  • Fast-path (*Int).Format for common verbs (%d, %v, %s, %x) when no width, precision, or format flags are set, using Dec() / Hex() instead of allocating via ToBig().
  • Fall back to big.Int.Format for all other cases (flags, width, precision, other verbs) to preserve full fmt compatibility, including nil "<nil>" behavior.
  • Add BenchmarkFormat and expand TestFormat to compare against big.Int across verbs and flags.

Benchmark (Apple M4, benchstat n=6)

name                 old time/op    new time/op    delta
Format/%d/small-10     162ns ± 3%      66ns ± 8%  -59.36%
Format/%d/large-10     267ns ± 1%     212ns ± 4%  -20.42%
Format/%x/small-10     123ns ± 4%      59ns ± 1%  -52.21%
Format/%x/large-10     138ns ± 5%      78ns ± 0%  -43.63%
Format/%v/large-10     273ns ± 4%     214ns ± 0%  -21.60%
Format/%#x/large-10    148ns ± 3%     158ns ± 1%   +6.51%  (fallback)
Format/%10d/large-10   279ns ± 1%     281ns ± 1%   +0.75%  (fallback)
geomean                188ns          129ns       -31.16%

name                 old allocs/op  new allocs/op  delta
Format/%d/small-10     6.00 ± 0%      2.00 ± 0%  -66.67%
Format/%d/large-10     7.00 ± 0%      2.00 ± 0%  -71.43%
Format/%x/small-10     6.00 ± 0%      2.00 ± 0%  -66.67%
Format/%x/large-10     6.00 ± 0%      2.00 ± 0%  -66.67%
Format/%v/large-10     7.00 ± 0%      2.00 ± 0%  -71.43%
geomean                6.55           2.86       -56.34%

Test plan

  • go test -run=TestFormat
  • go test -bench=BenchmarkFormat -benchmem -count=6 (before/after via benchstat)
  • CI passes on the PR

Avoid allocating a big.Int via ToBig() for the common %d/%v/%s/%x
paths when no width, precision, or format flags are set. Fall back to
big.Int.Format for all other cases to preserve full fmt compatibility.

Benchmark (Apple M4, benchstat n=6):

  name                 old time/op    new time/op    delta
  Format/%d/small-10     162ns ± 3%      66ns ± 8%  -59.36%
  Format/%d/large-10     267ns ± 1%     212ns ± 4%  -20.42%
  Format/%x/small-10     123ns ± 4%      59ns ± 1%  -52.21%
  Format/%x/large-10     138ns ± 5%      78ns ± 0%  -43.63%
  Format/%v/large-10     273ns ± 4%     214ns ± 0%  -21.60%
  Format/%#x/large-10    148ns ± 3%     158ns ± 1%   +6.51%  (fallback)
  Format/%10d/large-10   279ns ± 1%     281ns ± 1%   +0.75%  (fallback)
  geomean                188ns          129ns       -31.16%

  name                 old allocs/op  new allocs/op  delta
  Format/%d/small-10     6.00 ± 0%      2.00 ± 0%  -66.67%
  Format/%d/large-10     7.00 ± 0%      2.00 ± 0%  -71.43%
  Format/%x/small-10     6.00 ± 0%      2.00 ± 0%  -66.67%
  Format/%x/large-10     6.00 ± 0%      2.00 ± 0%  -66.67%
  Format/%v/large-10     7.00 ± 0%      2.00 ± 0%  -71.43%
  geomean                6.55           2.86       -56.34%
Explicitly discard the return values of s.Write and fmt.Fprintf to
satisfy the errcheck linter.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant