-
-
Notifications
You must be signed in to change notification settings - Fork 387
[DO NOT MERGE] Use require relative to load files. #476
Conversation
In theory this should be faster, but I can't get a statistically significant benchmark showing it is.
Do you find
There are simple ways to avoid defining a global method: Or we could define
How so?
If there's no noticable perf improvement, then certainly we should not do it. That said, this got done in rspec-core due to a quite noticable perf improvement it provided, as reported by a user: https://groups.google.com/forum/#!searchin/rspec/require_relative/rspec/G4Dntk9aVeg/WYHhxMSsALAJ I confess I haven't benchmarked this myself. I'd like to play with it a bit before ruling out doing this (at which point we should probably remove the code for this from rspec-core)...or, if I find a case where it is noticably faster than I'd like to do some version of it. |
this
because you also need to ensure you have the file that defines the hack loaded. I guess if that is in it's own file you could do it, but now you're adding an extra require to every file...
Oh right, that's kind of obvious :/ I also forgot to mention: there are a heap of autoloads in this lib, I'm not sure there's a way to apply this fix to them. |
Also worth noting that require performance in 1.9.3 was a massive regression, and has since been fixed in 2 and later. |
I'm leaning heavily on "let's not do this" now... given the regression on 1.9.3 for next to no benefit? |
I found something interesting in rspec/rspec-core#1348 but my son wants to play a game so I'll explain later :). |
OK...so in that rspec-core PR, I set out to benchmark time (for i in {1..100}; do ruby -Ilib:../rspec-support/lib -e "require 'rspec/core'"; done) Then I noticed that there's already a benchmark script there, where David had measured a 12% difference in 1.9.3 (probably the newest MRI at the time): I tried this benchmark on 1.9.3, 2.0 and 2.1 in rspec/rspec-core@78daad6 and definitely saw a noticeable difference. This puzzled me at first, but after thinking about it some more, I think it actually makes perfect sense. Here's what I think is going on:
In my first benchmark, I setup the load path with
Notice that it prepends the load path with the specified directory -- which means in my initial benchmark, I was comparing the best-case constant time performance of In the second benchmark, I used So...here's what I think this means:
So...after my initial benchmark, I was thinking I agreed with both of you about not doing this (and also removing it from rspec-core). After the second benchmark and further reflection, I'm leaning towards doing it in all the rspec repos. However, I first want to test my thinking above some more. I'm going to try running some benchmarks with 10, 100 and 1000 dirs on my load path and see how |
OK, so I did the 10 vs 100 vs 1000 dirs in load path benchmarking I mentioned above: The results confirm what I conjectured above:
@xaviershay / @JonRowe -- thoughts? |
I'm ok with it if we can refactor it into a non global method, how about we put this in support? Then we can have |
I can't think of a way to make it work for other libs from rspec-support, because |
Actually, I figured out a way we can reduce the duplication and leverage rspec-support. I'm working on a PR. |
See rspec/rspec-expectations#476 for the discussion and background to this.
See rspec/rspec-expectations#476 for the discussion and background to this.
new benchmark convinces me. |
👍 |
@xaviershay -- do you want to update this PR (or open a new one) that leverages the new support for this in rspec-support? Similar to rspec/rspec-mocks#607. |
I won't be able to get this until the weekend, if anyone else wanted to have a crack. |
* Use new and shiny require_relative (rspec/rspec-expectations#476 (comment)) * Remove references to RubyForge as it will die soon * Require Ruby >= 1.9.3 and build on newer rubies * Remove bundler_stubis from gitignore as Google knows nothing about it * Remove references to RDoc as project uses YARD * Remove rubygems_version as it shouldn't be specified (http://guides.rubygems.org/specification-reference/#rubygems_version)
* Use new and shiny require_relative (rspec/rspec-expectations#476 (comment)) * Remove references to RubyForge as it will die soon * Require Ruby >= 1.9.3 and build on newer rubies * Remove bundler_stubis from gitignore as Google knows nothing about it * Remove references to RDoc as project uses YARD * Remove rubygems_version as it shouldn't be specified (http://guides.rubygems.org/specification-reference/#rubygems_version)
require_relative is faster than require See rspec/rspec-expectations#476 (comment) Signed-off-by: Tim Smith <tsmith@chef.io>
require_relative does not have to traverse the filesystem to find the deps so it's faster than require. See rspec/rspec-expectations#476 (comment) Signed-off-by: Tim Smith <tsmith@chef.io>
require_relative is faster than require See rspec/rspec-expectations#476 (comment) Signed-off-by: Tim Smith <tsmith@chef.io>
require_relative is faster as it doesn't require traversing the filesystem looking for the require. See benchmarks here: rspec/rspec-expectations#476 (comment) Signed-off-by: Tim Smith <tsmith@chef.io>
In theory this should be faster, but I can't get a statistically
significant benchmark showing it is.
I'm currently not in favour of this change:
Use
require_relative
rather thanrequire
to load files for faster boot times #464