Skip to content

Commit f24cc38

Browse files
committed
refactor BYTE_LENGTH
1 parent 958603c commit f24cc38

3 files changed

Lines changed: 21 additions & 25 deletions

File tree

crates/node_binding/napi-binding.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,8 @@ export declare enum BuiltinPluginName {
626626

627627
export declare function cleanupGlobalTrace(): void
628628

629+
export const COMPILATION_HOOK_SUBSCRIPTION_BITSET_BYTE_LENGTH: number
630+
629631
export declare enum CompilationHooks {
630632
BuildModule = 0,
631633
StillValidModule = 1,
@@ -672,6 +674,8 @@ export declare enum CompilationHooks {
672674
RsdoctorPluginAssets = 42
673675
}
674676

677+
export const COMPILER_HOOK_SUBSCRIPTION_BITSET_BYTE_LENGTH: number
678+
675679
export declare enum CompilerHooks {
676680
ThisCompilation = 0,
677681
Compilation = 1,

crates/rspack_binding_api/src/plugins/interceptor.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,15 @@ impl HookSubscriptionBitsetView {
194194
pub(super) fn compiler(bitset: Buffer) -> Self {
195195
Self::new(
196196
bitset,
197-
COMPILER_HOOK_SUBSCRIPTION_BITSET_BYTE_LENGTH,
197+
COMPILER_HOOK_SUBSCRIPTION_BITSET_BYTE_LENGTH as usize,
198198
"compiler hook subscription bitset length mismatch",
199199
)
200200
}
201201

202202
pub(super) fn compilation(bitset: Buffer) -> Self {
203203
Self::new(
204204
bitset,
205-
COMPILATION_HOOK_SUBSCRIPTION_BITSET_BYTE_LENGTH,
205+
COMPILATION_HOOK_SUBSCRIPTION_BITSET_BYTE_LENGTH as usize,
206206
"compilation hook subscription bitset length mismatch",
207207
)
208208
}
@@ -430,6 +430,10 @@ pub enum CompilerHooks {
430430

431431
const COMPILER_HOOK_COUNT: usize = (CompilerHooks::AssetEmitted as usize) + 1;
432432

433+
#[napi]
434+
pub const COMPILER_HOOK_SUBSCRIPTION_BITSET_BYTE_LENGTH: u32 =
435+
((COMPILER_HOOK_COUNT as u32 - 1) >> 3) + 1;
436+
433437
#[napi]
434438
#[repr(u8)]
435439
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -481,18 +485,9 @@ pub enum CompilationHooks {
481485

482486
const COMPILATION_HOOK_COUNT: usize = (CompilationHooks::RsdoctorPluginAssets as usize) + 1;
483487

484-
const fn hook_subscription_bitset_byte_length(kind_count: usize) -> usize {
485-
if kind_count == 0 {
486-
return 0;
487-
}
488-
((kind_count - 1) >> 3) + 1
489-
}
490-
491-
const COMPILER_HOOK_SUBSCRIPTION_BITSET_BYTE_LENGTH: usize =
492-
hook_subscription_bitset_byte_length(COMPILER_HOOK_COUNT);
493-
494-
const COMPILATION_HOOK_SUBSCRIPTION_BITSET_BYTE_LENGTH: usize =
495-
hook_subscription_bitset_byte_length(COMPILATION_HOOK_COUNT);
488+
#[napi]
489+
pub const COMPILATION_HOOK_SUBSCRIPTION_BITSET_BYTE_LENGTH: u32 =
490+
((COMPILATION_HOOK_COUNT as u32 - 1) >> 3) + 1;
496491

497492
#[derive(Clone)]
498493
#[napi(object, object_to_js = false)]

packages/rspack/src/BindingHooks.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,11 @@ type DefaultAdditionalOptions =
1616
? A
1717
: never;
1818

19-
const COMPILER_HOOK_COUNT = binding.CompilerHooks.AssetEmitted + 1;
20-
const COMPILATION_HOOK_COUNT =
21-
binding.CompilationHooks.RsdoctorPluginAssets + 1;
22-
23-
const getHookSubscriptionBitsetByteLength = (kindCount: number) =>
24-
Math.max((kindCount - 1) >> 3, 0) + 1;
25-
2619
export class HookSubscriptionBitset {
2720
readonly buffer: Buffer;
2821

29-
constructor(kindCount: number) {
30-
this.buffer = Buffer.alloc(getHookSubscriptionBitsetByteLength(kindCount));
22+
constructor(byteLength: number) {
23+
this.buffer = Buffer.alloc(byteLength);
3124
}
3225

3326
markSubscribed(bitIndex: number) {
@@ -50,10 +43,14 @@ export const COMPILATION_HOOK_SUBSCRIPTION_BITSETS = new WeakMap<
5043
>();
5144

5245
export const createCompilerHookSubscriptionBitset = () =>
53-
new HookSubscriptionBitset(COMPILER_HOOK_COUNT);
46+
new HookSubscriptionBitset(
47+
binding.COMPILER_HOOK_SUBSCRIPTION_BITSET_BYTE_LENGTH,
48+
);
5449

5550
export const createCompilationHookSubscriptionBitset = () =>
56-
new HookSubscriptionBitset(COMPILATION_HOOK_COUNT);
51+
new HookSubscriptionBitset(
52+
binding.COMPILATION_HOOK_SUBSCRIPTION_BITSET_BYTE_LENGTH,
53+
);
5754

5855
export class BindingSyncHook<
5956
T = any,

0 commit comments

Comments
 (0)