Skip to content
This repository

Reduce subshells #57

Merged
merged 2 commits into from over 2 years ago

2 participants

Aaron Patterson Joshua Peek
Aaron Patterson

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!

Joshua Peek
Collaborator

:metal:

Joshua Peek josh merged commit aaa25c4 into from December 05, 2011
Joshua Peek josh closed this December 05, 2011
Joshua Peek josh commented on the diff December 05, 2011
lib/execjs/external_runtime.rb
((6 lines not shown))
117 117
     end
118 118
 
  119
+    private
  120
+      def binary
  121
+        @binary ||= locate_binary
  122
+      end
  123
+
  124
+      def which_windows(name)
  125
+        result = `#{shell_escape("#{ExecJS.root}/support/which.bat", name)}`
  126
+        result.strip.split("\n").first
  127
+      end
  128
+
  129
+      def which_unix(name)
  130
+        if File.executable? cmd
1
Joshua Peek Collaborator
josh added a note December 05, 2011

name vs cmd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Joshua Peek
Collaborator

Fixed 9329d2b

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 1 changed file with 29 additions and 13 deletions. Show diff stats Hide diff stats

  1. 42  lib/execjs/external_runtime.rb
42  lib/execjs/external_runtime.rb
@@ -94,7 +94,7 @@ def initialize(options)
94 94
       @test_args   = options[:test_args]
95 95
       @test_match  = options[:test_match]
96 96
       @encoding    = options[:encoding]
97  
-      @binary      = locate_binary
  97
+      @binary      = nil
98 98
     end
99 99
 
100 100
     def exec(source)
@@ -113,16 +113,37 @@ def compile(source)
113 113
 
114 114
     def available?
115 115
       require "multi_json"
116  
-      @binary ? true : false
  116
+      binary ? true : false
117 117
     end
118 118
 
  119
+    private
  120
+      def binary
  121
+        @binary ||= locate_binary
  122
+      end
  123
+
  124
+      def which_windows(name)
  125
+        result = `#{shell_escape("#{ExecJS.root}/support/which.bat", name)}`
  126
+        result.strip.split("\n").first
  127
+      end
  128
+
  129
+      def which_unix(name)
  130
+        if File.executable? cmd
  131
+          cmd
  132
+        else
  133
+          path = ENV['PATH'].split(File::PATH_SEPARATOR).find { |path|
  134
+            File.executable? File.join(path, cmd)
  135
+          }
  136
+          path && File.expand_path(cmd, path)
  137
+        end
  138
+      end
  139
+
119 140
     protected
120 141
       def runner_source
121 142
         @runner_source ||= IO.read(@runner_path)
122 143
       end
123 144
 
124 145
       def exec_runtime(filename)
125  
-        output = sh("#{shell_escape(*(@binary.split(' ') << filename))} 2>&1")
  146
+        output = sh("#{shell_escape(*(binary.split(' ') << filename))} 2>&1")
126 147
         if $?.success?
127 148
           output
128 149
         else
@@ -142,19 +163,14 @@ def locate_binary
142 163
       end
143 164
 
144 165
       def which(command)
145  
-        Array(command).each do |name|
  166
+        Array(command).find do |name|
146 167
           name, args = name.split(/\s+/, 2)
147  
-          result = if ExecJS.windows?
148  
-            `#{shell_escape("#{ExecJS.root}/support/which.bat", name)}`
149  
-          else
150  
-            `#{shell_escape('command', '-v', name)} 2>/dev/null`
151  
-          end
  168
+          path = ExecJS.windows? ? which_windows(name) : which_unix(name)
152 169
 
153  
-          if path = result.strip.split("\n").first
154  
-            return args ? "#{path} #{args}" : path
155  
-          end
  170
+          next unless path
  171
+
  172
+          args ? "#{path} #{args}" : path
156 173
         end
157  
-        nil
158 174
       end
159 175
 
160 176
       if "".respond_to?(:force_encoding)
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.