Merge pull request #3022 from jekyll/perf

This commit is contained in:
Parker Moore 2014-10-24 11:34:41 -07:00
commit 578f38748d
12 changed files with 99 additions and 15 deletions

17
benchmark/flat-map Normal file
View File

@ -0,0 +1,17 @@
require 'benchmark/ips'
enum = (0..50).to_a
nested = enum.map { |i| [i] }
def do_thing(blah)
blah * 1
end
Benchmark.ips do |x|
x.report('.map.flatten with nested arrays') { nested.map { |i| do_thing(i) }.flatten(1) }
x.report('.flat_map with nested arrays') { nested.flat_map { |i| do_thing(i) } }
x.report('.map.flatten with no nested arrays') { enum.map { |i| do_thing(i) }.flatten(1) }
x.report('.flat_map with no nested arrays') { enum.flat_map { |i| do_thing(i) } }
end

9
benchmark/hash-fetch Normal file
View File

@ -0,0 +1,9 @@
require 'benchmark/ips'
h = {:bar => 'uco'}
Benchmark.ips do |x|
x.report('fetch with no block') { h.fetch(:bar, (0..9).to_a) }
x.report('fetch with a block') { h.fetch(:bar) { (0..9).to_a } }
x.report('brackets with an ||') { h[:bar] || (0..9).to_a }
end

View File

@ -0,0 +1,14 @@
require 'benchmark/ips'
def fast
yield
end
def slow(&block)
block.call
end
Benchmark.ips do |x|
x.report('yield') { fast { (0..9).to_a } }
x.report('block.call') { slow { (0..9).to_a } }
end

View File

@ -0,0 +1,11 @@
require 'benchmark/ips'
Benchmark.ips do |x|
x.report('parallel assignment') do
a, b = 1, 2
end
x.report('multi-line assignment') do
a = 1
b = 2
end
end

8
benchmark/string-concat Normal file
View File

@ -0,0 +1,8 @@
require 'benchmark/ips'
url = "http://jekyllrb.com"
Benchmark.ips do |x|
x.report('+=') { url += '/' }
x.report('<<') { url << '/' }
end

View File

@ -0,0 +1,13 @@
require 'benchmark/ips'
def str
'http://baruco.org/2014/some-talk-with-some-amount-of-value'
end
Benchmark.ips do |x|
x.report('#tr') { str.tr('some', 'a') }
x.report('#gsub') { str.gsub('some', 'a') }
x.report('#gsub!') { str.gsub!('some', 'a') }
x.report('#sub') { str.sub('some', 'a') }
x.report('#sub!') { str.sub!('some', 'a') }
end

6
benchmark/symbol-to-proc Normal file
View File

@ -0,0 +1,6 @@
require 'benchmark/ips'
Benchmark.ips do |x|
x.report('block') { (1..100).map { |i| i.to_s } }
x.report('&:to_s') { (1..100).map(&:to_s) }
end

View File

@ -76,7 +76,7 @@ module Jekyll
#
# Returns a Set with the directory paths
def keep_dirs
site.keep_files.map{|file| parent_dirs(File.join(site.dest, file))}.flatten.to_set
site.keep_files.map { |file| parent_dirs(File.join(site.dest, file)) }.flatten.to_set
end
# Private: Creates a regular expression from the config's keep_files array

View File

@ -126,7 +126,7 @@ module Jekyll
#
# Returns the path to the source file
def path
data.fetch('path', relative_path.sub(/\A\//, ''))
data.fetch('path') { relative_path.sub(/\A\//, '') }
end
# The path to the page source file, relative to the site source

View File

@ -108,14 +108,14 @@ module Jekyll
#
# Returns excerpt string.
def excerpt
data.fetch('excerpt', extracted_excerpt.to_s)
data.fetch('excerpt') { extracted_excerpt.to_s }
end
# Public: the Post title, from the YAML Front-Matter or from the slug
#
# Returns the post title
def title
data.fetch("title", titleized_slug)
data.fetch('title') { titleized_slug }
end
# Turns the post slug into a suitable title
@ -130,7 +130,7 @@ module Jekyll
#
# Returns the path to the file relative to the site source
def path
data.fetch('path', relative_path.sub(/\A\//, ''))
data.fetch('path') { relative_path.sub(/\A\//, '') }
end
# The path to the post source file, relative to the site source

View File

@ -46,24 +46,23 @@ module Jekyll
# Returns the _unsanitizied_ String URL
def generate_url
@placeholders.inject(@template) do |result, token|
break result if result.index(':').nil?
result.gsub(/:#{token.first}/, self.class.escape_path(token.last))
end
end
# Returns a sanitized String URL
def sanitize_url(in_url)
# Remove all double slashes
url = in_url.gsub(/\/\//, "/")
# Remove every URL segment that consists solely of dots
url = url.split('/').reject{ |part| part =~ /^\.+$/ }.join('/')
url = in_url \
# Remove all double slashes
.gsub(/\/\//, '/') \
# Remove every URL segment that consists solely of dots
.split('/').reject{ |part| part =~ /^\.+$/ }.join('/') \
# Always add a leading slash
.gsub(/\A([^\/])/, '/\1')
# Append a trailing slash to the URL if the unsanitized URL had one
url += "/" if in_url =~ /\/$/
# Always add a leading slash
url.gsub!(/\A([^\/])/, '/\1')
url << "/" if in_url[-1].eql?('/')
url
end

View File

@ -16,6 +16,13 @@ class TestURL < Test::Unit::TestCase
).to_s
end
should "handle multiple of the same key in the template" do
assert_equal '/foo/bar/foo/', URL.new(
:template => "/:x/:y/:x/",
:placeholders => {:x => "foo", :y => "bar"}
).to_s
end
should "return permalink if given" do
assert_equal "/le/perma/link", URL.new(
:template => "/:x/:y",