diff --git a/compiler/rockql-parser/src/lib.rs b/compiler/rockql-parser/src/lib.rs index 6b22432..04e1d44 100644 --- a/compiler/rockql-parser/src/lib.rs +++ b/compiler/rockql-parser/src/lib.rs @@ -214,10 +214,36 @@ fn parse_sort(rest: &str, span: Span) -> Result { fn parse_take(rest: &str, span: Span) -> Result { require_value(rest, "expected a row count after `take`", span)?; - let count = rest - .replace('_', "") - .parse::() - .map_err(|_| Diagnostic::new("`take` requires a non-negative integer row count", span))?; + // ⚡ Bolt Optimization: Manually parse the integer without allocating an intermediate + // String via `.replace('_', "")`. This directly iterates over bytes, ignoring `_`. + let mut count: u64 = 0; + let mut has_digits = false; + for &byte in rest.as_bytes() { + if byte == b'_' { + continue; + } else if byte.is_ascii_digit() { + has_digits = true; + let digit = (byte - b'0') as u64; + count = count + .checked_mul(10) + .and_then(|c| c.checked_add(digit)) + .ok_or_else(|| { + Diagnostic::new("`take` requires a non-negative integer row count", span) + })?; + } else { + return Err(Diagnostic::new( + "`take` requires a non-negative integer row count", + span, + )); + } + } + + if !has_digits { + return Err(Diagnostic::new( + "`take` requires a non-negative integer row count", + span, + )); + } Ok(Transform::Take { count }) } diff --git a/compiler/rockql-sql/src/lib.rs b/compiler/rockql-sql/src/lib.rs index 34a76b9..2baee83 100644 --- a/compiler/rockql-sql/src/lib.rs +++ b/compiler/rockql-sql/src/lib.rs @@ -24,13 +24,18 @@ impl FromStr for Dialect { type Err = String; fn from_str(value: &str) -> Result { - match value.to_ascii_lowercase().as_str() { - "generic" | "sql" => Ok(Self::Generic), - "sqlite" | "sqlite3" => Ok(Self::Sqlite), - "postgres" | "postgresql" => Ok(Self::Postgres), - _ => Err(format!( + // ⚡ Bolt Optimization: Use `eq_ignore_ascii_case` to avoid intermediate String allocation from `to_ascii_lowercase()` + if value.eq_ignore_ascii_case("generic") || value.eq_ignore_ascii_case("sql") { + Ok(Self::Generic) + } else if value.eq_ignore_ascii_case("sqlite") || value.eq_ignore_ascii_case("sqlite3") { + Ok(Self::Sqlite) + } else if value.eq_ignore_ascii_case("postgres") || value.eq_ignore_ascii_case("postgresql") + { + Ok(Self::Postgres) + } else { + Err(format!( "unsupported SQL target `{value}`; expected generic, sqlite, or postgres" - )), + )) } } }