Skip requiring cross compiled .so
on non-Windows platform
#4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
require "io/console"
takes so much time on my machine, and here's an attempt to fix that situation.Currently,
console.rb
tries to load the cross-compiled.so
file first, then falls back to the plain extension onLoadError
.However, it needs to scan through all installed gem's
lib
directories to make sure if something with this name exists, and that whole process results in significant overhead.For instance, while simply running a ruby process takes 0.17 second,
just requiring 'io/console' adds extra 0.1+ sec on my MacBook Pro having 656 gems installed.
For me, 0.1+ second is unignorable overhead for this kind of library.
As I wrote above, this delay is because
require "#{RUBY_VERSION[/\d+\.\d+/]}/io/console.so"
searches all locally installed gems for a matching.so
file, and so with more gems installed, the more delay it would cause.The following tiny oneliner shows what's actually happening here. You see, thousands of method calls are occurring inside RubyGems.
Obviously, we'd better avoid raising and rescuing a
LoadError
where not necessary.This PR drastically reduces these method calls by not trying to require the cross-compiled
.so
file where not needed,and reduces the
require
overhead to be almost zero second on non-Windows platforms.I'm not 100% confident about my approach though. There could be a better way to detect the existence of a cross-compiled extension, or we could use
require_relative
or something, maybe? I'm not a regular user of rake-compiler, so I'm open to any advice. :)