This implements a roughly 40-45% improvement for distance measurements over `BigString`’s character and Unicode Scalar views.
Interestingly, we don’t see an improvement for UTF-16 views — I’m guessing this is due to String’s breadcrumbing. (And it will go away with the upcoming UTF8Span-ification of this type.)
The benchmarks I added only show an improvement for multi-chunk big string instances, but this is merely an artifact of the specific algorithm that is being measured — for single-chunk strings, the existing measurement method is already optimal, so the new heuristic never triggers for this particular payload.
BigString sometimes miscounted distances in its character view due to alignment issues. Resolve this by overhauling distance calculations in the character metric:
- Introduce a `prefixSize` function in `_StringMetric`, distinct from `distance`.
- In the metric-agnostic index distance algorithm, use `prefixCount` to avoid double-counting characters that cross (or end on) chunk boundaries.
- Simplify `_StringMetric.distance` by requiring that `start <= end`.
- Crucially, change character-index operations to start by rounding each input index down to the nearest character break.
When ingesting a `String` instance, `BigString` assumes that the input string has a reasonably efficient UTF-8 view.
Unfortunately, that is very much not the case when the input happens to be backed by a bridged NSString object — it appears that in this case, the ingester loop invokes some operation(s) with linear complexity in the size of the entire input, rendering the ingester’s overall complexity quadratic.
The BigString ingester is only expected to operate within a single chunk at the time. It’s unclear precisely which operation triggers the quadratic behavior; ideally we should figure it out and resolve it with a more targeted fix.
In the meantime, a blunt stopgap fix is to force-transcode the input string to UTF-8 at the time the ingester is initialized. This unnecessarily wastes some (temporary) memory on holding the transcoded string, but it avoids the quadratic cliff.
Rope.join has a logic error where it fails to properly ensure uniqueness of child node references before zipping the two b-trees together.
rdar://128450202
We are forced to do this due to an Xcode issue (#364) with underscored module names.
This module remains unstable, despite the removal of the underscore. Directly importing it from outside the package remains unsupported. This module and/or its entire contents may go away or change in incompatible ways in any future swift-collections release.
Rope remains woefully undocumented, and I suspect its abstraction
level isn’t right for general use, but let’s see if writing up some
documentation for it will make it a bit more accessible.