From 7c9532ef858fc4ee8010c02a9ddc76c6a9aba7bd Mon Sep 17 00:00:00 2001 From: Jared White Date: Thu, 5 Sep 2024 22:27:16 -0700 Subject: [PATCH] Memoize resource URLs for major performance gain Resolves #914 --- Gemfile.lock | 107 +++++++++--------- .../lib/bridgetown-core/collection.rb | 2 +- .../lib/bridgetown-core/resource/base.rb | 12 +- .../resource/permalink_processor.rb | 2 +- 4 files changed, 67 insertions(+), 56 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index fa9716e77..201bf7663 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -60,18 +60,19 @@ PATH GEM remote: https://rubygems.org/ specs: - activesupport (7.1.3.2) + activesupport (7.2.1) base64 bigdecimal - concurrent-ruby (~> 1.0, >= 1.0.2) + concurrent-ruby (~> 1.0, >= 1.3.1) connection_pool (>= 2.2.5) drb i18n (>= 1.6, < 2) + logger (>= 1.4.2) minitest (>= 5.1) - mutex_m - tzinfo (~> 2.0) - addressable (2.8.6) - public_suffix (>= 2.0.2, < 6.0) + securerandom (>= 0.3) + tzinfo (~> 2.0, >= 2.0.5) + addressable (2.8.7) + public_suffix (>= 2.0.2, < 7.0) amazing_print (1.6.0) ansi (1.5.0) ast (2.4.2) @@ -79,29 +80,33 @@ GEM base64 (0.2.0) benchmark (0.3.0) benchmark-ips (2.13.0) - bigdecimal (3.1.7) - builder (3.2.4) - concurrent-ruby (1.2.3) + bigdecimal (3.1.8) + builder (3.3.0) + concurrent-ruby (1.3.4) connection_pool (2.4.1) csv (3.3.0) diff-lcs (1.5.1) - docile (1.4.0) + docile (1.4.1) drb (2.2.1) - dry-inflector (1.0.0) + dry-inflector (1.1.0) e2mmap (0.1.0) - erubi (1.12.0) - faraday (2.9.0) - faraday-net_http (>= 2.0, < 3.2) + erubi (1.13.0) + faraday (2.11.0) + faraday-net_http (>= 2.0, < 3.4) + logger faraday-follow_redirects (0.3.0) faraday (>= 1, < 3) - faraday-net_http (3.1.0) + faraday-net_http (3.3.0) net-http - ffi (1.16.3) + ffi (1.17.0) + ffi (1.17.0-arm64-darwin) + ffi (1.17.0-x86_64-darwin) + ffi (1.17.0-x86_64-linux-gnu) hash_with_dot_access (2.1.1) - i18n (1.14.4) + i18n (1.14.5) concurrent-ruby (~> 1.0) inclusive (1.0.0) - jaro_winkler (1.5.6) + jaro_winkler (1.6.0) json (2.7.2) kramdown (2.4.0) rexml @@ -112,38 +117,38 @@ GEM listen (3.9.0) rb-fsevent (~> 0.10, >= 0.10.3) rb-inotify (~> 0.9, >= 0.9.10) - memory_profiler (1.0.1) - mini_portile2 (2.8.6) - minitest (5.22.3) + logger (1.6.1) + memory_profiler (1.0.2) + mini_portile2 (2.8.7) + minitest (5.25.1) minitest-profile (0.0.2) - minitest-reporters (1.6.1) + minitest-reporters (1.7.1) ansi builder minitest (>= 5.0) ruby-progressbar - mutex_m (0.2.0) net-http (0.4.1) uri - nokogiri (1.16.4) + nokogiri (1.16.7) mini_portile2 (~> 2.8.2) racc (~> 1.4) - nokogiri (1.16.4-arm64-darwin) + nokogiri (1.16.7-arm64-darwin) racc (~> 1.4) - nokogiri (1.16.4-x86_64-darwin) + nokogiri (1.16.7-x86_64-darwin) racc (~> 1.4) - nokogiri (1.16.4-x86_64-linux) + nokogiri (1.16.7-x86_64-linux) racc (~> 1.4) nokolexbor (0.5.4) nokolexbor (0.5.4-arm64-darwin) nokolexbor (0.5.4-x86_64-darwin) nokolexbor (0.5.4-x86_64-linux) - parallel (1.24.0) - parser (3.3.0.5) + parallel (1.26.3) + parser (3.3.5.0) ast (~> 2.4.1) racc - public_suffix (5.0.5) - racc (1.7.3) - rack (3.0.10) + public_suffix (6.0.1) + racc (1.8.1) + rack (3.1.7) rack-test (2.1.0) rack (>= 1.3) rackup (2.1.0) @@ -152,41 +157,41 @@ GEM rainbow (3.1.1) rake (13.2.1) rb-fsevent (0.11.2) - rb-inotify (0.10.1) + rb-inotify (0.11.1) ffi (~> 1.0) rbs (2.8.4) - regexp_parser (2.9.0) + regexp_parser (2.9.2) reverse_markdown (2.1.1) nokogiri - rexml (3.2.6) - roda (3.79.0) + rexml (3.3.7) + roda (3.83.0) rack - rouge (4.2.1) - rspec-mocks (3.13.0) + rouge (4.3.0) + rspec-mocks (3.13.1) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.13.0) rspec-support (3.13.1) - rubocop (1.63.2) + rubocop (1.66.1) json (~> 2.3) language_server-protocol (>= 3.17.0) parallel (~> 1.10) parser (>= 3.3.0.2) rainbow (>= 2.2.2, < 4.0) - regexp_parser (>= 1.8, < 3.0) - rexml (>= 3.2.5, < 4.0) - rubocop-ast (>= 1.31.1, < 2.0) + regexp_parser (>= 2.4, < 3.0) + rubocop-ast (>= 1.32.2, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 2.4.0, < 3.0) - rubocop-ast (1.31.2) - parser (>= 3.3.0.4) + rubocop-ast (1.32.3) + parser (>= 3.3.1.0) rubocop-bridgetown (0.5.0) rubocop (~> 1.23) rubocop-performance (~> 1.12) - rubocop-performance (1.21.0) + rubocop-performance (1.21.1) rubocop (>= 1.48.1, < 2.0) rubocop-ast (>= 1.31.1, < 2.0) ruby-progressbar (1.13.0) - serbea (2.1.0) + securerandom (0.3.1) + serbea (2.2.0) erubi (>= 1.10) tilt (~> 2.0) shoulda (4.0.0) @@ -224,15 +229,15 @@ GEM zeitwerk (~> 2.5) terminal-table (3.0.2) unicode-display_width (>= 1.1.1, < 3) - thor (1.3.1) - tilt (2.3.0) + thor (1.3.2) + tilt (2.4.0) tzinfo (2.0.6) concurrent-ruby (~> 1.0) unicode-display_width (2.5.0) - uri (0.13.0) + uri (0.13.1) webrick (1.8.1) - yard (0.9.36) - zeitwerk (2.6.13) + yard (0.9.37) + zeitwerk (2.6.18) PLATFORMS arm64-darwin-22 diff --git a/bridgetown-core/lib/bridgetown-core/collection.rb b/bridgetown-core/lib/bridgetown-core/collection.rb index b7939ef35..f6da4a830 100644 --- a/bridgetown-core/lib/bridgetown-core/collection.rb +++ b/bridgetown-core/lib/bridgetown-core/collection.rb @@ -400,7 +400,7 @@ def relative_model_path_for(full_path, manifest: nil) def model_id_from_relative_path(model_relative_path, manifest: nil) scheme = manifest ? "plugin" : "repo" - id = +"#{scheme}://#{label}.collection/" + id = "#{scheme}://#{label}.collection/" id += "#{manifest.origin}/" if manifest id += Addressable::URI.escape(model_relative_path).gsub("#", "%23") id diff --git a/bridgetown-core/lib/bridgetown-core/resource/base.rb b/bridgetown-core/lib/bridgetown-core/resource/base.rb index c1d014632..208ef9a8b 100644 --- a/bridgetown-core/lib/bridgetown-core/resource/base.rb +++ b/bridgetown-core/lib/bridgetown-core/resource/base.rb @@ -139,7 +139,12 @@ def read! set_date_from_string(data.date) end - @destination = Destination.new(self) if requires_destination? + if requires_destination? + @destination = Destination.new(self) + # memoize for performance: + @absolute_url = absolute_url + @relative_url = relative_url + end trigger_hooks :post_read @@ -221,12 +226,12 @@ def path # @return [String] def absolute_url - format_url destination&.absolute_url + @absolute_url || format_url(destination&.absolute_url) end # @return [String] def relative_url - format_url destination&.relative_url + @relative_url || format_url(destination&.relative_url) end # @return [String] @@ -366,6 +371,7 @@ def prepare_for_fast_refresh! # rubocop:todo Metrics key == "categories" || key == "tags" || site.taxonomy_types.keys.any?(key) end model.attributes = model.origin.read + @relative_url = @absolute_url = nil # wipe memoizations read! tax_diff = past_values.any? { |k, v| @data.peek[k] != v } diff --git a/bridgetown-core/lib/bridgetown-core/resource/permalink_processor.rb b/bridgetown-core/lib/bridgetown-core/resource/permalink_processor.rb index ea3af5aca..0331bbaf3 100644 --- a/bridgetown-core/lib/bridgetown-core/resource/permalink_processor.rb +++ b/bridgetown-core/lib/bridgetown-core/resource/permalink_processor.rb @@ -120,7 +120,7 @@ def ensure_base_path(permalink) end # @param resource [Bridgetown::Resource::Base] - register_placeholder :name, ->(resource) do + register_placeholder :name, ->(resource) do # rubocop:disable Style/SymbolProc resource.basename_without_ext end