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

dialvia(http): change read buffer size to hold response line #620

Merged
merged 1 commit into from
Jan 2, 2024

Conversation

mmatczuk
Copy link
Contributor

http.ReadResponse() calls bufio.Reader.ReadLine() to read lines. When line fits we can reuse memory and prevent additional allocation and GC.

The status line should fit below 64 bytes:
HTTP/1.1 (8+1 bytes)
Status-Code (3+1 bytes)
Reason-Phrase (<=31 bytes)
CRLF (2 bytes)
Total bytes: 8 + 1 + 3 + 1 + 31 = 44

With 128 bytes we should be able to read most header lines too.

http.ReadResponse() calls bufio.Reader.ReadLine() to read lines.
When line fits we can reuse memory and prevent additional allocation and GC.

The status line should fit below 64 bytes:
HTTP/1.1 (8+1 bytes)
Status-Code (3+1 bytes)
Reason-Phrase (<=31 bytes)
CRLF (2 bytes)
Total bytes: 8 + 1 + 3 + 1 + 31 = 44

With 128 bytes we can handle most header lines.

As you can see we have 7% and 14% less allocs for 200 and 403 responses respectively.

benchmark                                              old ns/op     new ns/op     delta
BenchmarkHTTPProxyDialer_DialContextR/status_200-8     29666         29483         -0.62%
BenchmarkHTTPProxyDialer_DialContextR/status_403-8     83034         82855         -0.22%

benchmark                                              old allocs     new allocs     delta
BenchmarkHTTPProxyDialer_DialContextR/status_200-8     42             39             -7.14%
BenchmarkHTTPProxyDialer_DialContextR/status_403-8     62             52             -16.13%

benchmark                                              old bytes     new bytes     delta
BenchmarkHTTPProxyDialer_DialContextR/status_200-8     3975          4023          +1.21%
BenchmarkHTTPProxyDialer_DialContextR/status_403-8     5287          5079          -3.93%
Copy link
Contributor

@Choraden Choraden left a comment

Choose a reason for hiding this comment

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

Thanks!

@mmatczuk mmatczuk merged commit 64d587f into main Jan 2, 2024
4 checks passed
@mmatczuk mmatczuk deleted the mmt/dialvia_read_buffer_size branch January 2, 2024 09:18
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

2 participants