Repository navigation
performance: fast path for primitive list encoder - #354
Open
ClementBeal wants to merge 2 commits into
Open
ClementBeal wants to merge 2 commits into
ClementBeal wants to merge 2 commits into
Conversation
ClementBeal
marked this pull request as draft
September 8, 2026 01:28
ClementBeal
marked this pull request as ready for review
September 8, 2026 12:16
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.
The iterable encoder is doing that:
value .map((v) => context.$enc<T>(v as T, 'item', context.options)) .toList();It means that we call the encoder for each item of the iterable.
The problem is that if the iterable is made of primitive types like int, bool, String, etc., the primitive encoder will just return the same value. So for something like a
List<int>, we still go through the whole encoding logic for every item only to create a new copy of the list with the exact same values.In this PR, I added a special parameter in the mapper to know if the encoder is an identity encoder or not. If it is, and
context.optionsis null, it means we can bypass the.map()step and get a significant performance improvement.On my machine (Mac M4), it was around 20x faster on VM and 10x faster on AOT.
I know that #159 was mentioned to change the architecture and improve performance more globally. I think this could still be an interesting performance improvement in the meantime, especially because the primitive case is very common.