Skip to content
Open
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
19 changes: 13 additions & 6 deletions compiler/rockql-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ impl Display for Diagnostic {
}
}

/// Represents a raw query segment before parsing.
///
/// ⚑ Bolt Optimization:
/// Previously `Segment` stored `text: String`, causing an allocation for every segment
/// split by a pipe. By storing a reference to the original source string (`&'a str`),
/// we eliminate multiple heap allocations per parsed query, reducing memory usage
/// and speeding up parsing of pipelines.
#[derive(Debug)]
struct Segment {
text: String,
struct Segment<'a> {
text: &'a str,
span: Span,
}

Expand All @@ -47,7 +54,7 @@ pub fn parse(source: &str) -> Result<Query, Vec<Diagnostic>> {
let mut diagnostics = Vec::new();

for segment in segments {
match parse_transform(&segment.text, segment.span) {
match parse_transform(segment.text, segment.span) {
Ok(transform) => transforms.push(SpannedTransform::new(segment.span, transform)),
Err(diagnostic) => diagnostics.push(diagnostic),
}
Expand All @@ -64,7 +71,7 @@ pub fn format_source(source: &str) -> Result<String, Vec<Diagnostic>> {
parse(source).map(|query| format!("{query}\n"))
}

fn split_segments(source: &str) -> Vec<Segment> {
fn split_segments<'a>(source: &'a str) -> Vec<Segment<'a>> {
let mut segments = Vec::new();

for (line_index, line) in source.lines().enumerate() {
Expand All @@ -88,15 +95,15 @@ fn split_segments(source: &str) -> Vec<Segment> {
segments
}

fn push_segment(segments: &mut Vec<Segment>, raw: &str, line: usize, byte_start: usize) {
fn push_segment<'a>(segments: &mut Vec<Segment<'a>>, raw: &'a str, line: usize, byte_start: usize) {
let text = raw.trim();
if text.is_empty() {
return;
}

let leading_bytes = raw.find(text).unwrap_or(0);
segments.push(Segment {
text: text.to_owned(),
text,
span: Span::new(line, byte_start + leading_bytes + 1),
});
}
Expand Down
Loading