Skip to content

Commit 5d93da0

Browse files
author
Dima
committed
improve performance of regexp_count
1 parent cd69e37 commit 5d93da0

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

datafusion/functions/src/regex/regexpcount.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use datafusion_expr::{
3030
};
3131
use itertools::izip;
3232
use regex::Regex;
33-
use std::collections::hash_map::Entry;
3433
use std::collections::HashMap;
3534
use std::sync::{Arc, OnceLock};
3635

@@ -549,19 +548,16 @@ where
549548
}
550549
}
551550

552-
fn compile_and_cache_regex(
553-
regex: &str,
554-
flags: Option<&str>,
555-
regex_cache: &mut HashMap<String, Regex>,
556-
) -> Result<Regex, ArrowError> {
557-
match regex_cache.entry(regex.to_string()) {
558-
Entry::Vacant(entry) => {
559-
let compiled = compile_regex(regex, flags)?;
560-
entry.insert(compiled.clone());
561-
Ok(compiled)
562-
}
563-
Entry::Occupied(entry) => Ok(entry.get().to_owned()),
551+
fn compile_and_cache_regex<'a>(
552+
regex: &'a str,
553+
flags: Option<&'a str>,
554+
regex_cache: &'a mut HashMap<String, Regex>,
555+
) -> Result<&'a Regex, ArrowError> {
556+
if !regex_cache.contains_key(regex) {
557+
let compiled = compile_regex(regex, flags)?;
558+
regex_cache.insert(regex.to_string(), compiled);
564559
}
560+
Ok(regex_cache.get(regex).unwrap())
565561
}
566562

567563
fn compile_regex(regex: &str, flags: Option<&str>) -> Result<Regex, ArrowError> {

0 commit comments

Comments
 (0)