From f4c2ac4825f99ec4af529d65ae98abfaf034fb0f Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 2 Mar 2017 04:08:23 +0000 Subject: deduplicate strings using String#-@ (uminus) in Ruby 2.5+ This is faster than relying on eval() for older Rubies. https://bugs.ruby-lang.org/issues/13077 Ruby 2.5 is targetted for release in December 2017. --- lib/dtas/source/av_ff_common.rb | 8 ++++++-- lib/dtas/source/sox.rb | 7 ++----- 2 files changed, 8 insertions(+), 7 deletions(-) (limited to 'lib/dtas/source') diff --git a/lib/dtas/source/av_ff_common.rb b/lib/dtas/source/av_ff_common.rb index d89ea17..ae654ba 100644 --- a/lib/dtas/source/av_ff_common.rb +++ b/lib/dtas/source/av_ff_common.rb @@ -109,13 +109,17 @@ module DTAS::Source::AvFfCommon # :nodoc: f = $1.dup f =~ /^duration=([\d\.]+)\s*$/nm and @duration = $1.to_f # TODO: multi-line/multi-value/repeated tags - f.gsub!(/^TAG:([^=]+)=(.*)$/ni) { |_| @comments[$1.upcase.freeze] = $2 } + f.gsub!(/^TAG:([^=]+)=(.*)$/ni) { |_| + @comments[DTAS.dedupe_str($1.upcase)] = DTAS.dedupe_str($2) + } end # new avprobe s.scan(%r{^\[format\.tags\]\n(.*?)\n\n}m) do |_| f = $1.dup - f.gsub!(/^([^=]+)=(.*)$/ni) { |_| @comments[$1.upcase.freeze] = $2 } + f.gsub!(/^([^=]+)=(.*)$/ni) { |_| + @comments[DTAS.dedupe_str($1.upcase)] = DTAS.dedupe_str($2) + } end s.scan(%r{^\[format\]\n(.*?)\n\n}m) do |_| f = $1.dup diff --git a/lib/dtas/source/sox.rb b/lib/dtas/source/sox.rb index 7f55c5d..f702b41 100644 --- a/lib/dtas/source/sox.rb +++ b/lib/dtas/source/sox.rb @@ -52,19 +52,16 @@ class DTAS::Source::Sox # :nodoc: if out =~ /\nComments\s*:[ \t]*\n?(.*)\z/mn comments = dst['comments'] = {} - # we use eval "#{str.inspect}".freeze - # take advantage of the VM-wide dedupe in MRI (rb_fstring): key = nil $1.split(/\n/n).each do |line| if line.sub!(/^([^=]+)=/ni, '') - key = $1.upcase - key = eval "#{key.inspect}.freeze" + key = DTAS.dedupe_str($1.upcase) end (comments[key] ||= ''.b) << "#{line}\n" unless line.empty? end comments.each do |k,v| v.chomp! - comments[k] = eval "#{v.inspect}.freeze" + comments[k] = DTAS.dedupe_str(v) end end dst -- cgit v1.2.3-24-ge0c7