From 08a785c2e2b70f8096162ca77b032f73587749a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Joubert?= Date: Thu, 2 Jan 2025 11:05:22 +0100 Subject: [PATCH 1/3] Adapt XML generation for Ruby 3.4's default frozen string literals MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses warnings when running the gem in a Ruby 3.4 app, which has frozen string literals warning enabled by default. Ruby 3.5 will have frozen string literals turned on by default. Signed-off-by: Clément Joubert --- lib/sitemap_generator/builder/sitemap_file.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/sitemap_generator/builder/sitemap_file.rb b/lib/sitemap_generator/builder/sitemap_file.rb index a7000805..dd915314 100644 --- a/lib/sitemap_generator/builder/sitemap_file.rb +++ b/lib/sitemap_generator/builder/sitemap_file.rb @@ -26,8 +26,8 @@ def initialize(opts={}) @location = opts.is_a?(Hash) ? SitemapGenerator::SitemapLocation.new(opts) : opts @link_count = 0 @news_count = 0 - @xml_content = +'' # XML urlset content - @xml_wrapper_start = +<<-HTML + @xml_content = String.new # XML urlset content + @xml_wrapper_start = <<-HTML Date: Thu, 2 Jan 2025 12:07:06 +0100 Subject: [PATCH 2/3] Add Ruby 3.4 to test matrix --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a8d38f43..fe984a2b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,6 +19,7 @@ jobs: fail-fast: false matrix: ruby: + - '3.4' - '3.3' - '3.2' - '3.1' From 024273e399282ce25137e434a49ad0a37f18b8fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Joubert?= Date: Thu, 2 Jan 2025 12:10:19 +0100 Subject: [PATCH 3/3] Avoid duplicating the entire string when writing With a very large file, we avoid duplicating the whole string just to add the wrapper at the begging then at the end. I think this should save a lot on memory allocations and garbage collections on large sitemaps. --- lib/sitemap_generator/builder/sitemap_file.rb | 9 +++++---- lib/sitemap_generator/builder/sitemap_index_file.rb | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/sitemap_generator/builder/sitemap_file.rb b/lib/sitemap_generator/builder/sitemap_file.rb index dd915314..42b00a2a 100644 --- a/lib/sitemap_generator/builder/sitemap_file.rb +++ b/lib/sitemap_generator/builder/sitemap_file.rb @@ -26,7 +26,6 @@ def initialize(opts={}) @location = opts.is_a?(Hash) ? SitemapGenerator::SitemapLocation.new(opts) : opts @link_count = 0 @news_count = 0 - @xml_content = String.new # XML urlset content @xml_wrapper_start = <<-HTML HTML @xml_wrapper_start.gsub!(/\s+/, ' ').gsub!(/ *> */, '>').strip! - @xml_wrapper_end = %q[] + @xml_wrapper_end = '' @filesize = SitemapGenerator::Utilities.bytesize(@xml_wrapper_start) + SitemapGenerator::Utilities.bytesize(@xml_wrapper_end) + @xml_content = @xml_wrapper_start @written = false @reserved_name = nil # holds the name reserved from the namer @frozen = false # rather than actually freeze, use this boolean @@ -138,8 +138,9 @@ def write raise SitemapGenerator::SitemapError.new("Sitemap already written!") if written? finalize! unless finalized? reserve_name - @location.write("#{@xml_wrapper_start}#{@xml_content}#{@xml_wrapper_end}", link_count) - @xml_content = @xml_wrapper_start = @xml_wrapper_end = String.new + @xml_content << @xml_wrapper_end + @location.write(@xml_content, link_count) + @xml_content = @xml_wrapper_end = '' @written = true end diff --git a/lib/sitemap_generator/builder/sitemap_index_file.rb b/lib/sitemap_generator/builder/sitemap_index_file.rb index 8753906c..c175bac8 100644 --- a/lib/sitemap_generator/builder/sitemap_index_file.rb +++ b/lib/sitemap_generator/builder/sitemap_index_file.rb @@ -12,7 +12,6 @@ def initialize(opts={}) @location = opts.is_a?(Hash) ? SitemapGenerator::SitemapIndexLocation.new(opts) : opts @link_count = 0 @sitemaps_link_count = 0 - @xml_content = +'' # XML urlset content @xml_wrapper_start = +<<-HTML HTML @xml_wrapper_start.gsub!(/\s+/, ' ').gsub!(/ *> */, '>').strip! - @xml_wrapper_end = %q[] + @xml_wrapper_end = '' @filesize = SitemapGenerator::Utilities.bytesize(@xml_wrapper_start) + SitemapGenerator::Utilities.bytesize(@xml_wrapper_end) + @xml_content = @xml_wrapper_start @written = false @reserved_name = nil # holds the name reserved from the namer @frozen = false # rather than actually freeze, use this boolean