Skip to content
This repository was archived by the owner on Jul 10, 2018. It is now read-only.

Commit 5584734

Browse files
committed
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.
1 parent 18e1c5a commit 5584734

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

lib/paperclip/interpolations.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def self.plural_cache
4747

4848
# Returns the filename, the same way as ":basename.:extension" would.
4949
def filename attachment, style_name
50-
[ basename(attachment, style_name), extension(attachment, style_name) ].reject(&:blank?).join(".")
50+
[ basename(attachment, style_name), extension(attachment, style_name) ].delete_if(&:empty?).join(".".freeze)
5151
end
5252

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

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

101101
# Returns the extension of the file. e.g. "jpg" for "file.jpg"
102102
# If the style has a format defined, it will return the format instead
103103
# of the actual extension.
104104
def extension attachment, style_name
105105
((style = attachment.styles[style_name.to_s.to_sym]) && style[:format]) ||
106-
File.extname(attachment.original_filename).gsub(/\A\.+/, "")
106+
File.extname(attachment.original_filename).sub(/\A\.+/, "".freeze)
107107
end
108108

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

117117
# Returns an extension based on the content type. e.g. "jpeg" for
@@ -175,9 +175,9 @@ def hash attachment=nil, style_name=nil
175175
def id_partition attachment, style_name
176176
case id = attachment.instance.id
177177
when Integer
178-
("%09d" % id).scan(/\d{3}/).join("/")
178+
("%09d".freeze % id).scan(/\d{3}/).join("/".freeze)
179179
when String
180-
id.scan(/.{3}/).first(3).join("/")
180+
id.scan(/.{3}/).first(3).join("/".freeze)
181181
else
182182
nil
183183
end

lib/paperclip/storage/s3.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def sanitize_hash(hash)
141141
Proc.new do |style, attachment|
142142
permission = (@s3_permissions[style.to_s.to_sym] || @s3_permissions[:default])
143143
permission = permission.call(attachment, style) if permission.respond_to?(:call)
144-
(permission == :public_read) ? 'http' : 'https'
144+
(permission == :public_read) ? 'http'.freeze : 'https'.freeze
145145
end
146146
@s3_metadata = @options[:s3_metadata] || {}
147147
@s3_headers = {}
@@ -167,16 +167,16 @@ def sanitize_hash(hash)
167167
end
168168

169169
Paperclip.interpolates(:s3_alias_url) do |attachment, style|
170-
"#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{\A/}, "")}"
170+
"#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{\A/}, "".freeze)}"
171171
end unless Paperclip::Interpolations.respond_to? :s3_alias_url
172172
Paperclip.interpolates(:s3_path_url) do |attachment, style|
173-
"#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{\A/}, "")}"
173+
"#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{\A/}, "".freeze)}"
174174
end unless Paperclip::Interpolations.respond_to? :s3_path_url
175175
Paperclip.interpolates(:s3_domain_url) do |attachment, style|
176-
"#{attachment.s3_protocol(style, true)}//#{attachment.bucket_name}.#{attachment.s3_host_name}/#{attachment.path(style).gsub(%r{\A/}, "")}"
176+
"#{attachment.s3_protocol(style, true)}//#{attachment.bucket_name}.#{attachment.s3_host_name}/#{attachment.path(style).gsub(%r{\A/}, "".freeze)}"
177177
end unless Paperclip::Interpolations.respond_to? :s3_domain_url
178178
Paperclip.interpolates(:asset_host) do |attachment, style|
179-
"#{attachment.path(style).gsub(%r{\A/}, "")}"
179+
"#{attachment.path(style).gsub(%r{\A/}, "".freeze)}"
180180
end unless Paperclip::Interpolations.respond_to? :asset_host
181181
end
182182

@@ -197,7 +197,7 @@ def s3_host_name
197197
host_name = @options[:s3_host_name]
198198
host_name = host_name.call(self) if host_name.is_a?(Proc)
199199

200-
host_name || s3_credentials[:s3_host_name] || "s3.amazonaws.com"
200+
host_name || s3_credentials[:s3_host_name] || "s3.amazonaws.com".freeze
201201
end
202202

203203
def s3_host_alias

spec/paperclip/interpolations_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Thing ; end
3333

3434
it "returns the basename of the file" do
3535
attachment = mock
36-
attachment.expects(:original_filename).returns("one.jpg").times(2)
36+
attachment.expects(:original_filename).returns("one.jpg").times(1)
3737
assert_equal "one", Paperclip::Interpolations.basename(attachment, :style)
3838
end
3939

@@ -188,14 +188,14 @@ def url(*args)
188188
it "returns the filename as basename.extension" do
189189
attachment = mock
190190
attachment.expects(:styles).returns({})
191-
attachment.expects(:original_filename).returns("one.jpg").times(3)
191+
attachment.expects(:original_filename).returns("one.jpg").times(2)
192192
assert_equal "one.jpg", Paperclip::Interpolations.filename(attachment, :style)
193193
end
194194

195195
it "returns the filename as basename.extension when format supplied" do
196196
attachment = mock
197197
attachment.expects(:styles).returns({style: {format: :png}})
198-
attachment.expects(:original_filename).returns("one.jpg").times(2)
198+
attachment.expects(:original_filename).returns("one.jpg").times(1)
199199
assert_equal "one.png", Paperclip::Interpolations.filename(attachment, :style)
200200
end
201201

0 commit comments

Comments
 (0)