Skip to content

Commit

Permalink
Improving memory performance when it comes to snappy
Browse files Browse the repository at this point in the history
Moving snappy to lazy read from the original payload instead
decompressing it in memory
  • Loading branch information
MovieStoreGuy committed Sep 16, 2024
1 parent 3b50b38 commit d842e6c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
25 changes: 25 additions & 0 deletions .chloggen/msg_fix-snappy-lazy-read.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: confighttp

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Snappy compression to lazy read for memory efficiency

# One or more tracking issues or pull requests related to the change
issues: [11177]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
13 changes: 3 additions & 10 deletions config/confighttp/compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,10 @@ var availableDecoders = map[string]func(body io.ReadCloser) (io.ReadCloser, erro
}
return zr, nil
},
//nolint:unparam // ignoring lint since method needs to fit typedef
"snappy": func(body io.ReadCloser) (io.ReadCloser, error) {
sr := snappy.NewReader(body)
sb := new(bytes.Buffer)
_, err := io.Copy(sb, sr)
if err != nil {
return nil, err
}
if err = body.Close(); err != nil {
return nil, err
}
return io.NopCloser(sb), nil
// Lazy Reading content to improve memory efficiency
return io.NopCloser(snappy.NewReader(body)), nil
},
}

Expand Down
2 changes: 1 addition & 1 deletion config/confighttp/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func TestHTTPContentDecompressionHandler(t *testing.T) {
encoding: "snappy",
reqBody: bytes.NewBuffer(testBody),
respCode: http.StatusBadRequest,
respBody: "snappy: corrupt input\n",
respBody: "snappy: corrupt input",
},
{
name: "UnsupportedCompression",
Expand Down

0 comments on commit d842e6c

Please sign in to comment.