Skip to content

performance: fast path for primitive list encoder - #354

Open
ClementBeal wants to merge 2 commits into
schultek:mainfrom
ClementBeal:performance/faster-primitive-list-encoder
Open

ClementBeal wants to merge 2 commits into
schultek:mainfrom
ClementBeal:performance/faster-primitive-list-encoder

Conversation

@ClementBeal

Copy link
Copy Markdown

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.options is 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.

@ClementBeal
ClementBeal marked this pull request as draft September 8, 2026 01:28
@ClementBeal
ClementBeal marked this pull request as ready for review September 8, 2026 12:16
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.

1 participant