Archive of RubyForge sup-talk mailing list
 help / color / mirror / Atom feed
From: rlane@club.cc.cmu.edu (Rich Lane)
Subject: [sup-talk] [PATCH] cache results of Person.from_address
Date: Sun, 16 Aug 2009 23:39:32 -0700	[thread overview]
Message-ID: <1250491172-19317-1-git-send-email-rlane@club.cc.cmu.edu> (raw)

The regexes in this function are very expensive, so caching improves
performance significantly for queries and slightly for indexing.
---
 lib/sup/cache.rb  |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 lib/sup/person.rb |    7 ++++++-
 2 files changed, 52 insertions(+), 1 deletions(-)
 create mode 100644 lib/sup/cache.rb

diff --git a/lib/sup/cache.rb b/lib/sup/cache.rb
new file mode 100644
index 0000000..0836dbd
--- /dev/null
+++ b/lib/sup/cache.rb
@@ -0,0 +1,46 @@
+class Cache
+  def initialize n=128, i=3
+    @n = n
+    @i = i
+    @values = {}
+    @marks = {}
+    @delete_stack = []
+  end
+
+  def [](k)
+    if @values.member? k
+      @marks[k] = @i
+      @values[k]
+    else
+      nil
+    end
+  end
+
+  def []=(k,v)
+    if @values.size < @n
+      @values[k] = v
+      @marks[k] = @i
+    else
+      if @delete_stack.empty?
+        sweep
+      else
+        k2 = @delete_stack.pop
+        @values.delete k2
+        @marks.delete k2
+        self[k] = v
+      end
+    end
+  end
+
+  def sweep
+    @marks.each do |k,v|
+      v -= 1
+      if v == 0
+        @delete_stack.push k
+        @marks.delete k
+      else
+        @marks[k] = v
+      end
+    end
+  end
+end
diff --git a/lib/sup/person.rb b/lib/sup/person.rb
index c4f40a5..046eedc 100644
--- a/lib/sup/person.rb
+++ b/lib/sup/person.rb
@@ -1,3 +1,5 @@
+require 'sup/cache'
+
 module Redwood
 
 class Person 
@@ -73,8 +75,11 @@ class Person
     end.downcase
   end
 
+  ## This method is expensive, so memoize it.
+  @from_address_cache = Cache.new
   def self.from_address s
     return nil if s.nil?
+    @from_address_cache[s].tap { |x| return x if x }
 
     ## try and parse an email address and name
     name, email = case s
@@ -102,7 +107,7 @@ class Person
         [nil, s]
       end
 
-    Person.new name, email
+    @from_address_cache[s] = Person.new name, email
   end
 
   def self.from_address_list ss
-- 
1.6.4



             reply	other threads:[~2009-08-17  6:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-17  6:39 Rich Lane [this message]
2009-08-17 13:07 ` Andrei Thorp
2009-08-22 14:10   ` William Morgan
2009-08-22 14:10 ` William Morgan
2009-08-22 18:28   ` Rich Lane

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

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

  git send-email \
    --in-reply-to=1250491172-19317-1-git-send-email-rlane@club.cc.cmu.edu \
    --to=rlane@club.cc.cmu.edu \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox