Skip to content

Commit e19438a

Browse files
committed
perf: Use lazy_static with regex
1 parent 25d43ee commit e19438a

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

src/cli/self_update.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
use crate::common::{self, Confirm};
3434
use crate::errors::*;
3535
use crate::term2;
36-
use regex::Regex;
3736
use rustup::dist::dist;
3837
use rustup::utils::utils;
3938
use rustup::{DUP_TOOLS, TOOLS};
@@ -1378,8 +1377,14 @@ fn get_new_rustup_version(path: &Path) -> Option<String> {
13781377
}
13791378

13801379
fn parse_new_rustup_version(version: String) -> String {
1381-
let re = Regex::new(r"\d+.\d+.\d+[0-9a-zA-Z-]*").unwrap();
1382-
let capture = re.captures(&version);
1380+
use lazy_static::lazy_static;
1381+
use regex::Regex;
1382+
1383+
lazy_static! {
1384+
static ref RE: Regex = Regex::new(r"\d+.\d+.\d+[0-9a-zA-Z-]*").unwrap();
1385+
}
1386+
1387+
let capture = RE.captures(&version);
13831388
let matched_version = match capture {
13841389
Some(cap) => cap.get(0).unwrap().as_str(),
13851390
None => "(unknown)",

src/dist/dist.rs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ use crate::dist::temp;
88
use crate::errors::*;
99
use crate::utils::utils;
1010

11+
use lazy_static::lazy_static;
12+
use regex::Regex;
13+
1114
use std::env;
1215
use std::fmt;
1316
use std::path::Path;
1417

15-
use regex::Regex;
16-
1718
pub const DEFAULT_DIST_SERVER: &str = "https://static.rust-lang.org";
1819

1920
// Deprecated
@@ -223,15 +224,17 @@ impl PartialTargetTriple {
223224
// we can count on all triple components being
224225
// delineated by it.
225226
let name = format!("-{}", name);
226-
let pattern = format!(
227-
r"^(?:-({}))?(?:-({}))?(?:-({}))?$",
228-
LIST_ARCHS.join("|"),
229-
LIST_OSES.join("|"),
230-
LIST_ENVS.join("|")
231-
);
232-
233-
let re = Regex::new(&pattern).unwrap();
234-
re.captures(&name).map(|c| {
227+
lazy_static! {
228+
/// This is an example for using doc comment attributes
229+
static ref PATTERN: String = format!(
230+
r"^(?:-({}))?(?:-({}))?(?:-({}))?$",
231+
LIST_ARCHS.join("|"),
232+
LIST_OSES.join("|"),
233+
LIST_ENVS.join("|")
234+
);
235+
static ref RE: Regex = Regex::new(&PATTERN).unwrap();
236+
}
237+
RE.captures(&name).map(|c| {
235238
fn fn_map(s: &str) -> Option<String> {
236239
if s == "" {
237240
None
@@ -251,21 +254,22 @@ impl PartialTargetTriple {
251254

252255
impl PartialToolchainDesc {
253256
pub fn from_str(name: &str) -> Result<Self> {
254-
let channels = [
257+
const CHANNELS: &[&str] = &[
255258
"nightly",
256259
"beta",
257260
"stable",
258261
r"\d{1}\.\d{1}\.\d{1}",
259262
r"\d{1}\.\d{2}\.\d{1}",
260263
];
261264

262-
let pattern = format!(
263-
r"^({})(?:-(\d{{4}}-\d{{2}}-\d{{2}}))?(?:-(.*))?$",
264-
channels.join("|")
265-
);
266-
267-
let re = Regex::new(&pattern).unwrap();
268-
let d = re.captures(name).map(|c| {
265+
lazy_static! {
266+
static ref PATTERN: String = format!(
267+
r"^({})(?:-(\d{{4}}-\d{{2}}-\d{{2}}))?(?:-(.*))?$",
268+
CHANNELS.join("|")
269+
);
270+
static ref RE: Regex = Regex::new(&PATTERN).unwrap();
271+
}
272+
let d = RE.captures(name).map(|c| {
269273
fn fn_map(s: &str) -> Option<String> {
270274
if s == "" {
271275
None
@@ -341,21 +345,23 @@ impl PartialToolchainDesc {
341345

342346
impl ToolchainDesc {
343347
pub fn from_str(name: &str) -> Result<Self> {
344-
let channels = [
348+
const CHANNELS: &[&str] = &[
345349
"nightly",
346350
"beta",
347351
"stable",
348352
r"\d{1}\.\d{1}\.\d{1}",
349353
r"\d{1}\.\d{2}\.\d{1}",
350354
];
351355

352-
let pattern = format!(
353-
r"^({})(?:-(\d{{4}}-\d{{2}}-\d{{2}}))?-(.*)?$",
354-
channels.join("|"),
355-
);
356+
lazy_static! {
357+
static ref PATTERN: String = format!(
358+
r"^({})(?:-(\d{{4}}-\d{{2}}-\d{{2}}))?-(.*)?$",
359+
CHANNELS.join("|"),
360+
);
361+
static ref RE: Regex = Regex::new(&PATTERN).unwrap();
362+
}
356363

357-
let re = Regex::new(&pattern).unwrap();
358-
re.captures(name)
364+
RE.captures(name)
359365
.map(|c| {
360366
fn fn_map(s: &str) -> Option<String> {
361367
if s == "" {

0 commit comments

Comments
 (0)