Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Rope benchmarks: Generate random strings measured in bytes, not chars (#39951)

Martin Poolb519f53b3eca
diff --git a/crates/rope/benches/rope_benchmark.rs b/crates/rope/benches/rope_benchmark.rs
index 4ae6f1b54f1..030bec01df4 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 {