perf(es): Use Mutex from parking_lot (#3830)

This commit is contained in:
Donny/강동윤 2022-03-03 23:26:49 +09:00 committed by GitHub
parent c16bb588de
commit 351b814ed0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 25 additions and 26 deletions

3
Cargo.lock generated
View File

@ -2709,6 +2709,7 @@ dependencies = [
"napi",
"napi-derive",
"once_cell",
"parking_lot 0.12.0",
"pathdiff",
"rayon",
"regex",
@ -3065,6 +3066,7 @@ dependencies = [
"lru",
"normpath",
"once_cell",
"parking_lot 0.12.0",
"path-clean",
"regex",
"serde",
@ -3083,6 +3085,7 @@ dependencies = [
"backtrace",
"indexmap",
"once_cell",
"parking_lot 0.12.0",
"pretty_assertions",
"rayon",
"regex",

View File

@ -46,12 +46,12 @@ serde = {version = "1", features = ["derive"]}
serde_json = "1"
sourcemap = "6"
swc_atoms = {version = "0.2", path = "../swc_atoms"}
swc_common = {version = "0.17.0", path = "../swc_common", features = ["sourcemap", "concurrent"]}
swc_common = {version = "0.17.0", path = "../swc_common", features = ["sourcemap", "concurrent", "parking_lot"]}
swc_ecma_ast = {version = "0.68.0", path = "../swc_ecma_ast"}
swc_ecma_codegen = {version = "0.93.0", path = "../swc_ecma_codegen"}
swc_ecma_ext_transforms = {version = "0.55.0", path = "../swc_ecma_ext_transforms"}
swc_ecma_lints = {version = "0.19.0", path = "../swc_ecma_lints"}
swc_ecma_loader = {version = "0.28.0", path = "../swc_ecma_loader", features = ["lru", "node", "tsc"]}
swc_ecma_loader = {version = "0.28.0", path = "../swc_ecma_loader", features = ["cache", "node", "tsc"]}
swc_ecma_minifier = {version = "0.81.0", path = "../swc_ecma_minifier"}
swc_ecma_parser = {version = "0.91.0", path = "../swc_ecma_parser"}
swc_ecma_preset_env = {version = "0.98.0", path = "../swc_ecma_preset_env"}
@ -73,6 +73,7 @@ swc_node_comments = {version = "0.4.0", path = "../swc_node_comments"}
swc_plugin_runner = {version = "0.38.0", path = "../swc_plugin_runner", optional = true}
swc_visit = {version = "0.3.0", path = "../swc_visit"}
tracing = "0.1.31"
parking_lot = "0.12.0"
[dependencies.napi-derive]
default-features = false

View File

@ -116,7 +116,7 @@ use std::{
io::Write,
mem::take,
path::{Path, PathBuf},
sync::{Arc, Mutex},
sync::Arc,
};
use anyhow::{bail, Context, Error};
@ -127,6 +127,7 @@ use common::{
};
use config::{util::BoolOrObject, IsModule, JsMinifyCommentOption, JsMinifyOptions};
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use serde::Serialize;
use serde_json::error::Category;
pub use sourcemap;
@ -212,10 +213,7 @@ struct LockedWriter(Arc<Mutex<Vec<u8>>>);
impl Write for LockedWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let mut lock = self
.0
.lock()
.expect("failed to get lock while trying to report error");
let mut lock = self.0.lock();
lock.extend_from_slice(buf);
@ -246,9 +244,7 @@ where
let ret = HANDLER.set(&handler, || op(&handler));
if handler.has_errors() {
let mut lock =
wr.0.lock()
.expect("reference to handler should not exist in this point");
let mut lock = wr.0.lock();
let error = take(&mut *lock);
let msg = String::from_utf8(error).expect("error string should be utf8");

View File

@ -60,7 +60,7 @@ ntest = "0.7.2"
path-clean = "=0.1.0"
reqwest = {version = "0.11.4", features = ["blocking"]}
sha-1 = "0.9"
swc_ecma_loader = {version = "0.28.0", path = "../swc_ecma_loader", features = ["node", "lru"]}
swc_ecma_loader = {version = "0.28.0", path = "../swc_ecma_loader", features = ["node", "cache"]}
swc_ecma_minifier = {version = "0.81.0", path = "../swc_ecma_minifier"}
swc_ecma_transforms_react = {version = "0.86.0", path = "../swc_ecma_transforms_react"}
swc_ecma_transforms_typescript = {version = "0.89.0", path = "../swc_ecma_transforms_typescript"}

View File

@ -15,6 +15,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[features]
default = []
cache = ["lru", "parking_lot"]
# Enable node js resolver
node = ["normpath", "serde_json", "dashmap", "once_cell", "path-clean"]
# Enable support for `paths` of tsconfig.json
@ -26,6 +27,7 @@ anyhow = "1.0.41"
dashmap = {version = "4.0.2", optional = true}
lru = {version = "0.7.1", optional = true}
once_cell = {version = "1.9.0", optional = true}
parking_lot = {version = "0.12.0", optional = true}
path-clean = {version = "=0.1.0", optional = true}
regex = {version = "1", optional = true}
serde = {version = "1", features = ["derive"]}

View File

@ -1,7 +1,6 @@
use std::sync::Mutex;
use anyhow::Error;
use lru::LruCache;
use parking_lot::Mutex;
use swc_common::FileName;
use crate::resolve::Resolve;
@ -42,21 +41,17 @@ where
{
fn resolve(&self, base: &FileName, src: &str) -> Result<FileName, Error> {
{
let lock = self.cache.lock();
if let Ok(mut lock) = lock {
//
if let Some(v) = lock.get(&(base.clone(), src.to_string())) {
return Ok(v.clone());
}
let mut lock = self.cache.lock();
//
if let Some(v) = lock.get(&(base.clone(), src.to_string())) {
return Ok(v.clone());
}
}
let resolved = self.inner.resolve(base, src)?;
{
let lock = self.cache.lock();
if let Ok(mut lock) = lock {
lock.put((base.clone(), src.to_string()), resolved.clone());
}
let mut lock = self.cache.lock();
lock.put((base.clone(), src.to_string()), resolved.clone());
}
Ok(resolved)

View File

@ -21,6 +21,7 @@ ahash = "0.7.6"
backtrace = {version = "0.3.61", optional = true}
indexmap = "1.7.0"
once_cell = "1.9.0"
parking_lot = "0.12.0"
pretty_assertions = {version = "0.7.2", optional = true}
rayon = "1.5.1"
regex = "1.5.3"

View File

@ -1,5 +1,6 @@
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use parking_lot::Mutex;
use swc_atoms::js_word;
use swc_common::{collections::AHashMap, util::take::Take, DUMMY_SP};
use swc_ecma_ast::*;
@ -50,7 +51,7 @@ pub enum EvalResult {
impl Mode for Eval {
fn store(&self, id: Id, value: &Expr) {
let mut w = self.store.lock().unwrap();
let mut w = self.store.lock();
w.cache.insert(id, Box::new(value.clone()));
}
@ -173,7 +174,7 @@ impl Evaluator {
Expr::Ident(i) => {
self.run();
let lock = self.data.store.lock().ok()?;
let lock = self.data.store.lock();
let val = lock.cache.get(&i.to_id())?;
return Some(val.clone());