mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 20:32:10 +00:00
refactor: replace InlinableString with CompactString for Atom (#2517)
relates #2516
This commit is contained in:
parent
1519b9000b
commit
c56b6cb643
4 changed files with 15 additions and 55 deletions
10
Cargo.lock
generated
10
Cargo.lock
generated
|
|
@ -821,15 +821,6 @@ dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "inlinable_string"
|
|
||||||
version = "0.1.15"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "c8fae54786f62fb2918dcfae3d568594e50eb9b5c25bf04371af6fe7516452fb"
|
|
||||||
dependencies = [
|
|
||||||
"serde",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "insta"
|
name = "insta"
|
||||||
version = "1.35.1"
|
version = "1.35.1"
|
||||||
|
|
@ -1669,7 +1660,6 @@ name = "oxc_span"
|
||||||
version = "0.8.0"
|
version = "0.8.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"compact_str",
|
"compact_str",
|
||||||
"inlinable_string",
|
|
||||||
"miette",
|
"miette",
|
||||||
"serde",
|
"serde",
|
||||||
"tsify",
|
"tsify",
|
||||||
|
|
|
||||||
|
|
@ -345,7 +345,7 @@ impl<'a> Binder for TSEnumMember<'a> {
|
||||||
let name = match &self.id {
|
let name = match &self.id {
|
||||||
TSEnumMemberName::Identifier(id) => id.name.clone(),
|
TSEnumMemberName::Identifier(id) => id.name.clone(),
|
||||||
TSEnumMemberName::StringLiteral(s) => s.value.clone(),
|
TSEnumMemberName::StringLiteral(s) => s.value.clone(),
|
||||||
TSEnumMemberName::NumericLiteral(n) => Atom::new_inline(&n.value.to_string()),
|
TSEnumMemberName::NumericLiteral(n) => Atom::from(n.value.to_string()),
|
||||||
TSEnumMemberName::ComputedPropertyName(_) => panic!("TODO: implement"),
|
TSEnumMemberName::ComputedPropertyName(_) => panic!("TODO: implement"),
|
||||||
};
|
};
|
||||||
builder.declare_symbol(
|
builder.declare_symbol(
|
||||||
|
|
|
||||||
|
|
@ -20,8 +20,7 @@ doctest = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
miette = { workspace = true }
|
miette = { workspace = true }
|
||||||
inlinable_string = "0.1.15"
|
compact_str = { version = "0.7.1" }
|
||||||
compact_str = { version = "0.7.1", features = ["serde"] }
|
|
||||||
|
|
||||||
tsify = { workspace = true, optional = true }
|
tsify = { workspace = true, optional = true }
|
||||||
wasm-bindgen = { workspace = true, optional = true }
|
wasm-bindgen = { workspace = true, optional = true }
|
||||||
|
|
@ -30,5 +29,5 @@ serde = { workspace = true, features = ["derive"], optional = true }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
serde = ["dep:serde", "inlinable_string/serde"]
|
serde = ["dep:serde", "compact_str/serde"]
|
||||||
wasm = ["dep:tsify", "dep:wasm-bindgen"]
|
wasm = ["dep:tsify", "dep:wasm-bindgen"]
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ use std::{
|
||||||
use serde::{Serialize, Serializer};
|
use serde::{Serialize, Serializer};
|
||||||
|
|
||||||
use compact_str::CompactString;
|
use compact_str::CompactString;
|
||||||
use inlinable_string::inline_string::{InlineString, INLINE_STRING_CAPACITY};
|
|
||||||
|
|
||||||
const BASE54_CHARS: &[u8; 64] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_0123456789";
|
const BASE54_CHARS: &[u8; 64] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_0123456789";
|
||||||
|
|
||||||
|
|
@ -29,16 +28,10 @@ export type CompactString = string;
|
||||||
pub struct Atom<'a>(AtomImpl<'a>);
|
pub struct Atom<'a>(AtomImpl<'a>);
|
||||||
|
|
||||||
/// Immutable Inlinable String
|
/// Immutable Inlinable String
|
||||||
///
|
|
||||||
/// https://github.com/fitzgen/inlinable_string/blob/master/src/lib.rs
|
|
||||||
#[derive(Clone, Eq, PartialEq)]
|
#[derive(Clone, Eq, PartialEq)]
|
||||||
enum AtomImpl<'a> {
|
enum AtomImpl<'a> {
|
||||||
/// A arena heap-allocated string.
|
|
||||||
Arena(&'a str),
|
Arena(&'a str),
|
||||||
/// A heap-allocated string.
|
Compact(CompactString),
|
||||||
Heap(Box<str>),
|
|
||||||
/// A small string stored inline.
|
|
||||||
Inline(InlineString),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
|
|
@ -52,16 +45,11 @@ impl<'a> Serialize for Atom<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Atom<'a> {
|
impl<'a> Atom<'a> {
|
||||||
pub fn new_inline(s: &str) -> Self {
|
|
||||||
Self(AtomImpl::Inline(InlineString::from(s)))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn as_str(&self) -> &str {
|
pub fn as_str(&self) -> &str {
|
||||||
match &self.0 {
|
match &self.0 {
|
||||||
AtomImpl::Arena(s) => s,
|
AtomImpl::Arena(s) => s,
|
||||||
AtomImpl::Heap(s) => s,
|
AtomImpl::Compact(s) => s.as_ref(),
|
||||||
AtomImpl::Inline(s) => s.as_ref(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -69,24 +57,23 @@ impl<'a> Atom<'a> {
|
||||||
pub fn into_string(self) -> String {
|
pub fn into_string(self) -> String {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
AtomImpl::Arena(s) => String::from(s),
|
AtomImpl::Arena(s) => String::from(s),
|
||||||
AtomImpl::Heap(s) => s.to_string(),
|
AtomImpl::Compact(s) => s.to_string(),
|
||||||
AtomImpl::Inline(s) => s.to_string(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn into_compact_string(self) -> CompactString {
|
pub fn into_compact_string(self) -> CompactString {
|
||||||
match self.0 {
|
match self.0 {
|
||||||
AtomImpl::Arena(s) => CompactString::new(s),
|
AtomImpl::Arena(s) => CompactString::new(s),
|
||||||
AtomImpl::Heap(s) => CompactString::new(s),
|
AtomImpl::Compact(s) => s,
|
||||||
AtomImpl::Inline(s) => CompactString::new(s),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
pub fn to_compact_string(&self) -> CompactString {
|
pub fn to_compact_string(&self) -> CompactString {
|
||||||
match &self.0 {
|
match &self.0 {
|
||||||
AtomImpl::Arena(s) => CompactString::new(s),
|
AtomImpl::Arena(s) => CompactString::new(s),
|
||||||
AtomImpl::Heap(s) => CompactString::new(s),
|
AtomImpl::Compact(s) => s.clone(),
|
||||||
AtomImpl::Inline(s) => CompactString::new(s),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -108,37 +95,25 @@ impl<'a> Atom<'a> {
|
||||||
ret.push(BASE54_CHARS[num % base] as char);
|
ret.push(BASE54_CHARS[num % base] as char);
|
||||||
num /= base;
|
num /= base;
|
||||||
}
|
}
|
||||||
Self(AtomImpl::Heap(ret.into_boxed_str()))
|
Self(AtomImpl::Compact(CompactString::new(ret)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a str> for Atom<'a> {
|
impl<'a> From<&'a str> for Atom<'a> {
|
||||||
fn from(s: &'a str) -> Self {
|
fn from(s: &'a str) -> Self {
|
||||||
if s.len() <= INLINE_STRING_CAPACITY {
|
Self(AtomImpl::Arena(s))
|
||||||
Self(AtomImpl::Inline(InlineString::from(s)))
|
|
||||||
} else {
|
|
||||||
Self(AtomImpl::Arena(s))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<String> for Atom<'a> {
|
impl<'a> From<String> for Atom<'a> {
|
||||||
fn from(s: String) -> Self {
|
fn from(s: String) -> Self {
|
||||||
if s.len() <= INLINE_STRING_CAPACITY {
|
Self(AtomImpl::Compact(CompactString::from(s)))
|
||||||
Self(AtomImpl::Inline(InlineString::from(s.as_str())))
|
|
||||||
} else {
|
|
||||||
Self(AtomImpl::Heap(s.into_boxed_str()))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<Cow<'_, str>> for Atom<'a> {
|
impl<'a> From<Cow<'_, str>> for Atom<'a> {
|
||||||
fn from(s: Cow<'_, str>) -> Self {
|
fn from(s: Cow<'_, str>) -> Self {
|
||||||
if s.len() <= INLINE_STRING_CAPACITY {
|
Self(AtomImpl::Compact(CompactString::from(s)))
|
||||||
Self(AtomImpl::Inline(InlineString::from(s.borrow())))
|
|
||||||
} else {
|
|
||||||
Self(AtomImpl::Heap(s.into()))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -151,14 +126,12 @@ impl<'a> Deref for Atom<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> AsRef<str> for Atom<'a> {
|
impl<'a> AsRef<str> for Atom<'a> {
|
||||||
#[inline]
|
|
||||||
fn as_ref(&self) -> &str {
|
fn as_ref(&self) -> &str {
|
||||||
self.as_str()
|
self.as_str()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Borrow<str> for Atom<'a> {
|
impl<'a> Borrow<str> for Atom<'a> {
|
||||||
#[inline]
|
|
||||||
fn borrow(&self) -> &str {
|
fn borrow(&self) -> &str {
|
||||||
self.as_str()
|
self.as_str()
|
||||||
}
|
}
|
||||||
|
|
@ -177,12 +150,10 @@ impl<'a> PartialEq<Atom<'a>> for &str {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> hash::Hash for AtomImpl<'a> {
|
impl<'a> hash::Hash for AtomImpl<'a> {
|
||||||
#[inline]
|
|
||||||
fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
|
fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
|
||||||
match self {
|
match self {
|
||||||
Self::Arena(s) => s.hash(hasher),
|
Self::Arena(s) => s.hash(hasher),
|
||||||
Self::Heap(s) => s.hash(hasher),
|
Self::Compact(s) => s.hash(hasher),
|
||||||
Self::Inline(s) => s.hash(hasher),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue