From 2cb32edaa974e413c9a9beef65468bbfd218ed8f Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Tue, 30 Mar 2021 12:10:06 +0300 Subject: [PATCH] fix(criterion): compile asc/desc regex only once use once_cell instead of lazy_static reorder imports --- milli/src/criterion.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/milli/src/criterion.rs b/milli/src/criterion.rs index 5d8ba09ba..40f9a3e0b 100644 --- a/milli/src/criterion.rs +++ b/milli/src/criterion.rs @@ -4,9 +4,14 @@ use std::fmt; use anyhow::{Context, bail}; use regex::Regex; use serde::{Serialize, Deserialize}; +use once_cell::sync::Lazy; use crate::facet::FacetType; +static ASC_DESC_REGEX: Lazy = Lazy::new(|| { + Regex::new(r#"(asc|desc)\(([\w_-]+)\)"#).unwrap() +}); + #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] pub enum Criterion { /// Sorted by increasing number of typos. @@ -39,8 +44,7 @@ impl Criterion { "wordsposition" => Ok(Criterion::WordsPosition), "exactness" => Ok(Criterion::Exactness), text => { - let re = Regex::new(r#"(asc|desc)\(([\w_-]+)\)"#)?; - let caps = re.captures(text).with_context(|| format!("unknown criterion name: {}", text))?; + let caps = ASC_DESC_REGEX.captures(text).with_context(|| format!("unknown criterion name: {}", text))?; let order = caps.get(1).unwrap().as_str(); let field_name = caps.get(2).unwrap().as_str(); faceted_attributes.get(field_name).with_context(|| format!("Can't use {:?} as a criterion as it isn't a faceted field.", field_name))?;