Skip to content

Conversation

@Flgado
Copy link
Member

@Flgado Flgado commented Jul 16, 2025

Summary

This change optimizes the Zstd decompression logic by switching from io.ReadAll(zstd.NewReader(bytes.NewReader(data))) to using zstd.Decoder.DecodeAll with a pre-allocated buffer.

Motivation

The old approach creates a new bytes.Reader and reads all decompressed data into a freshly allocated buffer, causing many heap allocations and increased GC pressure.

Impact

  • Benchmark results show:
    • ~29% reduction in memory allocated per operation.
    • Consistent or slightly improved decompression speed.
    • No increase in allocation count (GC overhead unchanged).

This significantly improves memory efficiency, especially important for large result sets.

Benchmarks Example:

  • Old version 10M rows
BenchmarkSpoolingProtocolSpooledSegmentlJsonZstdDecoderQuery-8   	       1	53275675497 ns/op	21708494160 B/op	300044245 allocs/op
  • new version 10M row
cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
BenchmarkSpoolingProtocolSpooledSegmentlJsonZstdDecoderQuery-8   	       1	48507167074 ns/op	15496255624 B/op	300038202 allocs/op

The major impact was on io.Read allocation.

@cla-bot cla-bot bot added the cla-signed label Jul 16, 2025
@Flgado Flgado requested a review from nineinchnick July 16, 2025 20:29
@nineinchnick
Copy link
Member

@Flgado can you rebase? I think we can merge this

@Flgado
Copy link
Member Author

Flgado commented Nov 27, 2025

Sure @nineinchnick ! lets just first try to merge the #144 :)

@nineinchnick
Copy link
Member

Rebase and we can merge it now

@Flgado Flgado force-pushed the improve_memory_usage_json_zstd branch from f4f445a to da21e65 Compare November 29, 2025 18:23
@Flgado
Copy link
Member Author

Flgado commented Nov 29, 2025

@nineinchnick done :)

@nineinchnick nineinchnick added the enhancement New feature or request label Nov 30, 2025
@nineinchnick nineinchnick merged commit 7850c9b into trinodb:master Nov 30, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed enhancement New feature or request

Development

Successfully merging this pull request may close these issues.

2 participants