Commit 18e1c5a2 by Dave Gynn

optimize Interpolations::PluralCache to require few object creations

by using the attachment name (symbol) and Class as keys we reduce the number of Strings created before hitting the cache
parent 993a47b2
...@@ -68,7 +68,7 @@ module Paperclip ...@@ -68,7 +68,7 @@ module Paperclip
# +url_generator+ - the object used to generate URLs, using the interpolator. Defaults to Paperclip::UrlGenerator # +url_generator+ - the object used to generate URLs, using the interpolator. Defaults to Paperclip::UrlGenerator
# +escape_url+ - Perform URI escaping to URLs. Defaults to true # +escape_url+ - Perform URI escaping to URLs. Defaults to true
def initialize(name, instance, options = {}) def initialize(name, instance, options = {})
@name = name @name = name.to_sym
@instance = instance @instance = instance
options = self.class.default_options.deep_merge(options) options = self.class.default_options.deep_merge(options)
......
...@@ -90,7 +90,7 @@ module Paperclip ...@@ -90,7 +90,7 @@ module Paperclip
# all class names. Calling #class will return the expected class. # all class names. Calling #class will return the expected class.
def class attachment = nil, style_name = nil def class attachment = nil, style_name = nil
return super() if attachment.nil? && style_name.nil? return super() if attachment.nil? && style_name.nil?
plural_cache.underscore_and_pluralize(attachment.instance.class.to_s) plural_cache.underscore_and_pluralize_class(attachment.instance.class)
end end
# Returns the basename of the file. e.g. "file" for "file.jpg" # Returns the basename of the file. e.g. "file" for "file.jpg"
...@@ -186,7 +186,7 @@ module Paperclip ...@@ -186,7 +186,7 @@ module Paperclip
# Returns the pluralized form of the attachment name. e.g. # Returns the pluralized form of the attachment name. e.g.
# "avatars" for an attachment of :avatar # "avatars" for an attachment of :avatar
def attachment attachment, style_name def attachment attachment, style_name
plural_cache.pluralize(attachment.name.to_s.downcase) plural_cache.pluralize_symbol(attachment.name)
end end
# Returns the style, or the default style if nil is supplied. # Returns the style, or the default style if nil is supplied.
......
...@@ -2,15 +2,16 @@ module Paperclip ...@@ -2,15 +2,16 @@ module Paperclip
module Interpolations module Interpolations
class PluralCache class PluralCache
def initialize def initialize
@cache = {} @symbol_cache = {}.compare_by_identity
@klass_cache = {}.compare_by_identity
end end
def pluralize(word) def pluralize_symbol(symbol)
@cache[word] ||= word.pluralize @symbol_cache[symbol] ||= symbol.to_s.downcase.pluralize
end end
def underscore_and_pluralize(word) def underscore_and_pluralize_class(klass)
@cache[word] ||= word.underscore.pluralize @klass_cache[klass] ||= klass.name.underscore.pluralize
end end
end end
end end
......
...@@ -24,9 +24,10 @@ describe Paperclip::Interpolations do ...@@ -24,9 +24,10 @@ describe Paperclip::Interpolations do
end end
it "returns the class of the instance" do it "returns the class of the instance" do
class Thing ; end
attachment = mock attachment = mock
attachment.expects(:instance).returns(attachment) attachment.expects(:instance).returns(attachment)
attachment.expects(:class).returns("Thing") attachment.expects(:class).returns(Thing)
assert_equal "things", Paperclip::Interpolations.class(attachment, :style) assert_equal "things", Paperclip::Interpolations.class(attachment, :style)
end end
......
...@@ -3,34 +3,35 @@ require 'spec_helper' ...@@ -3,34 +3,35 @@ require 'spec_helper'
describe 'Plural cache' do describe 'Plural cache' do
it 'caches pluralizations' do it 'caches pluralizations' do
cache = Paperclip::Interpolations::PluralCache.new cache = Paperclip::Interpolations::PluralCache.new
word = "box" symbol = :box
word.expects(:pluralize).returns("boxes").once first = cache.pluralize_symbol(symbol)
second = cache.pluralize_symbol(symbol)
cache.pluralize(word) expect(first).to equal(second)
cache.pluralize(word)
end end
it 'caches pluralizations and underscores' do it 'caches pluralizations and underscores' do
class BigBox ; end
cache = Paperclip::Interpolations::PluralCache.new cache = Paperclip::Interpolations::PluralCache.new
word = "BigBox" klass = BigBox
word.expects(:pluralize).returns(word).once
word.expects(:underscore).returns(word).once
cache.underscore_and_pluralize(word) first = cache.underscore_and_pluralize_class(klass)
cache.underscore_and_pluralize(word) second = cache.underscore_and_pluralize_class(klass)
expect(first).to equal(second)
end end
it 'pluralizes words' do it 'pluralizes words' do
cache = Paperclip::Interpolations::PluralCache.new cache = Paperclip::Interpolations::PluralCache.new
word = "box" symbol = :box
assert_equal "boxes", cache.pluralize(word)
expect(cache.pluralize_symbol(symbol)).to eq("boxes")
end end
it 'pluralizes and underscore words' do it 'pluralizes and underscore class names' do
class BigBox ; end
cache = Paperclip::Interpolations::PluralCache.new cache = Paperclip::Interpolations::PluralCache.new
word = "BigBox" klass = BigBox
assert_equal "big_boxes", cache.underscore_and_pluralize(word)
expect(cache.underscore_and_pluralize_class(klass)).to eq("big_boxes")
end end
end end
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