Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute document's relative_path faster #6767

Merged
merged 2 commits into from
Feb 28, 2018

Conversation

ashmaroli
Copy link
Member

Use String#sub instead of Pathutil#relative_path_from which uses regexes internally

Benchmark script:

#!/usr/bin/env ruby

require 'benchmark/ips'
require 'pathutil'

DOC_PATH = File.join(File.expand_path(__dir__), "_puppies", "rover.md")
COL_PATH = File.join(File.expand_path(__dir__), "_puppies")


def pathutil_relative
  Pathutil.new(DOC_PATH).relative_path_from(COL_PATH).to_s
end

def native_relative
  DOC_PATH.sub("#{COL_PATH}/", "")
end

if pathutil_relative == native_relative
  Benchmark.ips do |x|
    x.report("pathutil") { pathutil_relative }
    x.report("native")   { native_relative }
    x.compare!
  end
else
  print "PATHUTIL: "
  puts pathutil_relative
  print "NATIVE:   "
  puts native_relative
end

Note: Since Document#relative_path is a memoized function, the difference is noticeable only on large sites that containing a huge number of Documents

Copy link
Member

@pathawks pathawks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable

@ashmaroli ashmaroli mentioned this pull request Feb 17, 2018
13 tasks
@DirtyF DirtyF requested a review from a team February 17, 2018 13:54
@parkr
Copy link
Member

parkr commented Feb 20, 2018

@ashmaroli Would you mind placing the benchmark script in the PR body into the repo?

~/jekyll/jekyll#master$ be ruby benchmark/pathutil-relative-vs-gsub.rb
Warming up --------------------------------------
            pathutil     5.992k i/100ms
              native    50.261k i/100ms
Calculating -------------------------------------
            pathutil     61.075k (± 8.6%) i/s -    305.592k in   5.042290s
              native    584.400k (± 5.4%) i/s -      2.915M in   5.001884s

Comparison:
              native:   584399.5 i/s
            pathutil:    61074.8 i/s - 9.57x  slower

@ashmaroli
Copy link
Member Author

Would you mind placing the benchmark script in the PR body into the repo?

Sure.. not a problem.. 😃

Copy link
Member

@DirtyF DirtyF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

@ashmaroli ashmaroli added this to the v3.8.0 milestone Feb 20, 2018
@DirtyF
Copy link
Member

DirtyF commented Feb 28, 2018

@jekyllbot: merge +minor

@jekyllbot jekyllbot merged commit 07e49f8 into jekyll:master Feb 28, 2018
jekyllbot added a commit that referenced this pull request Feb 28, 2018
@ashmaroli ashmaroli deleted the doc-relative-path branch February 28, 2018 16:45
@jekyll jekyll locked and limited conversation to collaborators Jul 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants