@@ -196,42 +196,43 @@ pub(super) struct HookUsageChecker {
196196struct HookUsageCheckerInner {
197197 _buffer : Buffer ,
198198 bytes : NonNull < AtomicU8 > ,
199- len : usize ,
200199}
201200
202201unsafe impl Send for HookUsageCheckerInner { }
203202unsafe impl Sync for HookUsageCheckerInner { }
204203
205204impl HookUsageChecker {
206205 pub ( super ) fn new ( buffer : Buffer ) -> Self {
206+ assert_eq ! (
207+ buffer. len( ) ,
208+ HOOK_USAGE_BUFFER_BYTE_LENGTH ,
209+ "hook_usage_buffer length mismatch" ,
210+ ) ;
207211 Self {
208212 inner : {
209- let len = buffer. len ( ) ;
210213 #[ allow( clippy:: unwrap_used) ]
211214 let bytes = NonNull :: new ( buffer. as_ref ( ) . as_ptr ( ) as * mut AtomicU8 ) . unwrap ( ) ;
212215 Arc :: new ( HookUsageCheckerInner {
213216 _buffer : buffer,
214217 bytes,
215- len,
216218 } )
217219 } ,
218220 }
219221 }
220222
221- pub ( super ) fn is_used ( & self , kind : & RegisterJsTapKind ) -> bool {
223+ #[ inline( always) ]
224+ pub ( super ) fn is_used ( & self , kind : RegisterJsTapKind ) -> bool {
222225 let inner = & self . inner ;
223- let bit_index = * kind as usize ;
226+ let bit_index = kind as usize ;
224227 let byte_index = bit_index >> 3 ;
225228 let bit_mask = 1 << ( bit_index & 7 ) ;
226- if byte_index >= inner. len {
227- return false ;
228- }
229229 let byte = unsafe { & * inner. bytes . as_ptr ( ) . add ( byte_index) } . load ( Ordering :: Acquire ) ;
230230 byte & bit_mask != 0
231231 }
232232}
233233
234- fn should_skip_register ( hook_usage_checker : & HookUsageChecker , kind : & RegisterJsTapKind ) -> bool {
234+ #[ inline( always) ]
235+ fn should_skip_register ( hook_usage_checker : & HookUsageChecker , kind : RegisterJsTapKind ) -> bool {
235236 !hook_usage_checker. is_used ( kind)
236237}
237238
@@ -399,7 +400,7 @@ macro_rules! define_register {
399400 & self ,
400401 hook: & $tap_hook,
401402 ) -> rspack_error:: Result <Vec <<$tap_hook as Hook >:: Tap >> {
402- if should_skip_register( & self . inner. hook_usage_checker, & $kind) {
403+ if should_skip_register( & self . inner. hook_usage_checker, $kind) {
403404 return Ok ( Vec :: new( ) ) ;
404405 }
405406 let js_taps = self . inner. call_register( hook) . await ?;
@@ -414,6 +415,7 @@ macro_rules! define_register {
414415}
415416
416417#[ napi]
418+ #[ repr( u8 ) ]
417419#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
418420pub enum RegisterJsTapKind {
419421 CompilerThisCompilation ,
@@ -469,6 +471,80 @@ pub enum RegisterJsTapKind {
469471 RsdoctorPluginAssets ,
470472}
471473
474+ const HOOK_USAGE_BUFFER_BYTE_LENGTH : usize =
475+ ( ( RegisterJsTapKind :: RsdoctorPluginAssets as usize ) >> 3 ) + 1 ;
476+
477+ const COMPILER_HOOKS : & [ RegisterJsTapKind ] = & [
478+ RegisterJsTapKind :: CompilerThisCompilation ,
479+ RegisterJsTapKind :: CompilerCompilation ,
480+ RegisterJsTapKind :: CompilerMake ,
481+ RegisterJsTapKind :: CompilerFinishMake ,
482+ RegisterJsTapKind :: CompilerShouldEmit ,
483+ RegisterJsTapKind :: CompilerEmit ,
484+ RegisterJsTapKind :: CompilerAfterEmit ,
485+ RegisterJsTapKind :: CompilerAssetEmitted ,
486+ ] ;
487+
488+ const COMPILATION_HOOKS : & [ RegisterJsTapKind ] = & [
489+ RegisterJsTapKind :: CompilationBuildModule ,
490+ RegisterJsTapKind :: CompilationStillValidModule ,
491+ RegisterJsTapKind :: CompilationSucceedModule ,
492+ RegisterJsTapKind :: CompilationExecuteModule ,
493+ RegisterJsTapKind :: CompilationFinishModules ,
494+ RegisterJsTapKind :: CompilationOptimizeModules ,
495+ RegisterJsTapKind :: CompilationAfterOptimizeModules ,
496+ RegisterJsTapKind :: CompilationOptimizeTree ,
497+ RegisterJsTapKind :: CompilationOptimizeChunkModules ,
498+ RegisterJsTapKind :: CompilationBeforeModuleIds ,
499+ RegisterJsTapKind :: CompilationAdditionalTreeRuntimeRequirements ,
500+ RegisterJsTapKind :: CompilationRuntimeRequirementInTree ,
501+ RegisterJsTapKind :: CompilationRuntimeModule ,
502+ RegisterJsTapKind :: CompilationChunkHash ,
503+ RegisterJsTapKind :: CompilationChunkAsset ,
504+ RegisterJsTapKind :: CompilationProcessAssets ,
505+ RegisterJsTapKind :: CompilationAfterProcessAssets ,
506+ RegisterJsTapKind :: CompilationSeal ,
507+ RegisterJsTapKind :: CompilationAfterSeal ,
508+ RegisterJsTapKind :: NormalModuleFactoryBeforeResolve ,
509+ RegisterJsTapKind :: NormalModuleFactoryFactorize ,
510+ RegisterJsTapKind :: NormalModuleFactoryResolve ,
511+ RegisterJsTapKind :: NormalModuleFactoryAfterResolve ,
512+ RegisterJsTapKind :: NormalModuleFactoryCreateModule ,
513+ RegisterJsTapKind :: NormalModuleFactoryResolveForScheme ,
514+ RegisterJsTapKind :: ContextModuleFactoryBeforeResolve ,
515+ RegisterJsTapKind :: ContextModuleFactoryAfterResolve ,
516+ RegisterJsTapKind :: JavascriptModulesChunkHash ,
517+ RegisterJsTapKind :: HtmlPluginBeforeAssetTagGeneration ,
518+ RegisterJsTapKind :: HtmlPluginAlterAssetTags ,
519+ RegisterJsTapKind :: HtmlPluginAlterAssetTagGroups ,
520+ RegisterJsTapKind :: HtmlPluginAfterTemplateExecution ,
521+ RegisterJsTapKind :: HtmlPluginBeforeEmit ,
522+ RegisterJsTapKind :: HtmlPluginAfterEmit ,
523+ RegisterJsTapKind :: RuntimePluginCreateScript ,
524+ RegisterJsTapKind :: RuntimePluginCreateLink ,
525+ RegisterJsTapKind :: RuntimePluginLinkPreload ,
526+ RegisterJsTapKind :: RuntimePluginLinkPrefetch ,
527+ RegisterJsTapKind :: RsdoctorPluginModuleGraph ,
528+ RegisterJsTapKind :: RsdoctorPluginChunkGraph ,
529+ RegisterJsTapKind :: RsdoctorPluginModuleIds ,
530+ RegisterJsTapKind :: RsdoctorPluginModuleSources ,
531+ RegisterJsTapKind :: RsdoctorPluginAssets ,
532+ ] ;
533+
534+ #[ napi( object) ]
535+ pub struct RegisterJsTapScopeKinds {
536+ pub compiler : Vec < RegisterJsTapKind > ,
537+ pub compilation : Vec < RegisterJsTapKind > ,
538+ }
539+
540+ #[ napi]
541+ pub fn get_register_js_tap_scope_kinds ( ) -> RegisterJsTapScopeKinds {
542+ RegisterJsTapScopeKinds {
543+ compiler : COMPILER_HOOKS . to_vec ( ) ,
544+ compilation : COMPILATION_HOOKS . to_vec ( ) ,
545+ }
546+ }
547+
472548#[ derive( Clone ) ]
473549#[ napi( object, object_to_js = false ) ]
474550pub struct RegisterJsTaps {
@@ -2033,8 +2109,7 @@ mod tests {
20332109 use super :: * ;
20342110
20352111 fn checker_with_used_kinds ( kinds : & [ RegisterJsTapKind ] ) -> HookUsageChecker {
2036- let byte_len = ( ( RegisterJsTapKind :: RsdoctorPluginAssets as usize ) >> 3 ) + 1 ;
2037- let mut bytes = vec ! [ 0 ; byte_len] ;
2112+ let mut bytes = vec ! [ 0 ; HOOK_USAGE_BUFFER_BYTE_LENGTH ] ;
20382113 for kind in kinds {
20392114 let bit_index = * kind as usize ;
20402115 bytes[ bit_index >> 3 ] |= 1 << ( bit_index & 7 ) ;
@@ -2049,9 +2124,9 @@ mod tests {
20492124 RegisterJsTapKind :: CompilationProcessAssets ,
20502125 ] ) ;
20512126
2052- assert ! ( checker. is_used( & RegisterJsTapKind :: CompilerCompilation ) ) ;
2053- assert ! ( checker. is_used( & RegisterJsTapKind :: CompilationProcessAssets ) ) ;
2054- assert ! ( !checker. is_used( & RegisterJsTapKind :: CompilerMake ) ) ;
2127+ assert ! ( checker. is_used( RegisterJsTapKind :: CompilerCompilation ) ) ;
2128+ assert ! ( checker. is_used( RegisterJsTapKind :: CompilationProcessAssets ) ) ;
2129+ assert ! ( !checker. is_used( RegisterJsTapKind :: CompilerMake ) ) ;
20552130 }
20562131
20572132 #[ test]
@@ -2061,11 +2136,11 @@ mod tests {
20612136
20622137 assert ! ( should_skip_register(
20632138 & empty_checker,
2064- & RegisterJsTapKind :: CompilationBuildModule ,
2139+ RegisterJsTapKind :: CompilationBuildModule ,
20652140 ) ) ;
20662141 assert ! ( !should_skip_register(
20672142 & used_checker,
2068- & RegisterJsTapKind :: CompilerCompilation ,
2143+ RegisterJsTapKind :: CompilerCompilation ,
20692144 ) ) ;
20702145 }
20712146}
0 commit comments