Skip to content

Commit

Permalink
individual interpolation method performance tunings
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dgynn committed Nov 15, 2015
1 parent 18e1c5a commit 5584734
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
12 changes: 6 additions & 6 deletions lib/paperclip/interpolations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def self.plural_cache

# Returns the filename, the same way as ":basename.:extension" would.
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

# Returns the interpolated URL. Will raise an error if the url itself
Expand Down Expand Up @@ -95,23 +95,23 @@ def class attachment = nil, style_name = nil

# Returns the basename of the file. e.g. "file" for "file.jpg"
def basename attachment, style_name
attachment.original_filename.gsub(/#{Regexp.escape(File.extname(attachment.original_filename))}\Z/, "")
File.basename(attachment.original_filename, ".*".freeze)
end

# Returns the extension of the file. e.g. "jpg" for "file.jpg"
# If the style has a format defined, it will return the format instead
# of the actual extension.
def extension attachment, style_name
((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

# Returns the dot+extension of the file. e.g. ".jpg" for "file.jpg"
# If the style has a format defined, it will return the format instead
# of the actual extension. If the extension is empty, no dot is added.
def dotextension attachment, style_name
ext = extension(attachment, style_name)
ext.empty? ? "" : ".#{ext}"
ext.empty? ? ext : ".#{ext}"
end

# Returns an extension based on the content type. e.g. "jpeg" for
Expand Down Expand Up @@ -175,9 +175,9 @@ def hash attachment=nil, style_name=nil
def id_partition attachment, style_name
case id = attachment.instance.id
when Integer
("%09d" % id).scan(/\d{3}/).join("/")
("%09d".freeze % id).scan(/\d{3}/).join("/".freeze)
when String
id.scan(/.{3}/).first(3).join("/")
id.scan(/.{3}/).first(3).join("/".freeze)
else
nil
end
Expand Down
12 changes: 6 additions & 6 deletions lib/paperclip/storage/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def sanitize_hash(hash)
Proc.new do |style, attachment|
permission = (@s3_permissions[style.to_s.to_sym] || @s3_permissions[:default])
permission = permission.call(attachment, style) if permission.respond_to?(:call)
(permission == :public_read) ? 'http' : 'https'
(permission == :public_read) ? 'http'.freeze : 'https'.freeze
end
@s3_metadata = @options[:s3_metadata] || {}
@s3_headers = {}
Expand All @@ -167,16 +167,16 @@ def sanitize_hash(hash)
end

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
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
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
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

Expand All @@ -197,7 +197,7 @@ def s3_host_name
host_name = @options[:s3_host_name]
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

def s3_host_alias
Expand Down
6 changes: 3 additions & 3 deletions spec/paperclip/interpolations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Thing ; end

it "returns the basename of the file" do
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)
end

Expand Down Expand Up @@ -188,14 +188,14 @@ def url(*args)
it "returns the filename as basename.extension" do
attachment = mock
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)
end

it "returns the filename as basename.extension when format supplied" do
attachment = mock
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)
end

Expand Down

0 comments on commit 5584734

Please sign in to comment.