perf(syntax): use NonZeroU32 for SymbolId and ReferenceId (#3970)

closes https://github.com/oxc-project/backlog/issues/55
closes https://github.com/oxc-project/oxc/issues/3318
This commit is contained in:
Boshen 2024-06-29 17:29:02 +08:00 committed by GitHub
parent 21b964b214
commit 0c81fbeac6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 6 deletions

View file

@ -1,10 +1,28 @@
use std::num::NonZeroU32;
use bitflags::bitflags;
use oxc_index::define_index_type;
#[cfg(feature = "serialize")]
use serde::Serialize;
define_index_type! {
pub struct ReferenceId = u32;
use oxc_index::Idx;
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
pub struct ReferenceId(NonZeroU32);
impl Idx for ReferenceId {
#[allow(clippy::cast_possible_truncation)]
fn from_usize(idx: usize) -> Self {
// SAFETY: + 1 is always non-zero.
#[allow(unsafe_code)]
unsafe {
Self(NonZeroU32::new_unchecked(idx as u32 + 1))
}
}
fn index(self) -> usize {
self.0.get() as usize - 1
}
}
#[cfg(feature = "serialize")]

View file

@ -1,10 +1,28 @@
use std::num::NonZeroU32;
use bitflags::bitflags;
use oxc_index::define_index_type;
#[cfg(feature = "serialize")]
use serde::Serialize;
define_index_type! {
pub struct SymbolId = u32;
use oxc_index::Idx;
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
pub struct SymbolId(NonZeroU32);
impl Idx for SymbolId {
#[allow(clippy::cast_possible_truncation)]
fn from_usize(idx: usize) -> Self {
// SAFETY: + 1 is always non-zero.
#[allow(unsafe_code)]
unsafe {
Self(NonZeroU32::new_unchecked(idx as u32 + 1))
}
}
fn index(self) -> usize {
self.0.get() as usize - 1
}
}
#[cfg(feature = "serialize")]