Commit 675bf34b by Ben Turner

Use symbol lookup map to avoid cache miss slowness

Without a pre-built lookup, we will look through all existing emojis (~1000) for non-existent emoji cheat codes. This is 30x faster than caching on demand in the presence of non-existent cheat codes, and roughly the same speed as caching on demand when there are no non-existent cheat codes.
parent 9cbd04d2
...@@ -12,7 +12,11 @@ module Rumoji ...@@ -12,7 +12,11 @@ module Rumoji
end end
def symbol def symbol
@cheat_codes.first symbols.first
end
def symbols
@cheat_codes
end end
def code def code
...@@ -20,7 +24,7 @@ module Rumoji ...@@ -20,7 +24,7 @@ module Rumoji
end end
def include?(symbol) def include?(symbol)
@cheat_codes.map(&:to_s).include? symbol.to_s symbols.map(&:to_s).include? symbol.to_s
end end
def to_s def to_s
...@@ -53,11 +57,14 @@ module Rumoji ...@@ -53,11 +57,14 @@ module Rumoji
ALL_REGEXP = Regexp.new(ALL.map(&:string).join('|')) ALL_REGEXP = Regexp.new(ALL.map(&:string).join('|'))
FIND_BY_SYMBOL_CACHE = {} SYMBOL_LOOKUP = ALL.each.with_object({}) do |emoji, lookup|
emoji.symbols.each do |symbol|
lookup[symbol.to_s] = emoji
end
end
def self.find(symbol) def self.find(symbol)
symbol = symbol.to_s SYMBOL_LOOKUP[symbol.to_s]
FIND_BY_SYMBOL_CACHE[symbol] ||= ALL.find { |emoji| emoji.include? symbol }
end end
STRING_LOOKUP = ALL.each.with_object({}) do |emoji, lookup| STRING_LOOKUP = ALL.each.with_object({}) do |emoji, lookup|
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment