perf(es/parser): Use Cow for _ in numeric literals (#7031)

This commit is contained in:
Donny/강동윤 2023-03-08 13:54:28 +09:00 committed by GitHub
parent 56aac6783b
commit 30546a28f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 10 deletions

12
Cargo.lock generated
View File

@ -2902,6 +2902,17 @@ version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83"
[[package]]
name = "smartstring"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fb72c633efbaa2dd666986505016c32c3044395ceaf881518399d2f4127ee29"
dependencies = [
"autocfg",
"static_assertions",
"version_check",
]
[[package]]
name = "smawk"
version = "0.3.1"
@ -3723,6 +3734,7 @@ dependencies = [
"serde",
"serde_json",
"smallvec",
"smartstring",
"stacker",
"swc_atoms",
"swc_common",

View File

@ -30,6 +30,7 @@ lexical = { version = "6.1.0", features = ["power-of-two"] }
num-bigint = "0.4"
serde = { version = "1", features = ["derive"] }
smallvec = "1.8.0"
smartstring = "1"
swc_atoms = { version = "0.4.39", path = "../swc_atoms" }
swc_common = { version = "0.29.35", path = "../swc_common" }
swc_ecma_ast = { version = "0.98.2", path = "../swc_ecma_ast" }

View File

@ -2,7 +2,7 @@
//!
//!
//! See https://tc39.github.io/ecma262/#sec-literals-numeric-literals
use std::fmt::Write;
use std::{borrow::Cow, fmt::Write};
use either::Either;
use num_bigint::BigInt as BigIntValue;
@ -153,11 +153,14 @@ impl<'a> Lexer<'a> {
raw_val.push_str(raw.0.as_ref().unwrap());
}
raw_val
// Remove number separator from number
.replace('_', "")
.parse()
.expect("failed to parse float using rust's impl")
// Remove number separator from number
if raw_val.contains('_') {
Cow::Owned(raw_val.replace('_', ""))
} else {
Cow::Borrowed(&raw_val)
}
.parse()
.expect("failed to parse float using rust's impl")
};
}
@ -210,10 +213,13 @@ impl<'a> Lexer<'a> {
write!(raw_val, "{}", exp).unwrap();
raw_val
.replace('_', "")
.parse()
.expect("failed to parse float literal")
if raw_val.contains('_') {
Cow::Owned(raw_val.replace('_', ""))
} else {
Cow::Borrowed(&raw_val)
}
.parse()
.expect("failed to parse float literal")
}
}
_ => {}