Allow swapping collection implementation to persistent collections#455
Open
andrewparmet wants to merge 6 commits intoopen-toast:mainfrom
Open
Allow swapping collection implementation to persistent collections#455andrewparmet wants to merge 6 commits intoopen-toast:mainfrom
andrewparmet wants to merge 6 commits intoopen-toast:mainfrom
Conversation
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
andrewparmet
commented
Feb 13, 2026
| defaultProtoc() | ||
|
|
||
| configure<JavaApplication> { | ||
| mainClass.set("com.toasttab.protokt.benchmarks.ProtobufBenchmarksKt") |
Collaborator
Author
There was a problem hiding this comment.
Benchmarks were broken.
Comment on lines
54
to
57
| map.isEmpty() -> emptyMap() | ||
| map is UnmodifiableMap -> map | ||
| usePersistentCollections && PersistentCollections.isFrozenMap(map) -> map | ||
| else -> UnmodifiableMap(LinkedHashMap(map)) |
Collaborator
Author
There was a problem hiding this comment.
Lazy comparison allows compileOnly dependency on the immutable collections runtime here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds opt-in persistent collections backed by
kotlinx-collections-immutable. When enabled (via system propertyprotokt.collections.persistent=trueon JVM, or env varPROTOKT_COLLECTIONS_PERSISTENT=trueon JVM/JS), deserialized repeated and map fields use PersistentList/PersistentMap instead of UnmodifiableList/UnmodifiableMap.The key benefit is structural sharing: the
+operator in builder DSLcopy {}blocks runs in O(log n) instead of O(n), making incremental message mutation dramatically faster on pre-populated collections.This is achieved through a BuilderScope interface with member extension plus operators that shadow stdlib's plus within generated builder classes. When the receiver is a persistent collection, PersistentList.add() / PersistentMap.put() are used for O(log n) structural sharing; otherwise, the stdlib copy behavior is preserved.
Benchmarks:
Copy-Append (1000 iterations of
msg.copy { field = field + element }, ms/op)Lists:
Maps:
Serialize/Deserialize (ms/op):
Deserialization costs ~5-7% more with persistent collections due to PersistentList.builder() overhead. Serialization is marginally slower for large messages. "Small" copy-append shows no improvement because the initial collection is emptyList()/emptyMap(), not a persistent type.