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
bnoordhuis added a commit to bnoordhuis/quickjs that referenced this pull request Oct 10, 2025
This change seems to have introduced a seemingly impossible bug in a
real-world code base where values flip to JS_NULL for no discernible
reason.

My best guess is something manipulating string data directly, without
being aware of slice strings, but it's impossible to track down where.
The weird thing is that the value flipping to null isn't even a string,
it appears to be an array.

This reverts commit 08db51a.
This reverts commit 62b4eed.

Fixes: quickjs-ng#1178
mochaaP added a commit to mcha-forks/quickjs that referenced this pull request Oct 11, 2025
fixup: 08db51a ("Add ref-counted string slices", quickjs-ng#1175)
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