From 7229d2553e8bd97e837f7bdc677962fe906dc1a7 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Sat, 28 Oct 2023 14:57:26 -0600 Subject: [PATCH] use a hash set for unread topics The "u" or "unread" command diffs two sets stored on disk: all topics, and unread messages. A flat array was previously used for both, but this resulted in slow O(n^2) behavior. This diff turns the unread messaegs into a hash set and uses that for the "contains" test in the loop over all topics. --- iris.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/iris.rb b/iris.rb index d6f85cb..17eb8a5 100755 --- a/iris.rb +++ b/iris.rb @@ -4,6 +4,7 @@ require 'digest' require 'etc' require 'json' require 'readline' +require 'set' require 'tempfile' require 'time' # require 'pry' # Only needed for debugging @@ -234,6 +235,10 @@ class Corpus end end + def self.unread_topics_set + unread_topics.map(&:hash).to_set + end + def self.size @@corpus.size end @@ -927,8 +932,9 @@ class Interface Display.say Display.topic_header # TODO: Paginate here + unread_hashes = Corpus.unread_topics_set Corpus.topics.each_with_index do |topic, index| - if Corpus.unread_topics.include?(topic) + if unread_hashes.include?(topic.hash) Display.say topic.to_topic_line(index + 1) end end