Skip to content

Commit 1710967

Browse files
authored
improve regex validation performance. (#825)
1 parent b2ba44f commit 1710967

File tree

8 files changed

+64
-64
lines changed

8 files changed

+64
-64
lines changed

Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ thiserror = "2.0.12"
4444
trybuild = "1.0.105"
4545
unicode-ident = "1.0.18"
4646
uuid = "1.16.0"
47+
criterion = "0.5.1"

rust-toolchain.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "1.80.1"
2+
channel = "1.81.0"
33
profile = "default"

typify-impl/src/type_entry.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1527,10 +1527,14 @@ impl TypeEntry {
15271527
}
15281528
}
15291529
});
1530+
15301531
let pat = pattern.as_ref().map(|p| {
15311532
let err = format!("doesn't match pattern \"{}\"", p);
15321533
quote! {
1533-
if regress::Regex::new(#p).unwrap().find(value).is_none() {
1534+
static PATTERN: ::std::sync::LazyLock<::regress::Regex> = ::std::sync::LazyLock::new(|| {
1535+
::regress::Regex::new(#p).unwrap()
1536+
});
1537+
if (&*PATTERN).find(value).is_none() {
15341538
return Err(#err.into());
15351539
}
15361540
}

typify-impl/tests/vega.out

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22327,11 +22327,9 @@ impl ::std::convert::From<&DataVariant2FormatVariant0Subtype0ParseVariant1ValueV
2232722327
impl ::std::str::FromStr for DataVariant2FormatVariant0Subtype0ParseVariant1ValueVariant1 {
2232822328
type Err = self::error::ConversionError;
2232922329
fn from_str(value: &str) -> ::std::result::Result<Self, self::error::ConversionError> {
22330-
if regress::Regex::new("^(date|utc):.*$")
22331-
.unwrap()
22332-
.find(value)
22333-
.is_none()
22334-
{
22330+
static PATTERN: ::std::sync::LazyLock<::regress::Regex> =
22331+
::std::sync::LazyLock::new(|| ::regress::Regex::new("^(date|utc):.*$").unwrap());
22332+
if (&*PATTERN).find(value).is_none() {
2233522333
return Err("doesn't match pattern \"^(date|utc):.*$\"".into());
2233622334
}
2233722335
Ok(Self(value.to_string()))
@@ -22849,11 +22847,9 @@ impl ::std::convert::From<&DataVariant2FormatVariant0Subtype1ParseVariant1ValueV
2284922847
impl ::std::str::FromStr for DataVariant2FormatVariant0Subtype1ParseVariant1ValueVariant1 {
2285022848
type Err = self::error::ConversionError;
2285122849
fn from_str(value: &str) -> ::std::result::Result<Self, self::error::ConversionError> {
22852-
if regress::Regex::new("^(date|utc):.*$")
22853-
.unwrap()
22854-
.find(value)
22855-
.is_none()
22856-
{
22850+
static PATTERN: ::std::sync::LazyLock<::regress::Regex> =
22851+
::std::sync::LazyLock::new(|| ::regress::Regex::new("^(date|utc):.*$").unwrap());
22852+
if (&*PATTERN).find(value).is_none() {
2285722853
return Err("doesn't match pattern \"^(date|utc):.*$\"".into());
2285822854
}
2285922855
Ok(Self(value.to_string()))
@@ -23430,11 +23426,9 @@ impl ::std::convert::From<&DataVariant2FormatVariant0Subtype2ParseVariant1ValueV
2343023426
impl ::std::str::FromStr for DataVariant2FormatVariant0Subtype2ParseVariant1ValueVariant1 {
2343123427
type Err = self::error::ConversionError;
2343223428
fn from_str(value: &str) -> ::std::result::Result<Self, self::error::ConversionError> {
23433-
if regress::Regex::new("^(date|utc):.*$")
23434-
.unwrap()
23435-
.find(value)
23436-
.is_none()
23437-
{
23429+
static PATTERN: ::std::sync::LazyLock<::regress::Regex> =
23430+
::std::sync::LazyLock::new(|| ::regress::Regex::new("^(date|utc):.*$").unwrap());
23431+
if (&*PATTERN).find(value).is_none() {
2343823432
return Err("doesn't match pattern \"^(date|utc):.*$\"".into());
2343923433
}
2344023434
Ok(Self(value.to_string()))
@@ -24020,11 +24014,9 @@ impl ::std::convert::From<&DataVariant2FormatVariant0Subtype3ParseVariant1ValueV
2402024014
impl ::std::str::FromStr for DataVariant2FormatVariant0Subtype3ParseVariant1ValueVariant1 {
2402124015
type Err = self::error::ConversionError;
2402224016
fn from_str(value: &str) -> ::std::result::Result<Self, self::error::ConversionError> {
24023-
if regress::Regex::new("^(date|utc):.*$")
24024-
.unwrap()
24025-
.find(value)
24026-
.is_none()
24027-
{
24017+
static PATTERN: ::std::sync::LazyLock<::regress::Regex> =
24018+
::std::sync::LazyLock::new(|| ::regress::Regex::new("^(date|utc):.*$").unwrap());
24019+
if (&*PATTERN).find(value).is_none() {
2402824020
return Err("doesn't match pattern \"^(date|utc):.*$\"".into());
2402924021
}
2403024022
Ok(Self(value.to_string()))
@@ -25225,11 +25217,9 @@ impl ::std::convert::From<&DataVariant3FormatVariant0Subtype0ParseVariant1ValueV
2522525217
impl ::std::str::FromStr for DataVariant3FormatVariant0Subtype0ParseVariant1ValueVariant1 {
2522625218
type Err = self::error::ConversionError;
2522725219
fn from_str(value: &str) -> ::std::result::Result<Self, self::error::ConversionError> {
25228-
if regress::Regex::new("^(date|utc):.*$")
25229-
.unwrap()
25230-
.find(value)
25231-
.is_none()
25232-
{
25220+
static PATTERN: ::std::sync::LazyLock<::regress::Regex> =
25221+
::std::sync::LazyLock::new(|| ::regress::Regex::new("^(date|utc):.*$").unwrap());
25222+
if (&*PATTERN).find(value).is_none() {
2523325223
return Err("doesn't match pattern \"^(date|utc):.*$\"".into());
2523425224
}
2523525225
Ok(Self(value.to_string()))
@@ -25747,11 +25737,9 @@ impl ::std::convert::From<&DataVariant3FormatVariant0Subtype1ParseVariant1ValueV
2574725737
impl ::std::str::FromStr for DataVariant3FormatVariant0Subtype1ParseVariant1ValueVariant1 {
2574825738
type Err = self::error::ConversionError;
2574925739
fn from_str(value: &str) -> ::std::result::Result<Self, self::error::ConversionError> {
25750-
if regress::Regex::new("^(date|utc):.*$")
25751-
.unwrap()
25752-
.find(value)
25753-
.is_none()
25754-
{
25740+
static PATTERN: ::std::sync::LazyLock<::regress::Regex> =
25741+
::std::sync::LazyLock::new(|| ::regress::Regex::new("^(date|utc):.*$").unwrap());
25742+
if (&*PATTERN).find(value).is_none() {
2575525743
return Err("doesn't match pattern \"^(date|utc):.*$\"".into());
2575625744
}
2575725745
Ok(Self(value.to_string()))
@@ -26328,11 +26316,9 @@ impl ::std::convert::From<&DataVariant3FormatVariant0Subtype2ParseVariant1ValueV
2632826316
impl ::std::str::FromStr for DataVariant3FormatVariant0Subtype2ParseVariant1ValueVariant1 {
2632926317
type Err = self::error::ConversionError;
2633026318
fn from_str(value: &str) -> ::std::result::Result<Self, self::error::ConversionError> {
26331-
if regress::Regex::new("^(date|utc):.*$")
26332-
.unwrap()
26333-
.find(value)
26334-
.is_none()
26335-
{
26319+
static PATTERN: ::std::sync::LazyLock<::regress::Regex> =
26320+
::std::sync::LazyLock::new(|| ::regress::Regex::new("^(date|utc):.*$").unwrap());
26321+
if (&*PATTERN).find(value).is_none() {
2633626322
return Err("doesn't match pattern \"^(date|utc):.*$\"".into());
2633726323
}
2633826324
Ok(Self(value.to_string()))
@@ -26918,11 +26904,9 @@ impl ::std::convert::From<&DataVariant3FormatVariant0Subtype3ParseVariant1ValueV
2691826904
impl ::std::str::FromStr for DataVariant3FormatVariant0Subtype3ParseVariant1ValueVariant1 {
2691926905
type Err = self::error::ConversionError;
2692026906
fn from_str(value: &str) -> ::std::result::Result<Self, self::error::ConversionError> {
26921-
if regress::Regex::new("^(date|utc):.*$")
26922-
.unwrap()
26923-
.find(value)
26924-
.is_none()
26925-
{
26907+
static PATTERN: ::std::sync::LazyLock<::regress::Regex> =
26908+
::std::sync::LazyLock::new(|| ::regress::Regex::new("^(date|utc):.*$").unwrap());
26909+
if (&*PATTERN).find(value).is_none() {
2692626910
return Err("doesn't match pattern \"^(date|utc):.*$\"".into());
2692726911
}
2692826912
Ok(Self(value.to_string()))
@@ -31398,7 +31382,9 @@ impl ::std::convert::From<&EncodeKey> for EncodeKey {
3139831382
impl ::std::str::FromStr for EncodeKey {
3139931383
type Err = self::error::ConversionError;
3140031384
fn from_str(value: &str) -> ::std::result::Result<Self, self::error::ConversionError> {
31401-
if regress::Regex::new("^.+$").unwrap().find(value).is_none() {
31385+
static PATTERN: ::std::sync::LazyLock<::regress::Regex> =
31386+
::std::sync::LazyLock::new(|| ::regress::Regex::new("^.+$").unwrap());
31387+
if (&*PATTERN).find(value).is_none() {
3140231388
return Err("doesn't match pattern \"^.+$\"".into());
3140331389
}
3140431390
Ok(Self(value.to_string()))
@@ -107885,11 +107871,11 @@ impl ::std::convert::From<&TitleVariant1EncodeSubtype0Key> for TitleVariant1Enco
107885107871
impl ::std::str::FromStr for TitleVariant1EncodeSubtype0Key {
107886107872
type Err = self::error::ConversionError;
107887107873
fn from_str(value: &str) -> ::std::result::Result<Self, self::error::ConversionError> {
107888-
if regress::Regex::new("^(?!interactive|name|style).+$")
107889-
.unwrap()
107890-
.find(value)
107891-
.is_none()
107892-
{
107874+
static PATTERN: ::std::sync::LazyLock<::regress::Regex> =
107875+
::std::sync::LazyLock::new(|| {
107876+
::regress::Regex::new("^(?!interactive|name|style).+$").unwrap()
107877+
});
107878+
if (&*PATTERN).find(value).is_none() {
107893107879
return Err("doesn't match pattern \"^(?!interactive|name|style).+$\"".into());
107894107880
}
107895107881
Ok(Self(value.to_string()))

typify/tests/schemas/id-or-name.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,9 @@ impl ::std::convert::From<&IdOrYoloYolo> for IdOrYoloYolo {
324324
impl ::std::str::FromStr for IdOrYoloYolo {
325325
type Err = self::error::ConversionError;
326326
fn from_str(value: &str) -> ::std::result::Result<Self, self::error::ConversionError> {
327-
if regress::Regex::new(".*").unwrap().find(value).is_none() {
327+
static PATTERN: ::std::sync::LazyLock<::regress::Regex> =
328+
::std::sync::LazyLock::new(|| ::regress::Regex::new(".*").unwrap());
329+
if (&*PATTERN).find(value).is_none() {
328330
return Err("doesn't match pattern \".*\"".into());
329331
}
330332
Ok(Self(value.to_string()))
@@ -403,7 +405,14 @@ impl ::std::str::FromStr for Name {
403405
if value.chars().count() > 63usize {
404406
return Err("longer than 63 characters".into());
405407
}
406-
if regress :: Regex :: new ("^(?![0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$)^[a-z][a-z0-9-]*[a-zA-Z0-9]$") . unwrap () . find (value) . is_none () { return Err ("doesn't match pattern \"^(?![0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$)^[a-z][a-z0-9-]*[a-zA-Z0-9]$\"" . into ()) ; }
408+
static PATTERN: ::std::sync::LazyLock<::regress::Regex> = ::std::sync::LazyLock::new(
409+
|| {
410+
:: regress :: Regex :: new ("^(?![0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$)^[a-z][a-z0-9-]*[a-zA-Z0-9]$") . unwrap ()
411+
},
412+
);
413+
if (&*PATTERN).find(value).is_none() {
414+
return Err ("doesn't match pattern \"^(?![0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$)^[a-z][a-z0-9-]*[a-zA-Z0-9]$\"" . into ()) ;
415+
}
407416
Ok(Self(value.to_string()))
408417
}
409418
}

typify/tests/schemas/property-pattern.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,9 @@ impl ::std::convert::From<&TestGrammarForPatternPropertiesRulesKey>
102102
impl ::std::str::FromStr for TestGrammarForPatternPropertiesRulesKey {
103103
type Err = self::error::ConversionError;
104104
fn from_str(value: &str) -> ::std::result::Result<Self, self::error::ConversionError> {
105-
if regress::Regex::new("^[a-zA-Z_]\\w*$")
106-
.unwrap()
107-
.find(value)
108-
.is_none()
109-
{
105+
static PATTERN: ::std::sync::LazyLock<::regress::Regex> =
106+
::std::sync::LazyLock::new(|| ::regress::Regex::new("^[a-zA-Z_]\\w*$").unwrap());
107+
if (&*PATTERN).find(value).is_none() {
110108
return Err("doesn't match pattern \"^[a-zA-Z_]\\w*$\"".into());
111109
}
112110
Ok(Self(value.to_string()))

typify/tests/schemas/types-with-more-impls.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ impl ::std::convert::From<&PatternString> for PatternString {
5858
impl ::std::str::FromStr for PatternString {
5959
type Err = self::error::ConversionError;
6060
fn from_str(value: &str) -> ::std::result::Result<Self, self::error::ConversionError> {
61-
if regress::Regex::new("xx").unwrap().find(value).is_none() {
61+
static PATTERN: ::std::sync::LazyLock<::regress::Regex> =
62+
::std::sync::LazyLock::new(|| ::regress::Regex::new("xx").unwrap());
63+
if (&*PATTERN).find(value).is_none() {
6264
return Err("doesn't match pattern \"xx\"".into());
6365
}
6466
Ok(Self(value.to_string()))

0 commit comments

Comments
 (0)