everything related to duct tape audio suite (dtas)
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: dtas-all@nongnu.org
Subject: [PATCH 2/4] get rid of DTAS.dedupe_str wrapper
Date: Thu, 20 Jan 2022 18:34:17 +0000	[thread overview]
Message-ID: <20220120183419.2214-3-e@80x24.org> (raw)
In-Reply-To: <20220120183419.2214-1-e@80x24.org>

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 @@ def self.libc
       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 <dtas-all@nongnu.org>
+# Copyright (C) all contributors <dtas-all@nongnu.org>
 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
 # frozen_string_literal: true
 require_relative '../dtas'
@@ -95,7 +95,7 @@ def fade_in_next_fx(format, tbeg, tlen)
   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 <dtas-all@nongnu.org>
+# Copyright (C) all contributors <dtas-all@nongnu.org>
 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
 # frozen_string_literal: true
 #
@@ -201,9 +201,7 @@ def load_tags
       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 @@ def path_of(node, cache)
     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 @@ def path_of(node, cache)
       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 @@ def parse_stats(stats, trim_part, buf)
       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 <dtas-all@nongnu.org>
+# Copyright (C) all contributors <dtas-all@nongnu.org>
 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
 # frozen_string_literal: true
 require 'yaml'
@@ -168,7 +168,7 @@ def self.load(hash)
 
       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 <dtas-all@nongnu.org>
+# Copyright (C) all contributors <dtas-all@nongnu.org>
 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
 # frozen_string_literal: true
 require_relative '../xs'
@@ -135,7 +135,7 @@ def dpc_sink(io, msg)
       # 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 @@ def dpc_sink(io, msg)
         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 <dtas-all@nongnu.org>
+# Copyright (C) all contributors <dtas-all@nongnu.org>
 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
 # frozen_string_literal: true
 #
@@ -72,7 +72,7 @@ def to_sox_gain(val)
     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 <dtas-all@nongnu.org>
+# Copyright (C) all contributors <dtas-all@nongnu.org>
 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
 # frozen_string_literal: true
 require_relative '../../dtas'
@@ -110,7 +110,7 @@ def av_ff_ok?
       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 @@ def av_ff_ok?
     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 <dtas-all@nongnu.org>
+# Copyright (C) all contributors <dtas-all@nongnu.org>
 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
 # frozen_string_literal: true
 # encoding: binary
@@ -56,14 +56,14 @@ def mcache_lookup(infile)
         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 <dtas-all@nongnu.org>
+# Copyright (C) all contributors <dtas-all@nongnu.org>
 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
 # frozen_string_literal: true
 require 'fiddle'
@@ -53,7 +53,7 @@ def take(nonblock) # :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


  parent reply	other threads:[~2022-01-20 22:59 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-20 18:34 [PATCH 0/4] require Ruby 2.3+, support Ruby 3.1 Eric Wong
2022-01-20 18:34 ` [PATCH 1/4] require Ruby 2.3+ Eric Wong
2022-01-20 18:34 ` Eric Wong [this message]
2022-01-20 18:34 ` [PATCH 3/4] move dtas-graph into script/, support Perl for dtas.sh Eric Wong
2022-01-20 18:34 ` [PATCH 4/4] use YAML.unsafe_load in Psych 4.x (Ruby 3.1+) Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://80x24.org/dtas/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220120183419.2214-3-e@80x24.org \
    --to=e@80x24.org \
    --cc=dtas-all@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/dtas.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).