Repository navigation
feat(string): implement RopeString with thin vtable and lazy flattening - #5006
akash-R-A-J wants to merge 23 commits into
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #5006 +/- ##
===========================================
+ Coverage 47.24% 62.66% +15.42%
===========================================
Files 476 540 +64
Lines 46892 59053 +12161
===========================================
+ Hits 22154 37007 +14853
+ Misses 24738 22046 -2692 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| // SAFETY: Caller must ensure the type matches. | ||
| unsafe { self.ptr.cast::<T>().as_ref() } | ||
| #[must_use] | ||
| pub fn as_str(&self) -> JsStr<'static> { |
There was a problem hiding this comment.
This is unsound. It's returning a 'static reference to a temporary JsString, so you could deallocate the original string and the compiler will happily let you access the JsStr<'static>:
let s = JsString::from_str("undefined behaviour!");
let temp = s.as_str();
drop(s)
println!("{}", temp.display_lossy()); // UB!There was a problem hiding this comment.
Thanks for pointing this out! you're absolutely right, that was a leftover from the initial pointer-based POC while focusing on the memory layout. I've now refactored the vtable to use HRTBs for as_str which replaces that temporary shim with a lifetime binding. It was my fault.
zhuzhu81998
left a comment
There was a problem hiding this comment.
ci shows segmentaion fault (potential cause below)
| let specifier = specifier.cow_replace('/', "\\"); | ||
|
|
||
| let short_path = Path::new(&specifier); | ||
| let short_path = Path::new(&*specifier); |
There was a problem hiding this comment.
what does this have to do with rope strings?
| pub(crate) mod rope; | ||
| pub(crate) use rope::RopeString; | ||
|
|
||
| /// Header for all `JsString` allocations. |
There was a problem hiding this comment.
Then call this JsStringHeader instead of RawJsString perhaps?
There was a problem hiding this comment.
sounds good, will replace RawJsString with the suggested JsStringHeader
| unsafe impl Sync for RawJsString {} | ||
| // SAFETY: RawJsString contains only thread-safe data. | ||
| unsafe impl Send for RawJsString {} |
There was a problem hiding this comment.
do you need this somewhere?
| // SAFETY: We only mutate refcount and hash via atomic-casts when kind != Static. | ||
| unsafe impl Sync for JsStringVTable {} | ||
| // SAFETY: JsStringVTable contains only thread-safe data. | ||
| unsafe impl Send for JsStringVTable {} |
There was a problem hiding this comment.
same question: where is the need?
| /// A rope string that is a tree of other strings. | ||
| #[repr(C)] | ||
| pub(crate) struct RopeString { | ||
| /// Standardized header for all strings. | ||
| pub(crate) header: RawJsString, | ||
| pub(crate) left: JsString, | ||
| pub(crate) right: JsString, | ||
| flattened: OnceCell<JsString>, | ||
| pub(crate) depth: u8, | ||
| } |
There was a problem hiding this comment.
hmmm so it is my understanding that JsString here will most likely GC-tracked. And there is no way for GC to know that RopeString is holdig reference to the left and right here?
are yo doing something to prevent this?
There was a problem hiding this comment.
To clarify: JSStrings are not GC-tracked, they're independently ref-counted, so it's fine to not trace through them. I'm fairly certain the UB comes from the modifications made to as_str that removed the lifetime inheritance for JsStr<'static>.
There was a problem hiding this comment.
indeed its not gc. disabling the gc does not resolve the segmentation fault XD.
6587b0c to
951985f
Compare
Test262 conformance changes
Broken tests (10):Tested main commit: |
|
@jedel1043 I believe all requested changes have now been addressed. CI is green across all platforms, and test262 results match Please let me know if anything else needs adjustment. |
c21f4cb to
b6134ad
Compare
f21309a to
af923c7
Compare
|
Hey @jedel1043, whenever you have some time, could you please take another look at this PR? All previously mentioned issues should now be addressed, including the rope rebalancing changes, safety fixes, and flattening adjustments discussed in the earlier review. Here's the latest benchmark results: |
Test262 conformance changes
Tested main commit: |
f157f37 to
72cd1ee
Compare
|
The Check SemVer compatibility CI job failed for
What is needed: To address this cleanly, I have:
Could you confirm if excluding |
|
Just bump the version; we'll be releasing 0.23 this month anyways |
|
@jedel1043 |
zhuzhu81998
left a comment
There was a problem hiding this comment.
a few comments on the general side.
not yet checked the concrete implementation.
also, maybe an updated performance comparison for a final version would be nice.
| unused_crate_dependencies, | ||
| reason = "https://github.com/rust-lang/rust/issues/95513" | ||
| )] | ||
| #![allow(unused_crate_dependencies)] |
There was a problem hiding this comment.
what is this change for?
| /// that should not leak through the API. | ||
| #[allow(private_interfaces)] | ||
| pub(crate) trait InternalStringType: StringType { | ||
| pub trait InternalStringType: StringType { |
There was a problem hiding this comment.
you are making this internal type public?
| let specifier = specifier.cow_replace('/', "\\"); | ||
|
|
||
| let short_path = Path::new(&specifier); | ||
| let short_path = Path::new(&*specifier); |
| } | ||
|
|
||
| false | ||
| self.shape.lookup(key).is_some() |
There was a problem hiding this comment.
looks like a valid change but still, unrelated to RopeStrings?
| use boa_engine::string::JsStr; | ||
| use boa_engine::{JsString, js_string}; | ||
|
|
||
| #[allow(dead_code)] |
|
|
||
| /// Fibonacci numbers for rope balancing thresholds. | ||
| /// `F(n) = Fib(n + 2)`. A rope of depth `n` is balanced if its length >= `F(n)`. | ||
| static FIBONACCI_THRESHOLDS: [u32; 46] = [ |
There was a problem hiding this comment.
use a const fn which is evaluated at comptime instead of hardcoding might be more idiomatic
| use boa_engine::string::JsStr; | ||
| use boa_engine::{JsString, js_string}; | ||
|
|
||
| #[allow(dead_code)] |
| #[derive(Debug, Clone, Copy, Eq, PartialEq)] | ||
| #[repr(u8)] | ||
| pub(crate) enum JsStringKind { | ||
| pub enum JsStringKind { |
There was a problem hiding this comment.
also, visibility change really necessary?
…flatten, and use const fn for Fibonacci thresholds

This Pull Request fixes/closes #5005 .
This PR introduces Rope strings to
boa_stringand integrates them into the engine’s concatenation pipeline to eliminate the pathological O(N²) behavior of repeated string concatenation.Key Changes
Rope Strings
RopeStringrepresentation (core/string/src/vtable/rope.rs)Engine Integration
JsValue::addnow creates ropes for large concatenations (len(lhs) + len(rhs) > 1024)ConcatToStringupdated to use balanced batch concatenationString.prototype.concatrefactored to use the new APIThin VTable & Performance Recovery
clone,drop,as_str,code_unit_at)u64hash to speed up property lookupsptr_eq+ hash checks to optimize equality comparisonsBenchmarks
Concatenation Stress Test
≈ 21.7× faster
V8 Combined Suite
Summary
This change removes quadratic concatenation behavior while maintaining baseline performance for general workloads. The thin vtable and devirtualization ensure that ropes introduce minimal overhead when not used.