diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 0f2cf2b20..c2c997177 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -10,6 +10,10 @@ nav_order: 6 ## main +* Speed up cold cache digests by skipping the `# Template Dependency:` scan for sources that declare none, and by not translating class names in synthetic templates, which only declare paths. + + *Erik Axel Nielsen* + ## 4.22.0 * Comment on release pull requests with benchmark results compared to the previous release. diff --git a/lib/view_component/cache_digest/dependency_tracking.rb b/lib/view_component/cache_digest/dependency_tracking.rb index e5c6cc8e6..dbb120579 100644 --- a/lib/view_component/cache_digest/dependency_tracking.rb +++ b/lib/view_component/cache_digest/dependency_tracking.rb @@ -21,6 +21,10 @@ def find_dependencies(name, template, view_paths = nil) dependencies = super source = template.source + # Synthetic templates already declare every dependency found in their + # sources, as paths. + next dependencies if name.start_with?("#{CacheDigest::VIRTUAL_PATH_PREFIX}/") + # `# Template Dependency: SomeComponent` names a class, which Rails # would resolve as a template path and never find. Swap it for the path # the component is digested under, so the declaration resolves instead @@ -29,8 +33,6 @@ def find_dependencies(name, template, view_paths = nil) dependencies = dependencies - [declared] + [virtual_path] end - # Synthetic templates already declare every dependency found in their sources. - next dependencies if name.start_with?("#{CacheDigest::VIRTUAL_PATH_PREFIX}/") next dependencies unless CacheDigest.template_dependencies?(source, template.handler) component_paths, inferred_partials = diff --git a/lib/view_component/cache_digest/resolver.rb b/lib/view_component/cache_digest/resolver.rb index d8654936e..4dd30c9e3 100644 --- a/lib/view_component/cache_digest/resolver.rb +++ b/lib/view_component/cache_digest/resolver.rb @@ -184,6 +184,10 @@ def template_sources(component) def explicit_dependencies(sources) sources.flat_map { |source| + # A cheap check first: most sources declare nothing, and the + # pattern is slow to scan for in sources with many comments. + next [] unless source.include?("Template Dependency:") + declared = source.scan(CacheDigest::EXPLICIT_DEPENDENCY).flatten # Component class names are translated to the path they're digested