From 226101fe38e2a931f1fb5fad53ef153afd52ecd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Mon, 3 Jun 2024 22:20:53 +0900 Subject: [PATCH 1/2] feat: Update `swc_core` to `v0.92.8` (#8272) ### Description Update swc_core ### Testing Instructions See [next.js counterpart](https://github.com/vercel/next.js/pull/66479) --- - Closes https://github.com/vercel/next.js/issues/66378 - Closes https://github.com/vercel/next.js/issues/66237 - Closes https://github.com/vercel/next.js/issues/65763 --- Cargo.lock | 44 ++++++++++++++++++++++---------------------- Cargo.toml | 4 ++-- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3fce1df8cea5e..0ccb39cc9a50d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3494,7 +3494,7 @@ dependencies = [ "httpdate", "itoa", "pin-project-lite", - "socket2 0.4.9", + "socket2 0.5.4", "tokio", "tower-service", "tracing", @@ -7576,9 +7576,9 @@ dependencies = [ [[package]] name = "styled_jsx" -version = "0.73.21" +version = "0.73.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17e38b2334f6613c9e811cc776bc9d2329c288f4dc125df68615fe6cf19b48ae" +checksum = "c05e5aea6dec71fcebfe01fc22139bda1a9a31feff17c219b1e2cee6ce815060" dependencies = [ "anyhow", "lightningcss", @@ -7888,9 +7888,9 @@ dependencies = [ [[package]] name = "swc_core" -version = "0.92.5" +version = "0.92.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e317f6f8b15019358d1e48631c0e6d098d9a3d00d666ea99650201661abea855" +checksum = "47a2aad0d9bede023181f50d63de80f20634f0c612a883cbb3f27f99da3d8d3d" dependencies = [ "binding_macros", "swc", @@ -8077,9 +8077,9 @@ dependencies = [ [[package]] name = "swc_ecma_ast" -version = "0.113.4" +version = "0.113.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc1690cc0c9ab60b44ac0225ba1e231ac532f7ba1d754df761c6ee607561afae" +checksum = "0fa77fca9412729a3fe634e55c354f6c70c7983f127411ce8684e4c7edf32ed3" dependencies = [ "bitflags 2.5.0", "bytecheck", @@ -8097,9 +8097,9 @@ dependencies = [ [[package]] name = "swc_ecma_codegen" -version = "0.149.1" +version = "0.149.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fef147127a2926ca26171c7afcbf028ff86dc543ced87d316713f25620a15b9" +checksum = "6ab6d5e7bbd9208f980b5dad2a4a6ae798c97569f809a48c3f92e6ae7e183c6c" dependencies = [ "memchr", "num-bigint", @@ -8158,9 +8158,9 @@ dependencies = [ [[package]] name = "swc_ecma_compat_es2015" -version = "0.5.1" +version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "248532f9ae603be6bf4763f66f74ad0dfd82d6307be876ccf4c5d081826a1161" +checksum = "6f0d3d5d4637af5195265444b2a708020ee90973008ec50c665dad83dd5f1c70" dependencies = [ "arrayvec 0.7.4", "indexmap 2.2.6", @@ -8322,9 +8322,9 @@ dependencies = [ [[package]] name = "swc_ecma_ext_transforms" -version = "0.114.1" +version = "0.114.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "259b7b69630aafde63c6304eeacb93fd54619cbdb199c978549acc76cd512d76" +checksum = "91b55ddf8b600f07d0086a9a782d55aa048d3c1ac5eabaa27733d9f45d960e52" dependencies = [ "phf 0.11.2", "swc_atoms", @@ -8378,9 +8378,9 @@ dependencies = [ [[package]] name = "swc_ecma_minifier" -version = "0.194.5" +version = "0.194.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "535bbb8adbdf730302f477948557a26e5cd73854d4543c0630e05132ffa16d8a" +checksum = "a5a94b50edef2186a16f283d55b9e5a1c05df9250446b1bf8686d69b996e231d" dependencies = [ "arrayvec 0.7.4", "indexmap 2.2.6", @@ -8736,9 +8736,9 @@ dependencies = [ [[package]] name = "swc_ecma_usage_analyzer" -version = "0.24.2" +version = "0.24.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d140be135c1af1726ee02406ad210c6598b3399303974d884b1b681563602c9" +checksum = "6d7d7109b3794756cc51e842dbb874d2da44293b06a9e3837b477300b0ccef8e" dependencies = [ "indexmap 2.2.6", "rustc-hash", @@ -8753,9 +8753,9 @@ dependencies = [ [[package]] name = "swc_ecma_utils" -version = "0.128.1" +version = "0.128.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe5242670bc74e0a0b64b9d4912b37be36944517ce0881314162aeb4381272c3" +checksum = "2f978f911664091d2d161ed7fedfb2128f6a8af77765a264b470195d6e20b768" dependencies = [ "indexmap 2.2.6", "num_cpus", @@ -8931,9 +8931,9 @@ dependencies = [ [[package]] name = "swc_plugin_runner" -version = "0.107.1" +version = "0.107.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73640537e0967a88a537c853de4a41ba6cdf77bfff1999f7c6c449e5bc550eed" +checksum = "0cc31ec32964d3ebaebfd5a2466a2aaa909aa00722d677f89994b2b6c27d105c" dependencies = [ "anyhow", "enumset", @@ -11445,7 +11445,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ "cfg-if", - "rand 0.8.5", + "rand 0.4.6", "static_assertions", ] diff --git a/Cargo.toml b/Cargo.toml index c86c67451d2cf..ea1691852e26c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -111,8 +111,8 @@ miette = { version = "5.10.0", features = ["fancy"] } mdxjs = "0.2.2" modularize_imports = { version = "0.68.14" } styled_components = { version = "0.96.15" } -styled_jsx = { version = "0.73.21" } -swc_core = { version = "0.92.5", features = [ +styled_jsx = { version = "0.73.22" } +swc_core = { version = "0.92.8", features = [ "ecma_loader_lru", "ecma_loader_parking_lot", ] } From 13aba96f9611839e87883257adc6fa03b8cc6c07 Mon Sep 17 00:00:00 2001 From: Alexander Lyon Date: Mon, 3 Jun 2024 16:42:00 +0100 Subject: [PATCH 2/2] add crate to calculate prehashes (#8262) ### Description We have a few cases where certain keys are repeatedly inserted into hashmaps. In these situations we can prehash items. This crate will do that. ### Testing Instructions There is a basic doctest to demonstrate the functionality. The code is pretty straight forward. --- Cargo.lock | 4 + Cargo.toml | 2 + crates/turbo-prehash/Cargo.toml | 10 +++ crates/turbo-prehash/src/lib.rs | 145 ++++++++++++++++++++++++++++++++ 4 files changed, 161 insertions(+) create mode 100644 crates/turbo-prehash/Cargo.toml create mode 100644 crates/turbo-prehash/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 0ccb39cc9a50d..4a28cb57e06eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10038,6 +10038,10 @@ dependencies = [ "winapi", ] +[[package]] +name = "turbo-prehash" +version = "0.1.0" + [[package]] name = "turbo-tasks" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index ea1691852e26c..433fe21737919 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ members = [ "crates/node-file-trace", "crates/tower-uds", "crates/turbo-tasks*", + "crates/turbo-prehash", "crates/turbopack*", "crates/turborepo*", "packages/turbo-repository/rust", @@ -128,6 +129,7 @@ signposter = { path = "crates/turbo-tasks-signposter" } signposter-sys = { path = "crates/turbo-tasks-signposter-sys" } tracing-signpost = { path = "crates/turbo-tasks-tracing-signpost" } swc-ast-explorer = { path = "crates/turbopack-swc-ast-explorer" } +turbo-prehash = { path = "crates/turbo-prehash" } turbo-tasks-malloc = { path = "crates/turbo-tasks-malloc", default-features = false } turbo-tasks = { path = "crates/turbo-tasks" } turbo-tasks-build = { path = "crates/turbo-tasks-build" } diff --git a/crates/turbo-prehash/Cargo.toml b/crates/turbo-prehash/Cargo.toml new file mode 100644 index 0000000000000..e2f6856763c65 --- /dev/null +++ b/crates/turbo-prehash/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "turbo-prehash" +version = "0.1.0" +edition = "2021" +license = "MPL-2.0" + +[dependencies] + +[lints] +workspace = true diff --git a/crates/turbo-prehash/src/lib.rs b/crates/turbo-prehash/src/lib.rs new file mode 100644 index 0000000000000..bf187fd72602c --- /dev/null +++ b/crates/turbo-prehash/src/lib.rs @@ -0,0 +1,145 @@ +//! turbo-prehash +//! +//! A small wrapper around `std::hash::Hasher` that allows you to pre-hash a +//! value before hashing it. +//! +//! This is useful for when you want to hash a value that is expensive to +//! compute (e.g. a large string) but you want to avoid re-hashing it every +//! time. +//! +//! # Example +//! +//! ``` +//! use turbo_prehash::{BuildHasherExt, PreHashed}; +//! use std::collections::HashMap; +//! use std::hash::{BuildHasherDefault, RandomState, Hash}; +//! +//! /// hash a key, returning a prehashed value +//! fn hash_key(key: T) -> PreHashed { +//! RandomState::new().prehash(key) +//! } +//! +//! // create hashmap to hold pre-hashed values +//! let mut map: HashMap, String> = Default::default(); +//! +//! // insert a prehashed value +//! let hashed_key = hash_key("hello".to_string()); +//! map.insert(hashed_key.clone(), "world".to_string()); +//! +//! // get the value +//! assert_eq!(map.get(&hashed_key), Some(&"world".to_string())); +//! ``` + +use std::{ + fmt, + hash::{BuildHasher, Hash, Hasher}, + ops::Deref, +}; + +/// A wrapper type that hashes some `inner` on creation, implementing [Hash] +/// by simply returning the pre-computed hash. +#[derive(Copy, Debug, Clone)] +pub struct PreHashed { + hash: H, + inner: I, +} + +impl PreHashed { + /// Create a new [PreHashed] value with the given hash and inner value. + /// + /// SAFETY: The hash must be a valid hash of the inner value. + pub fn new(hash: H, inner: I) -> Self { + Self { hash, inner } + } + + /// Split the [PreHashed] value into its hash and inner value. + pub fn into_parts(self) -> (H, I) { + (self.hash, self.inner) + } + + fn inner(&self) -> &I { + &self.inner + } +} + +impl PreHashed { + /// Create a new [PreHashed] value from a [BuildHasher]. + fn new_from_builder(hasher: &B, inner: I) -> Self { + Self::new(hasher.hash_one(&inner), inner) + } +} + +impl Deref for PreHashed { + type Target = I; + + fn deref(&self) -> &Self::Target { + self.inner() + } +} + +impl AsRef for PreHashed { + fn as_ref(&self) -> &I { + self.inner() + } +} + +impl Hash for PreHashed { + fn hash(&self, state: &mut S) { + self.hash.hash(state) + } +} + +impl Eq for PreHashed {} + +impl PartialEq for PreHashed { + // note: we compare the values, not the hashes + fn eq(&self, other: &Self) -> bool { + self.inner.eq(&other.inner) + } +} + +impl fmt::Display for PreHashed { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.inner.fmt(f) + } +} + +/// An implementer of [Hash] that simply returns the pre-computed hash. +#[derive(Copy, Clone, Debug, Default)] +pub struct PassThroughHash(u64); + +impl PassThroughHash { + pub fn new() -> Self { + Default::default() + } +} + +impl Hasher for PassThroughHash { + fn write(&mut self, _bytes: &[u8]) { + unimplemented!("do not use") + } + + fn write_u64(&mut self, i: u64) { + self.0 = i; + } + + fn finish(&self) -> u64 { + self.0 + } +} + +/// An extension trait for [BuildHasher] that provides the +/// [BuildHasherExt::prehash] method. +pub trait BuildHasherExt: BuildHasher { + type Hash; + + fn prehash(&self, value: T) -> PreHashed; +} + +impl BuildHasherExt for B { + type Hash = u64; + + fn prehash(&self, value: T) -> PreHashed { + PreHashed::new_from_builder(self, value) + } +}