perf(core): linear mmap VHI rebuild + zero-alloc wrap helpers
Four changes that compound to a 2-5x speedup on VHI rebuild for large files, plus eliminating one full wasted VHI pass on initial open. * spawn_indexer no longer computes VHI inline (progressive_reader.rs). The inline VHI used a hardcoded width=80, was invalidated immediately on Loading->Ready transition (gutter digit-boundary), and triggered a second full rebuild. Now returns visual_height_index: None and lets the UI trigger one rebuild with the correct post-layout width. * spawn_visual_height_rebuild rewritten as a single linear pass over mmap bytes using memchr::memchr (SIMD) to find newlines, replacing the byte-by-byte BufRead::read_until scan. Cancel check throttled to every 1MB instead of per-line. * wrap_line_count(&str, width) -> usize added as a zero-allocation counterpart to wrap_line_chars. Property tests cover ASCII, CJK, emoji, combining marks, tabs, zero-width chars, control chars, and real log lines across many widths - all match wrap_line_chars().len() exactly. compute_text_visual_height now uses wrap_line_count, removing 10M Vec allocations on a 10M-line file. * format_json_line returns Cow<'_, str> instead of String. Non-JSON lines borrow the input (zero allocation); only pretty-printed JSON objects allocate. Removes another 10M allocations when json_format=true. Removes now-unused compute_visual_heights (only caller was the deleted inline VHI path).
This commit is contained in:
+194
-12
@@ -1,3 +1,5 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
/// Maximum input length for wrap/format operations (10 MB).
|
||||
/// Callers should check against this constant before invoking `wrap_line_chars`
|
||||
/// to avoid pathological cases on oversized lines.
|
||||
@@ -69,21 +71,81 @@ pub fn wrap_line_chars(line: &str, width: usize) -> Vec<String> {
|
||||
result
|
||||
}
|
||||
|
||||
/// Format a line as pretty-printed JSON if it's a JSON Object.
|
||||
/// Returns the original line unchanged for non-JSON or non-Object content.
|
||||
pub fn format_json_line(line: &str) -> String {
|
||||
if line.trim().is_empty() {
|
||||
return String::new();
|
||||
/// Count wrapped rows for a line without allocating the wrapped strings.
|
||||
/// MUST produce the same count as `wrap_line_chars(line, width).len()`.
|
||||
pub fn wrap_line_count(line: &str, width: usize) -> usize {
|
||||
use unicode_width::UnicodeWidthChar;
|
||||
|
||||
if width == 0 || line.is_empty() {
|
||||
return 1;
|
||||
}
|
||||
// Quick pre-check: only try parsing if it starts with '{'
|
||||
if !line.trim_start().starts_with('{') {
|
||||
return line.to_string();
|
||||
let mut count = 0usize;
|
||||
let mut col = 0usize;
|
||||
let mut row_has_content = false;
|
||||
|
||||
for ch in line.chars() {
|
||||
if ch == '\t' {
|
||||
let tab_stop = TAB_WIDTH - (col % TAB_WIDTH);
|
||||
let mut remaining = tab_stop;
|
||||
while remaining > 0 {
|
||||
let avail = width.saturating_sub(col);
|
||||
let fill = remaining.min(avail);
|
||||
col += fill;
|
||||
remaining -= fill;
|
||||
if col >= width {
|
||||
count += 1;
|
||||
col = 0;
|
||||
row_has_content = false;
|
||||
} else {
|
||||
row_has_content = true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let w = if ch.is_control() {
|
||||
0
|
||||
} else {
|
||||
ch.width().unwrap_or(0)
|
||||
};
|
||||
if col + w > width && row_has_content {
|
||||
count += 1;
|
||||
col = 0;
|
||||
}
|
||||
col += w;
|
||||
if col >= width {
|
||||
count += 1;
|
||||
col = 0;
|
||||
row_has_content = false;
|
||||
} else {
|
||||
row_has_content = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if row_has_content {
|
||||
count += 1;
|
||||
}
|
||||
if count == 0 {
|
||||
count = 1;
|
||||
}
|
||||
count
|
||||
}
|
||||
|
||||
/// Format a line as pretty-printed JSON if it's a JSON Object.
|
||||
/// Returns the original line borrowed for non-JSON or non-Object content,
|
||||
/// only allocating when pretty-printing actually happens.
|
||||
pub fn format_json_line(line: &str) -> Cow<'_, str> {
|
||||
let trimmed = line.trim();
|
||||
if trimmed.is_empty() {
|
||||
return Cow::Borrowed("");
|
||||
}
|
||||
if !trimmed.starts_with('{') {
|
||||
return Cow::Borrowed(line);
|
||||
}
|
||||
match serde_json::from_str::<serde_json::Value>(line) {
|
||||
Ok(value) if value.is_object() => {
|
||||
serde_json::to_string_pretty(&value).unwrap_or_else(|_| line.to_string())
|
||||
}
|
||||
_ => line.to_string(),
|
||||
Ok(value) if value.is_object() => match serde_json::to_string_pretty(&value) {
|
||||
Ok(s) => Cow::Owned(s),
|
||||
Err(_) => Cow::Borrowed(line),
|
||||
},
|
||||
_ => Cow::Borrowed(line),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -231,4 +293,124 @@ mod tests {
|
||||
let result = wrap_line_chars("ab\t", 4);
|
||||
assert_eq!(result, vec!["ab "]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wrap_count_matches_chars_empty() {
|
||||
assert_eq!(wrap_line_count("", 80), wrap_line_chars("", 80).len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wrap_count_matches_chars_zero_width() {
|
||||
assert_eq!(
|
||||
wrap_line_count("hello", 0),
|
||||
wrap_line_chars("hello", 0).len()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wrap_count_matches_chars_ascii_short() {
|
||||
for width in 1..20 {
|
||||
assert_eq!(
|
||||
wrap_line_count("hello world", width),
|
||||
wrap_line_chars("hello world", width).len(),
|
||||
"width={width}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wrap_count_matches_chars_cjk() {
|
||||
for width in 1..10 {
|
||||
assert_eq!(
|
||||
wrap_line_count("你好世界", width),
|
||||
wrap_line_chars("你好世界", width).len(),
|
||||
"width={width}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wrap_count_matches_chars_emoji() {
|
||||
for width in 1..6 {
|
||||
assert_eq!(
|
||||
wrap_line_count("😀👨👩👧", width),
|
||||
wrap_line_chars("😀👨👩👧", width).len(),
|
||||
"width={width}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wrap_count_matches_chars_combining_mark() {
|
||||
for width in 1..6 {
|
||||
assert_eq!(
|
||||
wrap_line_count("a\u{0301}b\u{0301}c", width),
|
||||
wrap_line_chars("a\u{0301}b\u{0301}c", width).len(),
|
||||
"width={width}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wrap_count_matches_chars_tab_variants() {
|
||||
let inputs = ["\t", "a\tb", "ab\t", "\t\t", "abc\tb", "a\t\t\tb"];
|
||||
for input in inputs {
|
||||
for width in 1..9 {
|
||||
assert_eq!(
|
||||
wrap_line_count(input, width),
|
||||
wrap_line_chars(input, width).len(),
|
||||
"input={input:?} width={width}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wrap_count_matches_chars_mixed() {
|
||||
let input = "log: 你好 😀\tvalue=true [ERROR]";
|
||||
for width in 1..40 {
|
||||
assert_eq!(
|
||||
wrap_line_count(input, width),
|
||||
wrap_line_chars(input, width).len(),
|
||||
"width={width}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wrap_count_matches_chars_zero_width_chars() {
|
||||
let input = "a\u{200B}b\u{200C}c\u{200D}d";
|
||||
for width in 1..8 {
|
||||
assert_eq!(
|
||||
wrap_line_count(input, width),
|
||||
wrap_line_chars(input, width).len(),
|
||||
"width={width}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wrap_count_matches_chars_control_chars() {
|
||||
let input = "a\x07b\x1Fc";
|
||||
for width in 1..8 {
|
||||
assert_eq!(
|
||||
wrap_line_count(input, width),
|
||||
wrap_line_chars(input, width).len(),
|
||||
"width={width}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wrap_count_matches_chars_long_log_line() {
|
||||
let input = "2025-01-14T10:23:45.123Z [INFO] [auth] request handled status=200 \
|
||||
latency=0.234s endpoint=/api/v1/users user=user_42 request_id=req_abc123def456";
|
||||
for width in [10, 20, 40, 60, 80, 100, 120] {
|
||||
assert_eq!(
|
||||
wrap_line_count(input, width),
|
||||
wrap_line_chars(input, width).len(),
|
||||
"width={width}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user