From b519f53b3ecab94b0d9db6c87261657fb1c48c38 Mon Sep 17 00:00:00 2001 From: Martin Pool Date: Tue, 21 Oct 2025 18:42:05 -0400 Subject: [PATCH] Rope benchmarks: Generate random strings measured in bytes, not chars (#39951) Follows on from https://github.com/zed-industries/zed/pull/39949. Again I'm not 100% sure of the intent but I think this is a fix: `generate_random_string(rng, 4096)` would previously give you a string of 4096 *chars* which could be anywhere between 4kB and 16kB in bytes. This seems probably not what was intended, because Ropes generally work in bytes not chars, including for the offsets used to index into them. This seems to possibly cause a _regression_ in benchmark performance, which is surprising because it should generally cause smaller test data. But, possibly it's doing better at exercising different paths? cc @mrnugget Release Notes: - N/A --- crates/rope/benches/rope_benchmark.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/crates/rope/benches/rope_benchmark.rs b/crates/rope/benches/rope_benchmark.rs index 4ae6f1b54f19b756e12cc399181bdf6b5d894ad5..030bec01df4d223cd5288842ba0f9c1386dac31b 100644 --- a/crates/rope/benches/rope_benchmark.rs +++ b/crates/rope/benches/rope_benchmark.rs @@ -9,11 +9,21 @@ use rope::{Point, Rope}; use sum_tree::Bias; use util::RandomCharIter; -/// Generate a random text of the given length using the provided RNG. +/// Returns a biased random string whose UTF-8 length is close to but no more than `len` bytes. /// -/// *Note*: The length is in *characters*, not bytes. -fn generate_random_text(rng: &mut StdRng, text_len: usize) -> String { - RandomCharIter::new(rng).take(text_len).collect() +/// The string is biased towards characters expected to occur in text or likely to exercise edge +/// cases. +fn generate_random_text(rng: &mut StdRng, len: usize) -> String { + let mut str = String::with_capacity(len); + let mut chars = RandomCharIter::new(rng); + loop { + let ch = chars.next().unwrap(); + if str.len() + ch.len_utf8() > len { + break; + } + str.push(ch); + } + str } fn generate_random_rope(rng: &mut StdRng, text_len: usize) -> Rope {