kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Optimize read-only collections

Open dovchinnikov opened this issue 3 months ago • 1 comments

In Toolbox we parse HUGE json. Once parsed, it allocates too much memory. Right now, the default ArrayList is used, it allocates 10 elements, but most of the time it has zero or one element only. We have ~100k such arrays visible in the memory dump.

Suggestion

For read-only collections:

  • use empty singletons for empty collections (e.g., EmptyList)
  • use singleton implementations (e.g., SingletonMap) for single-element collections.
  • trim collections with >1 elements.

dovchinnikov avatar Aug 26 '25 17:08 dovchinnikov

The challenge here is that collections are/can be parsed incrementally, and any trimming would need to be done only when no more elements can be added – at the point the parent object is created. There are some cases where it may be straightforward to have these trimmed collections, and it may be worthwhile doing so.

Overall I would say this is a good opportunity to look again at how collection serialization works and how it can support custom collection serializers (without checking they are an instance of the internal AbstractCollectionSerializer.

The workaround would be to have a custom serializer for the container that does this trimming.

pdvrieze avatar Aug 27 '25 09:08 pdvrieze