From c1f024f99d4f24dde4ca3c1f35825eba19c413f6 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 30 Jan 2025 09:34:01 +0100 Subject: [PATCH] [ruby/psych] 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 https://github.com/ruby/psych/commit/b77bfee092 --- ext/psych/lib/psych/nodes/node.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ext/psych/lib/psych/nodes/node.rb b/ext/psych/lib/psych/nodes/node.rb index 1a4ea5531f..6ae5c59148 100644 --- a/ext/psych/lib/psych/nodes/node.rb +++ b/ext/psych/lib/psych/nodes/node.rb @@ -55,7 +55,8 @@ module Psych # # 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