Merge pull request #3022 from jekyll/perf
This commit is contained in:
		
						commit
						578f38748d
					
				| 
						 | 
					@ -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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,8 @@
 | 
				
			||||||
 | 
					require 'benchmark/ips'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					url = "http://jekyllrb.com"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Benchmark.ips do |x|
 | 
				
			||||||
 | 
					  x.report('+=') { url += '/' }
 | 
				
			||||||
 | 
					  x.report('<<') { url << '/' }
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
| 
						 | 
					@ -76,7 +76,7 @@ module Jekyll
 | 
				
			||||||
      #
 | 
					      #
 | 
				
			||||||
      # Returns a Set with the directory paths
 | 
					      # Returns a Set with the directory paths
 | 
				
			||||||
      def keep_dirs
 | 
					      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
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      # Private: Creates a regular expression from the config's keep_files array
 | 
					      # Private: Creates a regular expression from the config's keep_files array
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -126,7 +126,7 @@ module Jekyll
 | 
				
			||||||
    #
 | 
					    #
 | 
				
			||||||
    # Returns the path to the source file
 | 
					    # Returns the path to the source file
 | 
				
			||||||
    def path
 | 
					    def path
 | 
				
			||||||
      data.fetch('path', relative_path.sub(/\A\//, ''))
 | 
					      data.fetch('path') { relative_path.sub(/\A\//, '') }
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # The path to the page source file, relative to the site source
 | 
					    # The path to the page source file, relative to the site source
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -108,14 +108,14 @@ module Jekyll
 | 
				
			||||||
    #
 | 
					    #
 | 
				
			||||||
    # Returns excerpt string.
 | 
					    # Returns excerpt string.
 | 
				
			||||||
    def excerpt
 | 
					    def excerpt
 | 
				
			||||||
      data.fetch('excerpt', extracted_excerpt.to_s)
 | 
					      data.fetch('excerpt') { extracted_excerpt.to_s }
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Public: the Post title, from the YAML Front-Matter or from the slug
 | 
					    # Public: the Post title, from the YAML Front-Matter or from the slug
 | 
				
			||||||
    #
 | 
					    #
 | 
				
			||||||
    # Returns the post title
 | 
					    # Returns the post title
 | 
				
			||||||
    def title
 | 
					    def title
 | 
				
			||||||
      data.fetch("title", titleized_slug)
 | 
					      data.fetch('title') { titleized_slug }
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Turns the post slug into a suitable title
 | 
					    # 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
 | 
					    # Returns the path to the file relative to the site source
 | 
				
			||||||
    def path
 | 
					    def path
 | 
				
			||||||
      data.fetch('path', relative_path.sub(/\A\//, ''))
 | 
					      data.fetch('path') { relative_path.sub(/\A\//, '') }
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # The path to the post source file, relative to the site source
 | 
					    # The path to the post source file, relative to the site source
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -46,24 +46,23 @@ module Jekyll
 | 
				
			||||||
    # Returns the _unsanitizied_ String URL
 | 
					    # Returns the _unsanitizied_ String URL
 | 
				
			||||||
    def generate_url
 | 
					    def generate_url
 | 
				
			||||||
      @placeholders.inject(@template) do |result, token|
 | 
					      @placeholders.inject(@template) do |result, token|
 | 
				
			||||||
 | 
					        break result if result.index(':').nil?
 | 
				
			||||||
        result.gsub(/:#{token.first}/, self.class.escape_path(token.last))
 | 
					        result.gsub(/:#{token.first}/, self.class.escape_path(token.last))
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Returns a sanitized String URL
 | 
					    # Returns a sanitized String URL
 | 
				
			||||||
    def sanitize_url(in_url)
 | 
					    def sanitize_url(in_url)
 | 
				
			||||||
 | 
					      url = in_url \
 | 
				
			||||||
      # Remove all double slashes
 | 
					        # Remove all double slashes
 | 
				
			||||||
      url = in_url.gsub(/\/\//, "/")
 | 
					        .gsub(/\/\//, '/') \
 | 
				
			||||||
 | 
					        # Remove every URL segment that consists solely of dots
 | 
				
			||||||
      # Remove every URL segment that consists solely of dots
 | 
					        .split('/').reject{ |part| part =~ /^\.+$/ }.join('/') \
 | 
				
			||||||
      url = url.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
 | 
					      # Append a trailing slash to the URL if the unsanitized URL had one
 | 
				
			||||||
      url += "/" if in_url =~ /\/$/
 | 
					      url << "/" if in_url[-1].eql?('/')
 | 
				
			||||||
 | 
					 | 
				
			||||||
      # Always add a leading slash
 | 
					 | 
				
			||||||
      url.gsub!(/\A([^\/])/, '/\1')
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      url
 | 
					      url
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -16,6 +16,13 @@ class TestURL < Test::Unit::TestCase
 | 
				
			||||||
      ).to_s
 | 
					      ).to_s
 | 
				
			||||||
    end
 | 
					    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
 | 
					    should "return permalink if given" do
 | 
				
			||||||
      assert_equal "/le/perma/link", URL.new(
 | 
					      assert_equal "/le/perma/link", URL.new(
 | 
				
			||||||
        :template => "/:x/:y",
 | 
					        :template => "/:x/:y",
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue