StringCollection - #684
Draft
CTMacUser wants to merge 9 commits into
Draft
Conversation
Introduce StringCollection, which puts the elements' code-point data into a single memory buffer, instead of a separate allocation per String. The collection supports bi-directional traversal and range-replacing mutations. Add corresponding tests.
The availability checks were removed from the actual tested code in the previous patch, but I didn't want to overload that diff.
Define a "<" operator, as a Unicode-aware locale-agnostic lexicographic comparison. Define the matching ">", "<=", and ">=" operators. Add corresponding tests.
Description support is like [String], a bracketed comma-separated list of the terms. Each term uses their escaped form instead of being directly written.
Implement Encodable & Decodable for StringCollection using un-keyed containers. Add tests using JSON transit.
|
I think it's worth linking to the Swift forums thread: https://forums.swift.org/t/a-memory-saving-type-for-collections-of-strings |
lorentey
marked this pull request as draft
July 23, 2026 23:55
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.
Since
Stringobjects use dynamic memory, any collection of them that also uses dynamic memory will use up several patches of dynamic allocation. I wondered if we can reduce that memory allocation to one buffer. This is funnily similar to howStringobjects could be made up of UTF-8 bundles of data.UTF-8 lay outs its data such that we can naturally find borders between code points.
Stringobjects can't have that, so I had to use marker values to indicate where a string's embedded data starts and ends, bloating the total size of the data stored.Since a
StringCollectionstores elements with varying lengths, it cannot supportRandomAccessCollectionnorMutableCollection. LikeString,StringCollectiononly supports up toBidirectionalCollectionandRangeReplaceableCollection.StringCollectionuses a copy-on-write policy on its mutating operations. If a collection the only user of a string data block, the block will be manipulated directly, otherwise a computed clone is made.StringCollectionfollows many other system operations (Comparable,CustomStringConvertible,Decodable,Encodeable,Equatable,ExpressibleByArrayLiteral, andHashable).Checklist