Skip to content

Commit 7a998de

Browse files
committed
feat: 10% faster by using uuid
- went from 2.66s on before to 2.44s after
1 parent 0f38d10 commit 7a998de

File tree

8 files changed

+60
-46
lines changed

8 files changed

+60
-46
lines changed

crates/lib/src/core/parser/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct ParseContext<'a> {
1616
match_depth: usize,
1717
track_progress: bool,
1818
pub(crate) terminators: Vec<Rc<dyn Matchable>>,
19-
parse_cache: AHashMap<((String, (usize, usize), &'static str, usize), String), MatchResult>,
19+
parse_cache: AHashMap<((String, (usize, usize), &'static str, usize), uuid::Uuid), MatchResult>,
2020
pub(crate) indentation_config: AHashMap<String, bool>,
2121
}
2222

@@ -139,15 +139,15 @@ impl<'a> ParseContext<'a> {
139139
pub(crate) fn check_parse_cache(
140140
&self,
141141
loc_key: (String, (usize, usize), &'static str, usize),
142-
matcher_key: String,
142+
matcher_key: uuid::Uuid,
143143
) -> Option<MatchResult> {
144144
self.parse_cache.get(&(loc_key, matcher_key)).cloned()
145145
}
146146

147147
pub(crate) fn put_parse_cache(
148148
&mut self,
149149
loc_key: (String, (usize, usize), &'static str, usize),
150-
matcher_key: String,
150+
matcher_key: uuid::Uuid,
151151
match_result: MatchResult,
152152
) {
153153
self.parse_cache.insert((loc_key, matcher_key), match_result);

crates/lib/src/core/parser/grammar/anyof.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub struct AnyNumberOf {
8585
pub allow_gaps: bool,
8686
pub optional: bool,
8787
pub parse_mode: ParseMode,
88-
cache_key: String,
88+
cache_key: Uuid,
8989
}
9090

9191
impl PartialEq for AnyNumberOf {
@@ -104,7 +104,7 @@ impl AnyNumberOf {
104104
optional: false,
105105
parse_mode: ParseMode::Strict,
106106
terminators: Vec::new(),
107-
cache_key: Uuid::new_v4().hyphenated().to_string(),
107+
cache_key: Uuid::new_v4(),
108108
}
109109
}
110110

@@ -292,8 +292,8 @@ impl Matchable for AnyNumberOf {
292292
Rc::new(new_grammar)
293293
}
294294

295-
fn cache_key(&self) -> String {
296-
self.cache_key.clone()
295+
fn cache_key(&self) -> Option<Uuid> {
296+
Some(self.cache_key)
297297
}
298298
}
299299

crates/lib/src/core/parser/grammar/base.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ pub struct BaseGrammar {
2828
terminators: Vec<Rc<dyn Matchable>>,
2929
reset_terminators: bool,
3030
parse_mode: ParseMode,
31-
cache_key: String,
31+
cache_key: Uuid,
3232
}
3333

3434
impl PartialEq for BaseGrammar {
3535
fn eq(&self, other: &Self) -> bool {
3636
// self.elements == other.elements &&
3737
self.allow_gaps == other.allow_gaps
3838
&& self.optional == other.optional
39-
// && self.terminators == other.terminators
39+
// && self.terminators == other.terminators
4040
&& self.reset_terminators == other.reset_terminators
4141
&& self.parse_mode == other.parse_mode
4242
&& self.cache_key == other.cache_key
@@ -52,7 +52,7 @@ impl BaseGrammar {
5252
reset_terminators: bool,
5353
parse_mode: ParseMode,
5454
) -> Self {
55-
let cache_key = Uuid::new_v4().to_string();
55+
let cache_key = Uuid::new_v4();
5656

5757
Self {
5858
elements,
@@ -98,8 +98,8 @@ impl Matchable for BaseGrammar {
9898
Ok(MatchResult::new(Vec::new(), Vec::new()))
9999
}
100100

101-
fn cache_key(&self) -> String {
102-
self.cache_key.clone()
101+
fn cache_key(&self) -> Option<Uuid> {
102+
Some(self.cache_key)
103103
}
104104
}
105105

@@ -112,7 +112,7 @@ pub struct Ref {
112112
reset_terminators: bool,
113113
allow_gaps: bool,
114114
optional: bool,
115-
cache_key: String,
115+
cache_key: Uuid,
116116
simple_cache: OnceCell<Option<(AHashSet<String>, AHashSet<String>)>>,
117117
}
118118

@@ -132,7 +132,7 @@ impl Ref {
132132
reset_terminators: false,
133133
allow_gaps: true,
134134
optional: false,
135-
cache_key: Uuid::new_v4().hyphenated().to_string(),
135+
cache_key: Uuid::new_v4(),
136136
simple_cache: OnceCell::new(),
137137
}
138138
}
@@ -245,8 +245,8 @@ impl Matchable for Ref {
245245
})
246246
}
247247

248-
fn cache_key(&self) -> String {
249-
self.cache_key.clone()
248+
fn cache_key(&self) -> Option<Uuid> {
249+
Some(self.cache_key)
250250
}
251251
}
252252

@@ -363,15 +363,25 @@ pub fn longest_trimmed_match(
363363
for (_idx, matcher) in enumerate(available_options) {
364364
let matcher_key = matcher.cache_key();
365365

366-
let match_result = match parse_context
367-
.check_parse_cache(loc_key.clone(), matcher_key.clone())
368-
{
369-
Some(match_result) => match_result,
370-
None => {
371-
let match_result = matcher.match_segments(segments, parse_context)?;
366+
let match_result = if let Some(matcher_key) = matcher_key {
367+
match parse_context.check_parse_cache(loc_key.clone(), matcher_key) {
368+
Some(match_result) => match_result,
369+
None => {
370+
let match_result = matcher.match_segments(segments, parse_context)?;
371+
parse_context.put_parse_cache(
372+
loc_key.clone(),
373+
matcher_key,
374+
match_result.clone(),
375+
);
376+
match_result
377+
}
378+
}
379+
} else {
380+
let match_result = matcher.match_segments(segments, parse_context)?;
381+
if let Some(matcher_key) = matcher_key {
372382
parse_context.put_parse_cache(loc_key.clone(), matcher_key, match_result.clone());
373-
match_result
374383
}
384+
match_result
375385
};
376386

377387
// No match. Skip this one.

crates/lib/src/core/parser/grammar/delimited.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::ops::{Deref, DerefMut};
22
use std::rc::Rc;
33

44
use ahash::AHashSet;
5+
use uuid::Uuid;
56

67
use super::anyof::{one_of, AnyNumberOf};
78
use super::base::{longest_trimmed_match, Ref};
@@ -26,7 +27,7 @@ pub struct Delimited {
2627
delimiter: Rc<dyn Matchable>,
2728
pub(crate) min_delimiters: Option<usize>,
2829
optional: bool,
29-
cache_key: String,
30+
cache_key: Uuid,
3031
}
3132

3233
impl Delimited {
@@ -37,7 +38,7 @@ impl Delimited {
3738
delimiter: Ref::new("CommaSegment").to_matchable(),
3839
min_delimiters: None,
3940
optional: false,
40-
cache_key: uuid::Uuid::new_v4().hyphenated().to_string(),
41+
cache_key: Uuid::new_v4(),
4142
}
4243
}
4344

@@ -249,8 +250,8 @@ impl Matchable for Delimited {
249250
Ok(MatchResult { matched_segments, unmatched_segments })
250251
}
251252

252-
fn cache_key(&self) -> String {
253-
self.cache_key.clone()
253+
fn cache_key(&self) -> Option<Uuid> {
254+
Some(self.cache_key)
254255
}
255256
}
256257

crates/lib/src/core/parser/grammar/noncode.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use ahash::AHashSet;
2+
use uuid::Uuid;
23

34
use crate::core::errors::SQLParseError;
45
use crate::core::parser::context::ParseContext;
@@ -40,8 +41,8 @@ impl Matchable for NonCodeMatcher {
4041
Ok(MatchResult::new(segments[0..idx].to_vec(), segments[idx..].to_vec()))
4142
}
4243

43-
fn cache_key(&self) -> String {
44-
"non-code-matcher".to_string()
44+
fn cache_key(&self) -> Option<Uuid> {
45+
None
4546
}
4647
}
4748

crates/lib/src/core/parser/grammar/sequence.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub struct Sequence {
113113
pub(crate) allow_gaps: bool,
114114
is_optional: bool,
115115
pub(crate) terminators: Vec<Rc<dyn Matchable>>,
116-
cache_key: String,
116+
cache_key: Uuid,
117117
}
118118

119119
impl Sequence {
@@ -124,7 +124,7 @@ impl Sequence {
124124
is_optional: false,
125125
parse_mode: ParseMode::Strict,
126126
terminators: Vec::new(),
127-
cache_key: Uuid::new_v4().hyphenated().to_string(),
127+
cache_key: Uuid::new_v4(),
128128
}
129129
}
130130

@@ -407,8 +407,8 @@ impl Matchable for Sequence {
407407
})
408408
}
409409

410-
fn cache_key(&self) -> String {
411-
self.cache_key.clone()
410+
fn cache_key(&self) -> Option<Uuid> {
411+
Some(self.cache_key)
412412
}
413413

414414
fn copy(
@@ -698,7 +698,7 @@ impl Matchable for Bracketed {
698698
})
699699
}
700700

701-
fn cache_key(&self) -> String {
701+
fn cache_key(&self) -> Option<Uuid> {
702702
self.this.cache_key()
703703
}
704704
}

crates/lib/src/core/parser/matchable.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ pub trait Matchable: Any + Segment + DynClone + Debug + DynEq {
8484
}
8585

8686
// A method to generate a unique cache key for the matchable object.
87-
fn cache_key(&self) -> String {
87+
//
88+
// Returns none for no caching key
89+
fn cache_key(&self) -> Option<uuid::Uuid> {
8890
unimplemented!("{}", std::any::type_name::<Self>())
8991
}
9092

crates/lib/src/core/parser/parsers.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ impl TypedParser {
8585
impl Segment for TypedParser {}
8686

8787
impl Matchable for TypedParser {
88-
fn cache_key(&self) -> String {
89-
Uuid::new_v4().hyphenated().to_string()
88+
fn cache_key(&self) -> Option<Uuid> {
89+
Some(Uuid::new_v4())
9090
}
9191

9292
fn simple(
@@ -124,7 +124,7 @@ pub struct StringParser {
124124
* Rust */
125125
optional: bool,
126126
trim_chars: Option<Vec<char>>,
127-
cache_key: String,
127+
cache_key: Uuid,
128128
}
129129

130130
impl StringParser {
@@ -145,7 +145,7 @@ impl StringParser {
145145
type_,
146146
optional,
147147
trim_chars,
148-
cache_key: Uuid::new_v4().hyphenated().to_string(),
148+
cache_key: Uuid::new_v4(),
149149
}
150150
}
151151

@@ -212,8 +212,8 @@ impl Matchable for StringParser {
212212
Ok(MatchResult::from_unmatched(segments.to_vec()))
213213
}
214214

215-
fn cache_key(&self) -> String {
216-
self.cache_key.clone()
215+
fn cache_key(&self) -> Option<Uuid> {
216+
Some(self.cache_key)
217217
}
218218
}
219219

@@ -225,7 +225,7 @@ pub struct RegexParser {
225225
_template: HashableFancyRegex,
226226
_anti_template: HashableFancyRegex,
227227
factory: fn(&dyn Segment) -> ErasedSegment,
228-
cache_key: String, // Add other fields as needed
228+
cache_key: Uuid, // Add other fields as needed
229229
}
230230

231231
impl PartialEq for RegexParser {
@@ -257,7 +257,7 @@ impl RegexParser {
257257
_template: HashableFancyRegex(template_pattern),
258258
_anti_template: HashableFancyRegex(anti_template_pattern),
259259
factory, // Initialize other fields here
260-
cache_key: Uuid::new_v4().hyphenated().to_string(),
260+
cache_key: Uuid::new_v4(),
261261
}
262262
}
263263

@@ -332,8 +332,8 @@ impl Matchable for RegexParser {
332332
Ok(MatchResult::from_unmatched(segments.to_vec()))
333333
}
334334

335-
fn cache_key(&self) -> String {
336-
self.cache_key.clone()
335+
fn cache_key(&self) -> Option<Uuid> {
336+
Some(self.cache_key)
337337
}
338338
}
339339

@@ -432,7 +432,7 @@ impl Matchable for MultiStringParser {
432432
Ok(MatchResult::from_unmatched(segments.to_vec()))
433433
}
434434

435-
fn cache_key(&self) -> String {
435+
fn cache_key(&self) -> Option<Uuid> {
436436
todo!()
437437
}
438438
}

0 commit comments

Comments
 (0)