Skip to content

Conversation

bnoordhuis
Copy link
Contributor

@bnoordhuis bnoordhuis commented Sep 30, 2025

Instead of copying long substring slices, store a reference to and offset into the parent string.

Gets more profitable as strings get longer. For 64k string slices, it's about 6.5x faster than copying.

The downside of ref-counted string slices is that they can keep the parent string alive for longer than it otherwise would have been, leading to memory usage that is higher than without string slices. That's why this optimization is only applied to long-ish slices, currently slices > 1,024 bytes.

Possible future enhancements are slicing only when the substring is > x% of the parent string, or copying lazily when the slice string is the only thing referencing the parent string.


edit: forgot to mention, the next step is adding cons/rope strings

Instead of copying long substring slices, store a reference to and
offset into the parent string.

Gets more profitable as strings get longer. For 64k string slices,
it's about 6.5x faster than copying.

The downside of ref-counted string slices is that they can keep the
parent string alive for longer than it otherwise would have been,
leading to memory usage that is higher than without string slices.
That's why this optimization is only applied to long-ish slices,
currently slices > 1,024 bytes.

Possible future enhancements are slicing only when the substring
is > x% of the parent string, or copying lazily when the slice
string is the only thing referencing the parent string.
Copy link
Contributor

@saghul saghul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a small suggestion.

@bnoordhuis bnoordhuis merged commit 08db51a into quickjs-ng:master Sep 30, 2025
126 checks passed
@bnoordhuis bnoordhuis deleted the string-slices branch September 30, 2025 16:11
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