Commit 55847347 by Dave Gynn

individual interpolation method performance tunings

this commit primarily uses frozen strings to reduce object creation during interpolation.
the :basename method now uses File.basename(file, ".*") rather than a Regexp. basename may be called multiple times.
parent 18e1c5a2
...@@ -47,7 +47,7 @@ module Paperclip ...@@ -47,7 +47,7 @@ module Paperclip
# Returns the filename, the same way as ":basename.:extension" would. # Returns the filename, the same way as ":basename.:extension" would.
def filename attachment, style_name def filename attachment, style_name
[ basename(attachment, style_name), extension(attachment, style_name) ].reject(&:blank?).join(".") [ basename(attachment, style_name), extension(attachment, style_name) ].delete_if(&:empty?).join(".".freeze)
end end
# Returns the interpolated URL. Will raise an error if the url itself # Returns the interpolated URL. Will raise an error if the url itself
...@@ -95,7 +95,7 @@ module Paperclip ...@@ -95,7 +95,7 @@ module Paperclip
# Returns the basename of the file. e.g. "file" for "file.jpg" # Returns the basename of the file. e.g. "file" for "file.jpg"
def basename attachment, style_name def basename attachment, style_name
attachment.original_filename.gsub(/#{Regexp.escape(File.extname(attachment.original_filename))}\Z/, "") File.basename(attachment.original_filename, ".*".freeze)
end end
# Returns the extension of the file. e.g. "jpg" for "file.jpg" # Returns the extension of the file. e.g. "jpg" for "file.jpg"
...@@ -103,7 +103,7 @@ module Paperclip ...@@ -103,7 +103,7 @@ module Paperclip
# of the actual extension. # of the actual extension.
def extension attachment, style_name def extension attachment, style_name
((style = attachment.styles[style_name.to_s.to_sym]) && style[:format]) || ((style = attachment.styles[style_name.to_s.to_sym]) && style[:format]) ||
File.extname(attachment.original_filename).gsub(/\A\.+/, "") File.extname(attachment.original_filename).sub(/\A\.+/, "".freeze)
end end
# Returns the dot+extension of the file. e.g. ".jpg" for "file.jpg" # Returns the dot+extension of the file. e.g. ".jpg" for "file.jpg"
...@@ -111,7 +111,7 @@ module Paperclip ...@@ -111,7 +111,7 @@ module Paperclip
# of the actual extension. If the extension is empty, no dot is added. # of the actual extension. If the extension is empty, no dot is added.
def dotextension attachment, style_name def dotextension attachment, style_name
ext = extension(attachment, style_name) ext = extension(attachment, style_name)
ext.empty? ? "" : ".#{ext}" ext.empty? ? ext : ".#{ext}"
end end
# Returns an extension based on the content type. e.g. "jpeg" for # Returns an extension based on the content type. e.g. "jpeg" for
...@@ -175,9 +175,9 @@ module Paperclip ...@@ -175,9 +175,9 @@ module Paperclip
def id_partition attachment, style_name def id_partition attachment, style_name
case id = attachment.instance.id case id = attachment.instance.id
when Integer when Integer
("%09d" % id).scan(/\d{3}/).join("/") ("%09d".freeze % id).scan(/\d{3}/).join("/".freeze)
when String when String
id.scan(/.{3}/).first(3).join("/") id.scan(/.{3}/).first(3).join("/".freeze)
else else
nil nil
end end
......
...@@ -141,7 +141,7 @@ module Paperclip ...@@ -141,7 +141,7 @@ module Paperclip
Proc.new do |style, attachment| Proc.new do |style, attachment|
permission = (@s3_permissions[style.to_s.to_sym] || @s3_permissions[:default]) permission = (@s3_permissions[style.to_s.to_sym] || @s3_permissions[:default])
permission = permission.call(attachment, style) if permission.respond_to?(:call) permission = permission.call(attachment, style) if permission.respond_to?(:call)
(permission == :public_read) ? 'http' : 'https' (permission == :public_read) ? 'http'.freeze : 'https'.freeze
end end
@s3_metadata = @options[:s3_metadata] || {} @s3_metadata = @options[:s3_metadata] || {}
@s3_headers = {} @s3_headers = {}
...@@ -167,16 +167,16 @@ module Paperclip ...@@ -167,16 +167,16 @@ module Paperclip
end end
Paperclip.interpolates(:s3_alias_url) do |attachment, style| Paperclip.interpolates(:s3_alias_url) do |attachment, style|
"#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{\A/}, "")}" "#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{\A/}, "".freeze)}"
end unless Paperclip::Interpolations.respond_to? :s3_alias_url end unless Paperclip::Interpolations.respond_to? :s3_alias_url
Paperclip.interpolates(:s3_path_url) do |attachment, style| Paperclip.interpolates(:s3_path_url) do |attachment, style|
"#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{\A/}, "")}" "#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{\A/}, "".freeze)}"
end unless Paperclip::Interpolations.respond_to? :s3_path_url end unless Paperclip::Interpolations.respond_to? :s3_path_url
Paperclip.interpolates(:s3_domain_url) do |attachment, style| Paperclip.interpolates(:s3_domain_url) do |attachment, style|
"#{attachment.s3_protocol(style, true)}//#{attachment.bucket_name}.#{attachment.s3_host_name}/#{attachment.path(style).gsub(%r{\A/}, "")}" "#{attachment.s3_protocol(style, true)}//#{attachment.bucket_name}.#{attachment.s3_host_name}/#{attachment.path(style).gsub(%r{\A/}, "".freeze)}"
end unless Paperclip::Interpolations.respond_to? :s3_domain_url end unless Paperclip::Interpolations.respond_to? :s3_domain_url
Paperclip.interpolates(:asset_host) do |attachment, style| Paperclip.interpolates(:asset_host) do |attachment, style|
"#{attachment.path(style).gsub(%r{\A/}, "")}" "#{attachment.path(style).gsub(%r{\A/}, "".freeze)}"
end unless Paperclip::Interpolations.respond_to? :asset_host end unless Paperclip::Interpolations.respond_to? :asset_host
end end
...@@ -197,7 +197,7 @@ module Paperclip ...@@ -197,7 +197,7 @@ module Paperclip
host_name = @options[:s3_host_name] host_name = @options[:s3_host_name]
host_name = host_name.call(self) if host_name.is_a?(Proc) host_name = host_name.call(self) if host_name.is_a?(Proc)
host_name || s3_credentials[:s3_host_name] || "s3.amazonaws.com" host_name || s3_credentials[:s3_host_name] || "s3.amazonaws.com".freeze
end end
def s3_host_alias def s3_host_alias
......
...@@ -33,7 +33,7 @@ describe Paperclip::Interpolations do ...@@ -33,7 +33,7 @@ describe Paperclip::Interpolations do
it "returns the basename of the file" do it "returns the basename of the file" do
attachment = mock attachment = mock
attachment.expects(:original_filename).returns("one.jpg").times(2) attachment.expects(:original_filename).returns("one.jpg").times(1)
assert_equal "one", Paperclip::Interpolations.basename(attachment, :style) assert_equal "one", Paperclip::Interpolations.basename(attachment, :style)
end end
...@@ -188,14 +188,14 @@ describe Paperclip::Interpolations do ...@@ -188,14 +188,14 @@ describe Paperclip::Interpolations do
it "returns the filename as basename.extension" do it "returns the filename as basename.extension" do
attachment = mock attachment = mock
attachment.expects(:styles).returns({}) attachment.expects(:styles).returns({})
attachment.expects(:original_filename).returns("one.jpg").times(3) attachment.expects(:original_filename).returns("one.jpg").times(2)
assert_equal "one.jpg", Paperclip::Interpolations.filename(attachment, :style) assert_equal "one.jpg", Paperclip::Interpolations.filename(attachment, :style)
end end
it "returns the filename as basename.extension when format supplied" do it "returns the filename as basename.extension when format supplied" do
attachment = mock attachment = mock
attachment.expects(:styles).returns({style: {format: :png}}) attachment.expects(:styles).returns({style: {format: :png}})
attachment.expects(:original_filename).returns("one.jpg").times(2) attachment.expects(:original_filename).returns("one.jpg").times(1)
assert_equal "one.png", Paperclip::Interpolations.filename(attachment, :style) assert_equal "one.png", Paperclip::Interpolations.filename(attachment, :style)
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