Skip to content

perf(trace): optimize traceSingleQuotedString#2466

Open
analytically wants to merge 1 commit intojackc:masterfrom
analytically:perf/traceSingleQuotedString
Open

perf(trace): optimize traceSingleQuotedString#2466
analytically wants to merge 1 commit intojackc:masterfrom
analytically:perf/traceSingleQuotedString

Conversation

@analytically
Copy link
Contributor

Replace fmt.Fprintf with direct hex encoding using lookup table. Add Grow() pre-allocation to eliminate builder reallocations.

Benchmarks show 1.7-8x speedup depending on input:

  • all non-printable: 743ns -> 92ns (8x)
  • mixed content: 422ns -> 108ns (4x)
  • printable only: 242ns -> 144ns (1.7x)

Allocations reduced from 2-6 to 1 in all cases.

Replace fmt.Fprintf with direct hex encoding using lookup table.
Add Grow() pre-allocation to eliminate builder reallocations.

Benchmarks show 1.7-8x speedup depending on input:
- all non-printable: 743ns -> 92ns (8x)
- mixed content:     422ns -> 108ns (4x)
- printable only:    242ns -> 144ns (1.7x)

Allocations reduced from 2-6 to 1 in all cases.

Signed-off-by: Mathias Bogaert <mathias.bogaert@gmail.com>
@jackc
Copy link
Owner

jackc commented Jan 4, 2026

Take a look at #2457. I'm still reluctant to merge optimizations of paths that shouldn't be triggered very often, but if it is to be done then I think that #2457 is a superset of this.

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.

2 participants