From b77bfee0925e264ea5376913e4abd8ca2ad001a1 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 30 Jan 2025 09:34:01 +0100 Subject: [PATCH] Avoid calls to `require` in hotspots Followup: https://github.com/ruby/psych/pull/686 This single call shows up as 4% of some controller actions in the lobsters benchmark. Profile: https://share.firefox.dev/3EqKnhS --- lib/psych/nodes/node.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/psych/nodes/node.rb b/lib/psych/nodes/node.rb index 1a4ea553..6ae5c591 100644 --- a/lib/psych/nodes/node.rb +++ b/lib/psych/nodes/node.rb @@ -55,7 +55,8 @@ def to_ruby(symbolize_names: false, freeze: false, strict_integer: false) # # See also Psych::Visitors::Emitter def yaml io = nil, options = {} - require "stringio" + require "stringio" unless defined?(StringIO) + real_io = io || StringIO.new(''.encode('utf-8')) Visitors::Emitter.new(real_io, options).accept self