Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rust/crates/psychoscript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ icu_datetime = { version = "2.4", default-features = false, features = ["compile
# Number and BigInt `toLocaleString`; already dependencies of icu_datetime.
icu_decimal = { version = "2.4", default-features = false, features = ["compiled_data"] }
fixed_decimal = { version = "0.7", default-features = false }
# Substring search for `includes`, `indexOf`, `split` and `replace`; already a dependency of regress and oxc.
memchr = "2"
# Parses the locale tags `localeCompare` takes; already a dependency of icu_collator.
icu_locale_core = { version = "2.4", default-features = false }
# Time zone inputs (`ZonedDateTime`, `ZoneNameTimestamp`) for `icu_datetime`.
Expand Down
32 changes: 23 additions & 9 deletions rust/crates/psychoscript/src/builtins/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,13 +414,20 @@ fn s_at(vm: &mut Vm, this: Value, args: &[Value], callee: Id) -> Res {
})
}

/// Where `needle` first is in `hay`, in bytes. The standard library's search sets up a two-way searcher for each
/// call, which costs more than the search itself for the short needles programs look for.
#[inline]
fn find_text(hay: &str, needle: &str) -> Option<usize> {
memchr::memmem::find(hay.as_bytes(), needle.as_bytes())
}

/// Finds `needle` in `hay` at or after code unit `from`, in code units.
fn find16(hay: StrRef, needle: StrRef, from: usize) -> Option<usize> {
if hay.ascii && needle.ascii {
if from > hay.s.len() {
return None;
}
return hay.s[from..].find(needle.s).map(|i| i + from);
return find_text(&hay.s[from..], needle.s).map(|i| i + from);
}
let h = hay.utf16();
let n = needle.utf16();
Expand Down Expand Up @@ -767,13 +774,20 @@ fn split(vm: &mut Vm, c: &mut Conversions, s: StrId, args: &[Value]) -> Res {
.map(|p| Value::Str(vm.atoms.alloc_units(p)))
.collect()
} else {
let pieces: Vec<String> = vm
.text(s)
.split(vm.text(sep))
.take(limit)
.map(str::to_owned)
.collect();
pieces.into_iter().map(|p| vm.string_val(p)).collect()
// One copy of the text and one searcher for the separator, then each piece is a short string.
let (text, sep) = (vm.text(s).to_owned(), vm.text(sep).to_owned());
let finder = memchr::memmem::Finder::new(sep.as_bytes());
let mut pieces = Vec::new();
let mut from = 0;
while pieces.len() < limit {
let at = finder.find(&text.as_bytes()[from..]).map(|i| from + i);
pieces.push(vm.string_of_str(&text[from..at.unwrap_or(text.len())]));
match at {
Some(at) => from = at + sep.len(),
None => break,
}
}
pieces
}
}
};
Expand Down Expand Up @@ -875,7 +889,7 @@ fn replace_in(vm: &mut Vm, c: &mut Conversions, s: StrId, args: &[Value], all: b
}
} else {
let mut from = 0;
while let Some(i) = subject[from..].find(&needle) {
while let Some(i) = find_text(&subject[from..], &needle) {
positions.push(from + i);
from += i + needle.len();
if !all {
Expand Down
13 changes: 13 additions & 0 deletions rust/crates/psychoscript/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,19 @@ impl Strings {
s
}

/// A string of text borrowed from elsewhere, a piece of another string as a rule. A short one goes at the end of
/// the buffer short strings share, so it allocates nothing of its own.
pub fn alloc_str(&mut self, s: &str) -> StrId {
if s.len() >= GROW_MIN || !s.is_ascii() {
return self.alloc(s.to_owned());
}
let g = self.arena();
let mut buf = std::mem::take(&mut self.grow[g]);
let start = buf.len();
buf.push_str(s);
self.push_grown(buf, g, start, true)
}

/// The strings a compile starts from: the fresh runtime's (`builtins::image`), so they have the same ids in
/// every run and a run's VM is a copy of the image.
pub fn fresh() -> Self {
Expand Down
11 changes: 11 additions & 0 deletions rust/crates/psychoscript/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,17 @@ impl Vm {
Value::Str(self.atoms.alloc(s))
}

/// `string_val` of borrowed text (`Strings::alloc_str`).
pub fn string_of_str(&mut self, s: &str) -> Value {
if self.atoms.bufs.len() >= MAX_STRINGS {
self.out_of_memory();
}
if let Some(m) = self.heap_meter.as_deref_mut() {
m.debt += 2 * s.len() as u64;
}
Value::Str(self.atoms.alloc_str(s))
}

/// The interned string for a fixed text, without allocating a new one each time.
pub fn static_str(&mut self, s: &str) -> Value {
let a = self.atoms.intern(s);
Expand Down