Skip to content

StringCollection - #684

Draft
CTMacUser wants to merge 9 commits into
apple:mainfrom
CTMacUser:block-of-strings
Draft

StringCollection#684
CTMacUser wants to merge 9 commits into
apple:mainfrom
CTMacUser:block-of-strings

Conversation

@CTMacUser

Copy link
Copy Markdown
Contributor

Since String objects 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 how String objects 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. String objects 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 StringCollection stores elements with varying lengths, it cannot support RandomAccessCollection nor MutableCollection. Like String, StringCollection only supports up to BidirectionalCollection and RangeReplaceableCollection.

StringCollection uses 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.

StringCollection follows many other system operations (Comparable, CustomStringConvertible, Decodable, Encodeable, Equatable, ExpressibleByArrayLiteral, and Hashable).

Checklist

  • I've read the Contribution Guidelines
  • My contributions are licensed under the Swift license.
  • I've followed the coding style of the rest of the project.
  • I've added tests covering all new code paths my change adds to the project (if appropriate).
  • I've added benchmarks covering new functionality (if appropriate).
  • I've verified that my change does not break any existing tests or introduce unexplained benchmark regressions.
  • I've updated the documentation if necessary.

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.
@CTMacUser
CTMacUser requested a review from lorentey as a code owner July 15, 2026 01:38
@MahdiBM

MahdiBM commented Jul 19, 2026

Copy link
Copy Markdown

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
lorentey marked this pull request as draft July 23, 2026 23:55
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.

2 participants