everything related to duct tape audio suite (dtas)
 help / color / Atom feed
* [PATCH] tracklist: avoid needlessly building a hash for track IDs
@ 2015-12-06  2:44 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2015-12-06  2:44 UTC (permalink / raw)
  To: dtas-all

Building this hash is a linear operation anyways,
so there's no point in doing it when Array#index can stop
early if the track is found, to avoid unnecessary work.
---
 lib/dtas/tracklist.rb | 36 +++++++++++++-----------------------
 1 file changed, 13 insertions(+), 23 deletions(-)

diff --git a/lib/dtas/tracklist.rb b/lib/dtas/tracklist.rb
index 1750f26..1ae2719 100644
--- a/lib/dtas/tracklist.rb
+++ b/lib/dtas/tracklist.rb
@@ -56,25 +56,14 @@ class DTAS::Tracklist # :nodoc:
     @pos = TL_DEFAULTS["pos"]
   end
 
-  def size
-    @list.size
-  end
-
-  # caching this probably isn't worth it.  a tracklist is usually
-  # a few tens of tracks, maybe a hundred at most.
-  def _track_id_map
-    by_track_id = {}
-    @list.each_with_index { |t,i| by_track_id[t.track_id] = i }
-    by_track_id
-  end
-
   def get_tracks(track_ids)
-    by_track_id = _track_id_map
-    track_ids.map do |track_id|
-      idx = by_track_id[track_id]
-      # dtas-mpris fills in the metadata, we just return a path
-      [ track_id, idx ? @list[idx].to_path : nil ]
+    want = {}
+    track_ids.each { |i| want[i] = i }
+    rv = []
+    @list.each do |t|
+      i = want[t.track_id] and rv << [ i, t.to_path ]
     end
+    rv
   end
 
   def tracks
@@ -105,8 +94,7 @@ class DTAS::Tracklist # :nodoc:
   def add_track(track, after_track_id = nil, set_as_current = false)
     track = new_track(track)
     if after_track_id
-      by_track_id = _track_id_map
-      idx = by_track_id[after_track_id] or
+      idx = _idx_of(after_track_id) or
         raise ArgumentError, "after_track_id invalid"
       @list[idx, 1] = [ @list[idx], track ]
       if set_as_current
@@ -125,9 +113,12 @@ class DTAS::Tracklist # :nodoc:
     track.track_id
   end
 
+  def _idx_of(track_id)
+    @list.index { |t| t.track_id == track_id }
+  end
+
   def remove_track(track_id)
-    by_track_id = _track_id_map
-    idx = by_track_id.delete(track_id) or return false
+    idx = _idx_of(track_id) or return false
     track = @list.delete_at(idx)
     len = @list.size
     if @pos >= len
@@ -138,8 +129,7 @@ class DTAS::Tracklist # :nodoc:
   end
 
   def go_to(track_id, offset_hhmmss = nil)
-    by_track_id = _track_id_map
-    if idx = by_track_id[track_id]
+    if idx = _idx_of(track_id)
       @goto_off = offset_hhmmss
       return @list[@goto_pos = idx].to_path
     end
-- 
EW



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, back to index

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-12-06  2:44 [PATCH] tracklist: avoid needlessly building a hash for track IDs Eric Wong

everything related to duct tape audio suite (dtas)

Archives are clonable:
	git clone --mirror https://80x24.org/dtas-all
	git clone --mirror http://ou63pmih66umazou.onion/dtas-all

Example config snippet for mirrors

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.audio.dtas
	nntp://ou63pmih66umazou.onion/inbox.comp.audio.dtas

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/ public-inbox