From 5111742a5a100e44fccf191b12a13c452b025944 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 20 Jan 2022 18:34:17 +0000 Subject: get rid of DTAS.dedupe_str wrapper Ruby 2.3+ supports String#@-, though it did not deduplicate strings. But 2.5 is already old at this point and most users can be expected to have it. This gives some memory regressions for Ruby <= 2.4 users, but cuts down on the code we maintain and reduces bytecode overhead for 2.5+ users. --- lib/dtas.rb | 13 ------------- lib/dtas/fadefx.rb | 4 ++-- lib/dtas/mlib.rb | 12 +++++------- lib/dtas/partstats.rb | 2 +- lib/dtas/player.rb | 4 ++-- lib/dtas/player/client_handler.rb | 6 +++--- lib/dtas/rg_state.rb | 4 ++-- lib/dtas/source/av_ff_common.rb | 6 +++--- lib/dtas/source/sox.rb | 6 +++--- lib/dtas/watchable/fiddle_ino.rb | 4 ++-- 10 files changed, 23 insertions(+), 38 deletions(-) diff --git a/lib/dtas.rb b/lib/dtas.rb index eb8f49d..477a176 100644 --- a/lib/dtas.rb +++ b/lib/dtas.rb @@ -25,19 +25,6 @@ module DTAS Fiddle.dlopen(nil) end end - - # String#-@ will deduplicate strings when Ruby 2.5 is released (Dec 2017) - # https://bugs.ruby-lang.org/issues/13077 - if RUBY_VERSION.to_f >= 2.5 - def self.dedupe_str(str) - -str - end - else - # Ruby 2.1 - 2.4, noop for older Rubies - def self.dedupe_str(str) - eval "#{str.inspect}.freeze" - end - end # :startdoc: end diff --git a/lib/dtas/fadefx.rb b/lib/dtas/fadefx.rb index 7bccff8..0ec108c 100644 --- a/lib/dtas/fadefx.rb +++ b/lib/dtas/fadefx.rb @@ -1,4 +1,4 @@ -# Copyright (C) 2013-2020 all contributors +# Copyright (C) all contributors # License: GPL-3.0+ # frozen_string_literal: true require_relative '../dtas' @@ -95,7 +95,7 @@ class DTAS::FadeFX # :nodoc: def parse!(str) return nil if str.empty? type = "t" - str.sub!(/\A([a-z])/, "") and type = DTAS.dedupe_str($1) + str.sub!(/\A([a-z])/, "") and type = -$1 F.new(type, parse_time(str)) end end diff --git a/lib/dtas/mlib.rb b/lib/dtas/mlib.rb index eb7554a..f99ed6a 100644 --- a/lib/dtas/mlib.rb +++ b/lib/dtas/mlib.rb @@ -1,5 +1,5 @@ # -*- encoding: utf-8 -*- -# Copyright (C) 2015-2021 all contributors +# Copyright (C) all contributors # License: GPL-3.0+ # frozen_string_literal: true # @@ -201,9 +201,7 @@ class DTAS::Mlib # :nodoc: tag_id = tag_map[x] and tag_map["#{x}number"] = tag_id end @tag_rmap = tag_map.invert.freeze - tag_map.merge!(Hash[*(tag_map.map { |k,v| - [DTAS.dedupe_str(k.upcase), v] - }.flatten!)]) + tag_map.merge!(Hash[*(tag_map.map { |k,v| [-(k.upcase), v] }.flatten!)]) @tag_map = tag_map.freeze end @@ -421,7 +419,7 @@ class DTAS::Mlib # :nodoc: return '/' if base == '' # root_node parent_id = node[:parent_id] base += '/' unless node[:tlen] >= 0 - ppath = cache[parent_id] and return DTAS.dedupe_str("#{ppath}/#{base}") + ppath = cache[parent_id] and return -"#{ppath}/#{base}" parts = [] begin node = @db[:nodes][id: node[:parent_id]] @@ -429,9 +427,9 @@ class DTAS::Mlib # :nodoc: parts.unshift node[:name] end while true parts.unshift('') - cache[parent_id] = DTAS.dedupe_str(parts.join('/')) + cache[parent_id] = -(parts.join('/')) parts << base - DTAS.dedupe_str(parts.join('/')) + -(parts.join('/')) end def emit_recurse(node, cache, cb) diff --git a/lib/dtas/partstats.rb b/lib/dtas/partstats.rb index 8e9ee95..061ff50 100644 --- a/lib/dtas/partstats.rb +++ b/lib/dtas/partstats.rb @@ -171,7 +171,7 @@ becomes: else next end - key = DTAS.dedupe_str($1) + key = -$1 key_idx = @key_idx[key] parts = line.split(/\s+/) nshift.times { parts.shift } # remove stuff we don't need diff --git a/lib/dtas/player.rb b/lib/dtas/player.rb index b39a2e7..ff7440f 100644 --- a/lib/dtas/player.rb +++ b/lib/dtas/player.rb @@ -1,4 +1,4 @@ -# Copyright (C) 2013-2020 all contributors +# Copyright (C) all contributors # License: GPL-3.0+ # frozen_string_literal: true require 'yaml' @@ -168,7 +168,7 @@ class DTAS::Player # :nodoc: if sinks = hash["sinks"] sinks.each do |sink_hsh| - sink_hsh['name'] = DTAS.dedupe_str(sink_hsh['name']) + sink_hsh['name'] = -sink_hsh['name'] sink = DTAS::Sink.load(sink_hsh) sink.env = to_omap(sink.env) @sinks[sink.name] = sink diff --git a/lib/dtas/player/client_handler.rb b/lib/dtas/player/client_handler.rb index cf5442d..2914fe7 100644 --- a/lib/dtas/player/client_handler.rb +++ b/lib/dtas/player/client_handler.rb @@ -1,4 +1,4 @@ -# Copyright (C) 2013-2020 all contributors +# Copyright (C) all contributors # License: GPL-3.0+ # frozen_string_literal: true require_relative '../xs' @@ -135,7 +135,7 @@ module DTAS::Player::ClientHandler # :nodoc: # or variable names. sink.valid_name?(name) or return io.emit("ERR sink name invalid") - sink.name = DTAS.dedupe_str(name) + sink.name = -name active_before = sink.active before = __sink_snapshot(sink) @@ -144,7 +144,7 @@ module DTAS::Player::ClientHandler # :nodoc: k, v = kv.split('=', 2) case k when %r{\Aenv\.([^=]+)\z} - sink.env[DTAS.dedupe_str($1)] = v + sink.env[$1] = v when %r{\Aenv#([^=]+)\z} v == nil or return io.emit("ERR unset env has no value") sink.env.delete($1) diff --git a/lib/dtas/rg_state.rb b/lib/dtas/rg_state.rb index d463bd8..9a44835 100644 --- a/lib/dtas/rg_state.rb +++ b/lib/dtas/rg_state.rb @@ -1,4 +1,4 @@ -# Copyright (C) 2013-2020 all contributors +# Copyright (C) all contributors # License: GPL-3.0+ # frozen_string_literal: true # @@ -72,7 +72,7 @@ class DTAS::RGState # :nodoc: when 1 then return 'gain 192' else val.abs <= 0.00000001 and return - DTAS.dedupe_str(sprintf('gain %0.8f', val)) + -sprintf('gain %0.8f', val) end end diff --git a/lib/dtas/source/av_ff_common.rb b/lib/dtas/source/av_ff_common.rb index 6f92762..5299fdb 100644 --- a/lib/dtas/source/av_ff_common.rb +++ b/lib/dtas/source/av_ff_common.rb @@ -1,4 +1,4 @@ -# Copyright (C) 2013-2020 all contributors +# Copyright (C) all contributors # License: GPL-3.0+ # frozen_string_literal: true require_relative '../../dtas' @@ -110,7 +110,7 @@ module DTAS::Source::AvFfCommon # :nodoc: f =~ /^duration=([\d\.]+)\s*$/nm and @duration = $1.to_f # TODO: multi-line/multi-value/repeated tags f.gsub!(/^TAG:([^=]+)=(.*)$/ni) { |_| - @comments[DTAS.dedupe_str($1.upcase)] = DTAS.dedupe_str($2) + @comments[$1.upcase] = -($2) } end @@ -118,7 +118,7 @@ module DTAS::Source::AvFfCommon # :nodoc: s.scan(%r{^\[format\.tags\]\n(.*?)\n\n}m) do |_| f = $1.dup f.gsub!(/^([^=]+)=(.*)$/ni) { |_| - @comments[DTAS.dedupe_str($1.upcase)] = DTAS.dedupe_str($2) + @comments[$1.upcase] = -$2 } end s.scan(%r{^\[format\]\n(.*?)\n\n}m) do |_| diff --git a/lib/dtas/source/sox.rb b/lib/dtas/source/sox.rb index 3a7fe7d..e38e23c 100644 --- a/lib/dtas/source/sox.rb +++ b/lib/dtas/source/sox.rb @@ -1,4 +1,4 @@ -# Copyright (C) 2013-2020 all contributors +# Copyright (C) all contributors # License: GPL-3.0+ # frozen_string_literal: true # encoding: binary @@ -56,14 +56,14 @@ class DTAS::Source::Sox # :nodoc: key = nil $1.split(/\n/n).each do |line| if line.sub!(/^([^=]+)=/ni, '') - key = DTAS.dedupe_str(DTAS.try_enc($1.upcase, enc)) + key = DTAS.try_enc($1.upcase, enc) end (comments[key] ||= ''.b) << "#{line}\n" unless line.empty? end comments.each do |k,v| v.chomp! DTAS.try_enc(v, enc) - comments[k] = DTAS.dedupe_str(v) + comments[k] = v end end dst diff --git a/lib/dtas/watchable/fiddle_ino.rb b/lib/dtas/watchable/fiddle_ino.rb index e85fea1..b24bbf6 100644 --- a/lib/dtas/watchable/fiddle_ino.rb +++ b/lib/dtas/watchable/fiddle_ino.rb @@ -1,4 +1,4 @@ -# Copyright (C) 2013-2020 all contributors +# Copyright (C) all contributors # License: GPL-3.0+ # frozen_string_literal: true require 'fiddle' @@ -53,7 +53,7 @@ class DTAS::Watchable::InotifyReadableIter # :nodoc: name.size == len or raise "short name #{name.inspect} != #{len}" name.sub!(/\0+\z/, '') or raise "missing: `\\0', inotify_event.name=#{name.inspect}" - name = DTAS.dedupe_str(name) + name = -name end ie = InotifyEvent.new(wd, mask, cookie, len, name) if event -- cgit v1.2.3-24-ge0c7