From 63f78bbaaea4bb0b7009e50a938ef146416c53c2 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Fri, 26 Apr 2019 13:26:14 +0200 Subject: Reduce string allocations in scalar_scanner --- ext/psych/lib/psych/scalar_scanner.rb | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/ext/psych/lib/psych/scalar_scanner.rb b/ext/psych/lib/psych/scalar_scanner.rb index 29c156c212..f89aa68ff8 100644 --- a/ext/psych/lib/psych/scalar_scanner.rb +++ b/ext/psych/lib/psych/scalar_scanner.rb @@ -34,68 +34,66 @@ module Psych return string if @string_cache.key?(string) return @symbol_cache[string] if @symbol_cache.key?(string) - case string # Check for a String type, being careful not to get caught by hash keys, hex values, and # special floats (e.g., -.inf). - when /^[^\d\.:-]?[A-Za-z_\s!@#\$%\^&\*\(\)\{\}\<\>\|\/\\~;=]+/, /\n/ + if string.match?(/^[^\d\.:-]?[A-Za-z_\s!@#\$%\^&\*\(\)\{\}\<\>\|\/\\~;=]+/) || string.match?(/\n/) if string.length > 5 @string_cache[string] = true return string end - case string - when /^[^ytonf~]/i + if string.match?(/^[^ytonf~]/i) @string_cache[string] = true string - when '~', /^null$/i + elsif string == '~' || string.match?(/^null$/i) nil - when /^(yes|true|on)$/i + elsif string.match?(/^(yes|true|on)$/i) true - when /^(no|false|off)$/i + elsif string.match?(/^(no|false|off)$/i) false else @string_cache[string] = true string end - when TIME + elsif string.match?(TIME) begin parse_time string rescue ArgumentError string end - when /^\d{4}-(?:1[012]|0\d|\d)-(?:[12]\d|3[01]|0\d|\d)$/ + elsif string.match?(/^\d{4}-(?:1[012]|0\d|\d)-(?:[12]\d|3[01]|0\d|\d)$/) require 'date' begin class_loader.date.strptime(string, '%Y-%m-%d') rescue ArgumentError string end - when /^\.inf$/i + elsif string.match?(/^\.inf$/i) Float::INFINITY - when /^-\.inf$/i + elsif string.match?(/^-\.inf$/i) -Float::INFINITY - when /^\.nan$/i + elsif string.match?(/^\.nan$/i) Float::NAN - when /^:./ + elsif string.match?(/^:./) if string =~ /^:(["'])(.*)\1/ @symbol_cache[string] = class_loader.symbolize($2.sub(/^:/, '')) else @symbol_cache[string] = class_loader.symbolize(string.sub(/^:/, '')) end - when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}$/ + elsif string.match?(/^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}$/) i = 0 string.split(':').each_with_index do |n,e| i += (n.to_i * 60 ** (e - 2).abs) end i - when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}\.[0-9_]*$/ + elsif string.match?(/^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}\.[0-9_]*$/) i = 0 string.split(':').each_with_index do |n,e| i += (n.to_f * 60 ** (e - 2).abs) end i - when FLOAT - if string =~ /\A[-+]?\.\Z/ + elsif string.match?(FLOAT) + if string.match?(/\A[-+]?\.\Z/) @string_cache[string] = true string else -- cgit v1.2.3