Skip to content

perf: memoize Rope::from_str(source_text) #5500

@Boshen

Description

@Boshen

/// Get line and column from offset and source text.
///
/// Line number starts at 1.
/// Column number is in UTF-16 characters, and starts at 1.
///
/// This matches Babel's output.
pub fn get_line_column(offset: u32, source_text: &str) -> (usize, usize) {
let offset = offset as usize;
let rope = Rope::from_str(source_text);
// Get line number and byte offset of start of line
let line_index = rope.byte_to_line(offset);
let line_offset = rope.line_to_byte(line_index);
// Get column number
let column_index = source_text[line_offset..offset].encode_utf16().count();
// line and column are zero-indexed, but we want 1-indexed
(line_index + 1, column_index + 1)
}

fn offset_to_position(offset: usize, source_text: &str) -> Option<Position> {
let rope = Rope::from_str(source_text);
let line = rope.try_byte_to_line(offset).ok()?;
let first_char_of_line = rope.try_line_to_char(line).ok()?;
// Original offset is byte, but Rope uses char offset
let offset = rope.try_byte_to_char(offset).ok()?;
let column = offset - first_char_of_line;
Some(Position::new(line as u32, column as u32))
}

Rope::from_str(source_text) is too expensive.

image

Metadata

Metadata

Assignees

Labels

A-transformerArea - Transformer / TranspilerC-bugCategory - BugC-performanceCategory - Solution not expected to change functional behavior, only performance

Type

No type

Priority

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions