From e8649f16bbee12c122f128e2168e7314d86647cc Mon Sep 17 00:00:00 2001 From: Boshen Date: Sun, 28 Sep 2025 16:31:44 +0800 Subject: [PATCH 1/2] perf: test oxc-index-vec change --- Cargo.lock | 3 +-- Cargo.toml | 2 +- crates/oxc_cfg/src/lib.rs | 8 ++++++++ crates/oxc_codegen/src/sourcemap_builder.rs | 8 ++++++++ .../src/rules/eslint/no_useless_backreference.rs | 8 ++++++++ crates/oxc_syntax/src/comment_node.rs | 6 ++++++ crates/oxc_syntax/src/node.rs | 8 ++++++++ crates/oxc_syntax/src/reference.rs | 8 ++++++++ crates/oxc_syntax/src/scope.rs | 8 ++++++++ crates/oxc_syntax/src/symbol.rs | 16 ++++++++++++++++ 10 files changed, 72 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ddc0cac43de85..b21e6bc3092a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1974,8 +1974,7 @@ dependencies = [ [[package]] name = "oxc_index" version = "3.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "967ae797e1f284bd1385f2d8e8ab94293ad27f623c76839ecf66827521365f5b" +source = "git+https://github.com/oxc-project/oxc-index-vec.git?branch=reduce-bounds-checks#e2d8098ce9a3d57ab649e4d7e9df2bfa20ac76a3" dependencies = [ "serde", ] diff --git a/Cargo.toml b/Cargo.toml index e5d93b8296151..d6fc0271e9acc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -158,7 +158,7 @@ unicode-id-start = "1" javascript-globals = "1" json-strip-comments = "3" oxc-browserslist = "2" -oxc_index = "3" +oxc_index = {git="https://github.com/oxc-project/oxc-index-vec.git",branch="reduce-bounds-checks"} oxc_resolver = "11" oxc_sourcemap = "4" diff --git a/crates/oxc_cfg/src/lib.rs b/crates/oxc_cfg/src/lib.rs index e7aac446d41c1..a668a1c7fa023 100644 --- a/crates/oxc_cfg/src/lib.rs +++ b/crates/oxc_cfg/src/lib.rs @@ -32,6 +32,8 @@ pub type BlockNodeId = petgraph::stable_graph::NodeIndex; pub struct BasicBlockId(NonMaxU32); impl Idx for BasicBlockId { + const MAX: usize = (u32::MAX - 1) as usize; + #[expect(clippy::cast_possible_truncation)] fn from_usize(idx: usize) -> Self { assert!(idx < u32::MAX as usize); @@ -39,6 +41,12 @@ impl Idx for BasicBlockId { Self(unsafe { NonMaxU32::new_unchecked(idx as u32) }) } + #[expect(clippy::cast_possible_truncation)] + unsafe fn from_usize_unchecked(idx: usize) -> Self { + // SAFETY: Caller must ensure `idx` is a legal value for `NonMaxU32` + Self(unsafe { NonMaxU32::new_unchecked(idx as u32) }) + } + fn index(self) -> usize { self.0.get() as usize } diff --git a/crates/oxc_codegen/src/sourcemap_builder.rs b/crates/oxc_codegen/src/sourcemap_builder.rs index 00e99c66902cf..545b23cf2829a 100644 --- a/crates/oxc_codegen/src/sourcemap_builder.rs +++ b/crates/oxc_codegen/src/sourcemap_builder.rs @@ -16,6 +16,8 @@ const LINE_SEARCH_LINEAR_ITERATIONS: usize = 16; pub struct ColumnOffsetsId(NonMaxU32); impl Idx for ColumnOffsetsId { + const MAX: usize = (u32::MAX - 1) as usize; + #[expect(clippy::cast_possible_truncation)] fn from_usize(idx: usize) -> Self { assert!(idx < u32::MAX as usize); @@ -23,6 +25,12 @@ impl Idx for ColumnOffsetsId { Self(unsafe { NonMaxU32::new_unchecked(idx as u32) }) } + #[expect(clippy::cast_possible_truncation)] + unsafe fn from_usize_unchecked(idx: usize) -> Self { + // SAFETY: Caller must ensure `idx` is a legal value for `NonMaxU32` + Self(unsafe { NonMaxU32::new_unchecked(idx as u32) }) + } + fn index(self) -> usize { self.0.get() as usize } diff --git a/crates/oxc_linter/src/rules/eslint/no_useless_backreference.rs b/crates/oxc_linter/src/rules/eslint/no_useless_backreference.rs index 4bc618cb51bcc..338fb37c4fb2b 100644 --- a/crates/oxc_linter/src/rules/eslint/no_useless_backreference.rs +++ b/crates/oxc_linter/src/rules/eslint/no_useless_backreference.rs @@ -121,6 +121,8 @@ enum BackRefInfoTarget<'a> { pub struct RegexNodeId(NonMaxU32); impl Idx for RegexNodeId { + const MAX: usize = (u32::MAX - 1) as usize; + #[expect(clippy::cast_possible_truncation)] fn from_usize(idx: usize) -> Self { assert!(idx < u32::MAX as usize); @@ -128,6 +130,12 @@ impl Idx for RegexNodeId { Self(unsafe { NonMaxU32::new_unchecked(idx as u32) }) } + #[expect(clippy::cast_possible_truncation)] + unsafe fn from_usize_unchecked(idx: usize) -> Self { + // SAFETY: Caller must ensure `idx` is a legal value for `NonMaxU32` + Self(unsafe { NonMaxU32::new_unchecked(idx as u32) }) + } + fn index(self) -> usize { self.0.get() as usize } diff --git a/crates/oxc_syntax/src/comment_node.rs b/crates/oxc_syntax/src/comment_node.rs index 04dfe8f308ecb..ae122b0caaae5 100644 --- a/crates/oxc_syntax/src/comment_node.rs +++ b/crates/oxc_syntax/src/comment_node.rs @@ -29,10 +29,16 @@ impl CommentNodeId { } impl Idx for CommentNodeId { + const MAX: usize = u32::MAX as usize; + fn from_usize(idx: usize) -> Self { Self(u32::try_from(idx).expect("`idx` is greater than `u32::MAX`")) } + unsafe fn from_usize_unchecked(idx: usize) -> Self { + Self(idx as u32) + } + fn index(self) -> usize { self.0 as usize } diff --git a/crates/oxc_syntax/src/node.rs b/crates/oxc_syntax/src/node.rs index 0fcaace1cd117..1d7f98649a71a 100644 --- a/crates/oxc_syntax/src/node.rs +++ b/crates/oxc_syntax/src/node.rs @@ -40,6 +40,8 @@ impl NodeId { } impl Idx for NodeId { + const MAX: usize = (u32::MAX - 1) as usize; + #[expect(clippy::cast_possible_truncation)] fn from_usize(idx: usize) -> Self { assert!(idx < u32::MAX as usize); @@ -47,6 +49,12 @@ impl Idx for NodeId { Self(unsafe { NonMaxU32::new_unchecked(idx as u32) }) } + #[expect(clippy::cast_possible_truncation)] + unsafe fn from_usize_unchecked(idx: usize) -> Self { + // SAFETY: Caller must ensure `idx` is a legal value for `NonMaxU32` + Self(unsafe { NonMaxU32::new_unchecked(idx as u32) }) + } + fn index(self) -> usize { self.0.get() as usize } diff --git a/crates/oxc_syntax/src/reference.rs b/crates/oxc_syntax/src/reference.rs index 735fd188a49a4..7b5200e2e3285 100644 --- a/crates/oxc_syntax/src/reference.rs +++ b/crates/oxc_syntax/src/reference.rs @@ -20,6 +20,8 @@ use oxc_ast_macros::ast; pub struct ReferenceId(NonMaxU32); impl Idx for ReferenceId { + const MAX: usize = (u32::MAX - 1) as usize; + #[expect(clippy::cast_possible_truncation)] fn from_usize(idx: usize) -> Self { assert!(idx < u32::MAX as usize); @@ -27,6 +29,12 @@ impl Idx for ReferenceId { Self(unsafe { NonMaxU32::new_unchecked(idx as u32) }) } + #[expect(clippy::cast_possible_truncation)] + unsafe fn from_usize_unchecked(idx: usize) -> Self { + // SAFETY: Caller must ensure `idx` is a legal value for `NonMaxU32` + Self(unsafe { NonMaxU32::new_unchecked(idx as u32) }) + } + fn index(self) -> usize { self.0.get() as usize } diff --git a/crates/oxc_syntax/src/scope.rs b/crates/oxc_syntax/src/scope.rs index a2554f3ed276c..d156dc3b067cb 100644 --- a/crates/oxc_syntax/src/scope.rs +++ b/crates/oxc_syntax/src/scope.rs @@ -39,6 +39,8 @@ impl ScopeId { } impl Idx for ScopeId { + const MAX: usize = (u32::MAX - 1) as usize; + #[expect(clippy::cast_possible_truncation)] fn from_usize(idx: usize) -> Self { assert!(idx < u32::MAX as usize); @@ -46,6 +48,12 @@ impl Idx for ScopeId { Self(unsafe { NonMaxU32::new_unchecked(idx as u32) }) } + #[expect(clippy::cast_possible_truncation)] + unsafe fn from_usize_unchecked(idx: usize) -> Self { + // SAFETY: Caller must ensure `idx` is a legal value for `NonMaxU32` + Self(unsafe { NonMaxU32::new_unchecked(idx as u32) }) + } + fn index(self) -> usize { self.0.get() as usize } diff --git a/crates/oxc_syntax/src/symbol.rs b/crates/oxc_syntax/src/symbol.rs index da0eaea4950bd..a16af3950ed4b 100644 --- a/crates/oxc_syntax/src/symbol.rs +++ b/crates/oxc_syntax/src/symbol.rs @@ -39,6 +39,8 @@ impl SymbolId { } impl Idx for SymbolId { + const MAX: usize = (u32::MAX - 1) as usize; + #[expect(clippy::cast_possible_truncation)] fn from_usize(idx: usize) -> Self { assert!(idx < u32::MAX as usize); @@ -46,6 +48,12 @@ impl Idx for SymbolId { Self(unsafe { NonMaxU32::new_unchecked(idx as u32) }) } + #[expect(clippy::cast_possible_truncation)] + unsafe fn from_usize_unchecked(idx: usize) -> Self { + // SAFETY: Caller must ensure `idx` is a legal value for `NonMaxU32` + Self(unsafe { NonMaxU32::new_unchecked(idx as u32) }) + } + fn index(self) -> usize { self.0.get() as usize } @@ -77,6 +85,8 @@ impl Serialize for SymbolId { pub struct RedeclarationId(NonMaxU32); impl Idx for RedeclarationId { + const MAX: usize = (u32::MAX - 1) as usize; + #[expect(clippy::cast_possible_truncation)] fn from_usize(idx: usize) -> Self { assert!(idx < u32::MAX as usize); @@ -84,6 +94,12 @@ impl Idx for RedeclarationId { Self(unsafe { NonMaxU32::new_unchecked(idx as u32) }) } + #[expect(clippy::cast_possible_truncation)] + unsafe fn from_usize_unchecked(idx: usize) -> Self { + // SAFETY: Caller must ensure `idx` is a legal value for `NonMaxU32` + Self(unsafe { NonMaxU32::new_unchecked(idx as u32) }) + } + fn index(self) -> usize { self.0.get() as usize } From bcfbefc80b892664f5e4f2a67eee001eab6d8cdd Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Sun, 28 Sep 2025 08:36:17 +0000 Subject: [PATCH 2/2] [autofix.ci] apply automated fixes --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index d6fc0271e9acc..d6b2f450b8d69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -158,7 +158,7 @@ unicode-id-start = "1" javascript-globals = "1" json-strip-comments = "3" oxc-browserslist = "2" -oxc_index = {git="https://github.com/oxc-project/oxc-index-vec.git",branch="reduce-bounds-checks"} +oxc_index = { git = "https://github.com/oxc-project/oxc-index-vec.git", branch = "reduce-bounds-checks" } oxc_resolver = "11" oxc_sourcemap = "4"