Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Reduce subshells #57

Merged
merged 2 commits into from

2 participants

@tenderlove

Rails boot seems to be spending a lot of time creating subshells. These two commits should reduce the subshells that ExecJS creates by implementing which in Ruby. File stats are reduced by lazily searching for the executable.

I made a similar change to bundler.

Thanks!

@josh josh merged commit aaa25c4 into sstephenson:master
@josh josh commented on the diff
lib/execjs/external_runtime.rb
((6 lines not shown))
end
+ private
+ def binary
+ @binary ||= locate_binary
+ end
+
+ def which_windows(name)
+ result = `#{shell_escape("#{ExecJS.root}/support/which.bat", name)}`
+ result.strip.split("\n").first
+ end
+
+ def which_unix(name)
+ if File.executable? cmd
@josh
josh added a note

name vs cmd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@josh

Fixed 9329d2b

@luislavena luislavena referenced this pull request
Merged

Remove subshells windows #58

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 29 additions and 13 deletions.
  1. +29 −13 lib/execjs/external_runtime.rb
View
42 lib/execjs/external_runtime.rb
@@ -94,7 +94,7 @@ def initialize(options)
@test_args = options[:test_args]
@test_match = options[:test_match]
@encoding = options[:encoding]
- @binary = locate_binary
+ @binary = nil
end
def exec(source)
@@ -113,16 +113,37 @@ def compile(source)
def available?
require "multi_json"
- @binary ? true : false
+ binary ? true : false
end
+ private
+ def binary
+ @binary ||= locate_binary
+ end
+
+ def which_windows(name)
+ result = `#{shell_escape("#{ExecJS.root}/support/which.bat", name)}`
+ result.strip.split("\n").first
+ end
+
+ def which_unix(name)
+ if File.executable? cmd
@josh
josh added a note

name vs cmd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ cmd
+ else
+ path = ENV['PATH'].split(File::PATH_SEPARATOR).find { |path|
+ File.executable? File.join(path, cmd)
+ }
+ path && File.expand_path(cmd, path)
+ end
+ end
+
protected
def runner_source
@runner_source ||= IO.read(@runner_path)
end
def exec_runtime(filename)
- output = sh("#{shell_escape(*(@binary.split(' ') << filename))} 2>&1")
+ output = sh("#{shell_escape(*(binary.split(' ') << filename))} 2>&1")
if $?.success?
output
else
@@ -142,19 +163,14 @@ def locate_binary
end
def which(command)
- Array(command).each do |name|
+ Array(command).find do |name|
name, args = name.split(/\s+/, 2)
- result = if ExecJS.windows?
- `#{shell_escape("#{ExecJS.root}/support/which.bat", name)}`
- else
- `#{shell_escape('command', '-v', name)} 2>/dev/null`
- end
+ path = ExecJS.windows? ? which_windows(name) : which_unix(name)
- if path = result.strip.split("\n").first
- return args ? "#{path} #{args}" : path
- end
+ next unless path
+
+ args ? "#{path} #{args}" : path
end
- nil
end
if "".respond_to?(:force_encoding)
Something went wrong with that request. Please try again.