-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…#9826) **Description**: `FastJsWord` and `FastId` implement `Copy` and do not need to be cloned or dropped, unlike `JsWord` or `Id`. Of course, they are inherently unsafe. But I found that I could use it for the minifier. **Related issue:** - Extracted from #9813
- Loading branch information
Showing
6 changed files
with
119 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
swc_atoms: patch | ||
swc_core: patch | ||
swc_ecma_ast: patch | ||
swc_ecma_utils: patch | ||
--- | ||
|
||
perf(es/minifier): Introduce `FastJsWord` and `FastId` in `swc_atoms` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::{ | ||
mem::{transmute_copy, ManuallyDrop}, | ||
ops::Deref, | ||
}; | ||
|
||
use crate::Atom; | ||
|
||
/// FastAtom is a wrapper around [Atom] that does not allocate, but extremely | ||
/// unsafe. | ||
/// | ||
/// Do not use this unless you know what you are doing. | ||
/// | ||
/// **Currently, it's considered as a unstable API and may be changed in the | ||
/// future without a semver bump.** | ||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
pub struct FastAtom(ManuallyDrop<Atom>); | ||
|
||
impl FastAtom { | ||
/// # Safety | ||
/// | ||
/// - You should ensure that the passed `atom` is not freed. | ||
/// | ||
/// Some simple solutions to ensure this are | ||
/// | ||
/// - Collect all [Atom] and store them somewhere while you are using | ||
/// [FastAtom] | ||
/// - Use [FastAtom] only for short-lived operations where all [Atom] is | ||
/// stored in AST and ensure that the AST is not dropped. | ||
#[inline] | ||
pub unsafe fn new(atom: &Atom) -> Self { | ||
Self(ManuallyDrop::new(transmute_copy(atom))) | ||
} | ||
} | ||
|
||
impl Deref for FastAtom { | ||
type Target = Atom; | ||
|
||
#[inline] | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl Clone for FastAtom { | ||
#[inline] | ||
fn clone(&self) -> Self { | ||
unsafe { Self::new(&self.0) } | ||
} | ||
} | ||
|
||
impl PartialEq<Atom> for FastAtom { | ||
#[inline] | ||
fn eq(&self, other: &Atom) -> bool { | ||
*self.0 == *other | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters