This repository has been archived by the owner on Jan 27, 2022. It is now read-only.
Cap internal buffer size for large array performance #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I was seeing some performance issues with marshalling large arrays where just a few thousands of complex objects were marshalled for 20+ seconds.
Digging deeper, it seems to be due to internal buffer management and step-wise growing of the buffer for each operation that extends the buffer beyond initial 128 bytes.
rmarsh/generator.go
Lines 538 to 544 in 69f255a
Doing
copy
for each operation on smaller buffer sizes has negligible performance impact, however, as the buffer size grows, the impact becomes significant. In my large-array situation, given that all array elements are being buffered untilEndArray
is called, this problem goes from bad to worse as array size grows.This PR flushes the buffer at 512 to limit its growth and reduce the performance penalty.
Performance before the changes:
Performance after the changes (almost 1900 times faster):