Skip to content

Use CompositeDecoder::decodeCollectionSize to pre-allocate collections#3192

Open
fzhinkin wants to merge 10 commits into
devfrom
fzhinkin/collections-initial-capacity
Open

Use CompositeDecoder::decodeCollectionSize to pre-allocate collections#3192
fzhinkin wants to merge 10 commits into
devfrom
fzhinkin/collections-initial-capacity

Conversation

@fzhinkin
Copy link
Copy Markdown
Contributor

@fzhinkin fzhinkin commented May 4, 2026

Reimplemented AbstractCollectionSerializer::merge to read expected collection size first and pass it as an initial capacity to a corresponding builder. Builder were updated to take the capacity into account.

The change is quite intrusive, but for formats providing information about the collection size before reading it (it's only CBOR for now, though) it reduces the footprint and slightly improve decoding performance.

Fixes #3153

Base automatically changed from fzhinkin/cbor-collections-size to dev May 5, 2026 12:00
@fzhinkin fzhinkin force-pushed the fzhinkin/collections-initial-capacity branch from 1a41b4d to c042815 Compare May 5, 2026 15:59
buffer[position++] = c
}

override fun build() = buffer.copyOf(position)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

To discuss: we can avoid a copy here if position == buffer.size, but we need to explicitly invalidate the builder, JIC.

@fzhinkin fzhinkin marked this pull request as ready for review May 6, 2026 13:42
@fzhinkin fzhinkin requested a review from Copilot May 6, 2026 13:42

This comment was marked as resolved.

val compositeDecoder = decoder.beginStructure(descriptor)
val expectedSize = compositeDecoder.decodeCollectionSize(descriptor)
val builder = if (previous != null) {
previous.toBuilder().also { it.checkCapacity(expectedSize) }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should add the new new "expectedSize" to the current size when calling checkCapacity. The common case for this branch is for incremental parsing of list elements.

It also makes sense to pass the new needed size to toBuilder to avoid an extra copy in the case that a new builder is actually created (not default behaviour).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Nice catch, thanks for pointing to it!

Not only the code does not work as expected, it can also fail in runtime if checkCapacity can't handle negative expectedSize (which is the case for all non-empty implementations, I guess).

@fzhinkin fzhinkin marked this pull request as draft May 6, 2026 21:11
@fzhinkin fzhinkin marked this pull request as ready for review May 12, 2026 19:45
@fzhinkin
Copy link
Copy Markdown
Contributor Author

Updated benchmarking results:

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.

Pre-allocate Map and Collection capacities using CBOR definite-length headers

3 participants