From 2862404b7b6d879df8063952f0641374aac92225 Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Wed, 14 Oct 2026 23:00:16 -0400 Subject: [PATCH] Avoid creating a String for every key format_key called Symbol#to_s, which returns a new String on every call, so each key emitted allocated one. Symbol#name returns the frozen String Ruby already keeps for the Symbol instead, so repeated keys allocate nothing. String keys still go through to_s, which returns them as-is. Symbol#name has been available since Ruby 3.0, and the gemspec requires 3.3 or later. On alba's collection benchmark this cuts allocations per render from 7,923 to 3,523 objects (-56%). --- lib/props_template/base.rb | 2 +- lib/props_template/base_with_extensions.rb | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/props_template/base.rb b/lib/props_template/base.rb index 273b83f..9380f4d 100644 --- a/lib/props_template/base.rb +++ b/lib/props_template/base.rb @@ -36,7 +36,7 @@ def handle_set_block(key, options) end def format_key(key) - key.to_s + (::Symbol === key) ? key.name : key.to_s end def set!(key, value = nil) diff --git a/lib/props_template/base_with_extensions.rb b/lib/props_template/base_with_extensions.rb index 94828d7..2a7d1d8 100644 --- a/lib/props_template/base_with_extensions.rb +++ b/lib/props_template/base_with_extensions.rb @@ -46,10 +46,6 @@ def scoped_state @em = ExtensionManager.new(self, prev_state[1], prev_state[2]) end - def format_key(key) - key.to_s - end - def set!(key, options = {}, &block) if !block && options.is_a?(Props::Options) options.valid_for_set!