-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't compile regex at every function call. #76818
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
//! A helpful diagram for debugging dataflow problems. | ||
use std::borrow::Cow; | ||
use std::lazy::SyncOnceCell; | ||
use std::{io, ops, str}; | ||
|
||
use regex::Regex; | ||
|
@@ -570,6 +571,13 @@ where | |
} | ||
} | ||
|
||
macro_rules! regex { | ||
($re:literal $(,)?) => {{ | ||
static RE: SyncOnceCell<regex::Regex> = SyncOnceCell::new(); | ||
RE.get_or_init(|| Regex::new($re).unwrap()) | ||
Comment on lines
+576
to
+577
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like this should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Depends on code-style I guess. My preference would be to use But I also can see "don't overthink this and use just |
||
}}; | ||
} | ||
|
||
fn diff_pretty<T, C>(new: T, old: T, ctxt: &C) -> String | ||
where | ||
T: DebugWithContext<C>, | ||
|
@@ -578,7 +586,7 @@ where | |
return String::new(); | ||
} | ||
|
||
let re = Regex::new("\t?\u{001f}([+-])").unwrap(); | ||
let re = regex!("\t?\u{001f}([+-])"); | ||
|
||
let raw_diff = format!("{:#?}", DebugDiffWithAdapter { new, old, ctxt }); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I quite like this. I think it would be a good addition to the
regex
crate (behind a feature for now). WDYT @hbina?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have opened an issue here rust-lang/regex#709
I am still new to this so I haven't open a PR yet.