Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the size of the bufio.Reader for message header parsing. #1187

Merged
merged 1 commit into from
Jan 11, 2023
Merged

Reduce the size of the bufio.Reader for message header parsing. #1187

merged 1 commit into from
Jan 11, 2023

Conversation

thomas-long-f3
Copy link
Contributor

When profiling a service that consumes messages from NATS Jetstream, we observed a lot of allocations from the Go client. The culprit is this line, by default bufio.NewReader() allocates a 4 KiB buffer. This PR reduces the buffer to 128, this seems to be a good middle-ground based on benchmarks below.

64

name                         old time/op    new time/op    delta
HeaderDecode/Small_-_25-8      1.44µs ±17%    0.31µs ±15%  -78.66%  (p=0.000 n=23+23)
HeaderDecode/Medium_-_141-8    2.46µs ±29%    0.95µs ±13%  -61.57%  (p=0.000 n=24+24)
HeaderDecode/Large_-_368-8     2.88µs ±22%    1.42µs ± 6%  -50.59%  (p=0.000 n=25+25)

name                         old alloc/op   new alloc/op   delta
HeaderDecode/Small_-_25-8      4.58kB ± 0%    0.55kB ± 0%  -87.96%  (p=0.000 n=25+25)
HeaderDecode/Medium_-_141-8    4.94kB ± 0%    0.91kB ± 0%  -81.55%  (p=0.000 n=25+25)
HeaderDecode/Large_-_368-8     5.49kB ± 0%    1.84kB ± 0%  -66.47%  (p=0.000 n=25+25)

name                         old allocs/op  new allocs/op  delta
HeaderDecode/Small_-_25-8        7.00 ± 0%      7.00 ± 0%     ~     (all equal)
HeaderDecode/Medium_-_141-8      21.0 ± 0%      21.0 ± 0%     ~     (all equal)
HeaderDecode/Large_-_368-8       27.0 ± 0%      31.0 ± 0%  +14.81%  (p=0.000 n=25+25)

128

name                         old time/op    new time/op    delta
HeaderDecode/Small_-_25-8      1.44µs ±17%    0.29µs ± 3%  -79.96%  (p=0.000 n=23+24)
HeaderDecode/Medium_-_141-8    2.46µs ±29%    0.90µs ± 1%  -63.29%  (p=0.000 n=24+20)
HeaderDecode/Large_-_368-8     2.88µs ±22%    1.20µs ± 8%  -58.47%  (p=0.000 n=25+23)

name                         old alloc/op   new alloc/op   delta
HeaderDecode/Small_-_25-8      4.58kB ± 0%    0.62kB ± 0%  -86.56%  (p=0.000 n=25+25)
HeaderDecode/Medium_-_141-8    4.94kB ± 0%    0.98kB ± 0%  -80.26%  (p=0.000 n=25+25)
HeaderDecode/Large_-_368-8     5.49kB ± 0%    1.52kB ± 0%  -72.30%  (p=0.000 n=25+25)

name                         old allocs/op  new allocs/op  delta
HeaderDecode/Small_-_25-8        7.00 ± 0%      7.00 ± 0%     ~     (all equal)
HeaderDecode/Medium_-_141-8      21.0 ± 0%      21.0 ± 0%     ~     (all equal)
HeaderDecode/Large_-_368-8       27.0 ± 0%      27.0 ± 0%     ~     (all equal)

256

name                         old time/op    new time/op    delta
HeaderDecode/Small_-_25-8      1.44µs ±17%    0.52µs ±17%  -63.63%  (p=0.000 n=23+21)
HeaderDecode/Medium_-_141-8    2.46µs ±29%    1.03µs ±19%  -58.22%  (p=0.000 n=24+23)
HeaderDecode/Large_-_368-8     2.88µs ±22%    1.27µs ±23%  -55.77%  (p=0.000 n=25+22)

name                         old alloc/op   new alloc/op   delta
HeaderDecode/Small_-_25-8      4.58kB ± 0%    0.74kB ± 0%  -83.77%  (p=0.000 n=25+25)
HeaderDecode/Medium_-_141-8    4.94kB ± 0%    1.10kB ± 0%  -77.67%  (p=0.000 n=25+25)
HeaderDecode/Large_-_368-8     5.49kB ± 0%    1.65kB ± 0%  -69.97%  (p=0.000 n=25+25)

name                         old allocs/op  new allocs/op  delta
HeaderDecode/Small_-_25-8        7.00 ± 0%      7.00 ± 0%     ~     (all equal)
HeaderDecode/Medium_-_141-8      21.0 ± 0%      21.0 ± 0%     ~     (all equal)
HeaderDecode/Large_-_368-8       27.0 ± 0%      27.0 ± 0%     ~     (all equal)

@coveralls
Copy link

coveralls commented Jan 11, 2023

Coverage Status

Coverage: 85.493% (+0.002%) from 85.492% when pulling 7f3b22e on thomas-long-f3:decode-header-allocations into eada5ec on nats-io:main.

nats_test.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@piotrpio piotrpio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@piotrpio piotrpio merged commit bbde3f3 into nats-io:main Jan 11, 2023
@thomas-long-f3 thomas-long-f3 deleted the decode-header-allocations branch January 11, 2023 12:41
@piotrpio piotrpio mentioned this pull request Jan 18, 2023
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.

None yet

3 participants