fast_require 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +36 -7
- data/VERSION +1 -1
- data/lib/fast_require.rb +18 -5
- data/spec/files/gem_after.rb +0 -2
- data/spec/files/requires_itself.rb +0 -3
- data/spec/spec.fast_require.rb +26 -22
- metadata +5 -2
data/README
CHANGED
@@ -1,22 +1,51 @@
|
|
1
1
|
A little utility to make
|
2
|
+
|
2
3
|
require 'xxx'
|
3
4
|
|
4
5
|
take much less time.
|
5
6
|
|
6
|
-
|
7
|
+
Well, mostly on windows--on linux it's a speedup of only 0.41 to 0.45s, or so. [1]
|
7
8
|
|
8
|
-
If you've ever wondered why ruby feels slow...sometimes it's just the startup time
|
9
|
+
If you've ever wondered why ruby feels slow on doze...sometimes it's just the startup time. This helps.
|
9
10
|
|
10
11
|
Benchmarks:
|
11
12
|
|
12
|
-
loading
|
13
|
+
loading a spec file:
|
13
14
|
|
14
|
-
|
15
|
+
1.9.1
|
16
|
+
without 3.20s
|
17
|
+
with 0.34s (10x improvement)
|
15
18
|
|
19
|
+
1.8.6
|
20
|
+
without 3.6s
|
21
|
+
with 1.25s
|
22
|
+
|
16
23
|
|
17
|
-
|
24
|
+
rails app, running script/console "puts 333"
|
18
25
|
|
19
|
-
windows:
|
20
26
|
1.9.1
|
21
27
|
without:
|
22
|
-
|
28
|
+
20s
|
29
|
+
with:
|
30
|
+
10s
|
31
|
+
|
32
|
+
1.8.6
|
33
|
+
without:
|
34
|
+
9s
|
35
|
+
with:
|
36
|
+
6s
|
37
|
+
|
38
|
+
rake -T
|
39
|
+
|
40
|
+
1.9.1
|
41
|
+
without: 3.75s
|
42
|
+
with: 1.5s
|
43
|
+
|
44
|
+
1.8.6
|
45
|
+
without: 1.37s
|
46
|
+
with: 1.25s
|
47
|
+
|
48
|
+
Note: in reality what we should do is fix core so that it doesn't have such awful I/O time in windows. There may be some gross inefficiency in there. For now, this is a work-around.
|
49
|
+
|
50
|
+
[1] A sister project to this one, faster_gem_script, can make ruby scripts in linux run faster by 0.1s :) http://github.com/rdp/faster_gem_script
|
51
|
+
(in windows it's a much higher gain). Eventually they'll be combined into one "gem optimizer" gem.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/lib/fast_require.rb
CHANGED
@@ -1,10 +1,13 @@
|
|
1
1
|
module FastRequire
|
2
2
|
$FAST_REQUIRE_DEBUG ||= false
|
3
|
+
|
3
4
|
def self.setup
|
4
5
|
@@dir = File.expand_path('~/.ruby_fast_require_cache')
|
5
6
|
|
6
7
|
Dir.mkdir @@dir unless File.directory?(@@dir)
|
7
|
-
@@loc = @@dir + '/' + RUBY_VERSION + '-' + RUBY_PLATFORM + '-' +
|
8
|
+
@@loc = @@dir + '/' + RUBY_VERSION + '-' + RUBY_PLATFORM + '-' +
|
9
|
+
sanitize(File.expand_path($0).gsub(/[\/:]/, '_')) +
|
10
|
+
sanitize(Dir.pwd)
|
8
11
|
end
|
9
12
|
|
10
13
|
def self.sanitize filename
|
@@ -12,12 +15,17 @@ module FastRequire
|
|
12
15
|
end
|
13
16
|
|
14
17
|
FastRequire.setup
|
18
|
+
|
19
|
+
def self.load filename
|
20
|
+
@@require_locs = Marshal.restore( File.open(filename, 'rb') {|f| f.read})
|
21
|
+
end
|
15
22
|
|
16
23
|
if File.exist?(@@loc)
|
17
|
-
|
24
|
+
FastRequire.load @@loc
|
18
25
|
else
|
19
26
|
@@require_locs = {}
|
20
27
|
end
|
28
|
+
|
21
29
|
@@already_loaded = {}
|
22
30
|
|
23
31
|
# try to see where this file was loaded from, from $:
|
@@ -99,11 +107,15 @@ module FastRequire
|
|
99
107
|
end
|
100
108
|
|
101
109
|
at_exit {
|
102
|
-
FastRequire.
|
110
|
+
FastRequire.default_save
|
103
111
|
}
|
112
|
+
|
113
|
+
def self.default_save
|
114
|
+
self.save @@loc
|
115
|
+
end
|
104
116
|
|
105
|
-
def self.save
|
106
|
-
File.open(
|
117
|
+
def self.save to_file
|
118
|
+
File.open(to_file, 'wb'){|f| f.write Marshal.dump(@@require_locs)}
|
107
119
|
end
|
108
120
|
|
109
121
|
def self.clear_all!
|
@@ -172,6 +184,7 @@ module FastRequire
|
|
172
184
|
if $FAST_REQUIRE_DEBUG
|
173
185
|
# happens for enumerator XXXX
|
174
186
|
puts 'unable to infer' + lib + ' in ' if $FAST_REQUIRE_DEBUG
|
187
|
+
@@already_loaded[found] = true # hacky
|
175
188
|
end
|
176
189
|
end
|
177
190
|
return false # XXXX test all these return values
|
data/spec/files/gem_after.rb
CHANGED
@@ -2,9 +2,7 @@ require '../lib/fast_require.rb'
|
|
2
2
|
require 'rubygems'
|
3
3
|
Gem::Specification
|
4
4
|
|
5
|
-
#require '_dbg'
|
6
5
|
raise if FastRequire.already_loaded.to_a.flatten.grep(/files\/b.rb/).length > 0
|
7
6
|
require 'files/b.rb'
|
8
7
|
raise if(require 'files/b.rb')
|
9
8
|
raise unless FastRequire.already_loaded.to_a.flatten.grep(/files\/b.rb/).length > 0
|
10
|
-
#raise 'I dont trust this'
|
data/spec/spec.fast_require.rb
CHANGED
@@ -1,10 +1,17 @@
|
|
1
1
|
# $FAST_REQUIRE_DEBUG
|
2
|
-
|
3
|
-
require '
|
4
|
-
require '
|
5
|
-
|
2
|
+
if RUBY_VERSION < '1.9'
|
3
|
+
require 'faster_rubygems'
|
4
|
+
require 'sane'
|
5
|
+
end
|
6
6
|
|
7
|
+
cached = '.cached_spec_locs' + RUBY_VERSION
|
8
|
+
puts cached
|
7
9
|
require_relative '../lib/fast_require'
|
10
|
+
# use it for our own local test specs
|
11
|
+
FastRequire.load cached if File.exist? cached
|
12
|
+
require 'spec/autorun'
|
13
|
+
require 'benchmark'
|
14
|
+
FastRequire.save cached
|
8
15
|
|
9
16
|
describe "faster requires" do
|
10
17
|
|
@@ -22,16 +29,13 @@ describe "faster requires" do
|
|
22
29
|
end
|
23
30
|
|
24
31
|
it "should be able to do a single require" do
|
25
|
-
old = $LOADED_FEATURES.dup
|
26
32
|
Dir.chdir('files') do
|
27
|
-
|
28
|
-
#require '_dbg'
|
29
|
-
3
|
33
|
+
old = $LOADED_FEATURES.dup
|
30
34
|
assert require('c')
|
31
35
|
assert !(require 'c')
|
36
|
+
new = $LOADED_FEATURES - old
|
37
|
+
raise new.inspect if new.length != 1
|
32
38
|
end
|
33
|
-
new = $LOADED_FEATURES - old
|
34
|
-
raise new.inspect if new.length > 1
|
35
39
|
end
|
36
40
|
|
37
41
|
it "should be able to go two sub-requires deep appropriately" do
|
@@ -77,7 +81,7 @@ describe "faster requires" do
|
|
77
81
|
|
78
82
|
it "should save a file as a cache in a dir" do
|
79
83
|
assert Dir[FastRequire.dir + '/*'].length == 0 # all clear
|
80
|
-
FastRequire.
|
84
|
+
FastRequire.default_save
|
81
85
|
assert Dir[FastRequire.dir + '/*'].length > 0
|
82
86
|
end
|
83
87
|
|
@@ -99,8 +103,9 @@ describe "faster requires" do
|
|
99
103
|
end
|
100
104
|
|
101
105
|
private
|
106
|
+
|
102
107
|
def ruby filename
|
103
|
-
assert system(@ruby + " " + filename)
|
108
|
+
3.times { assert system(@ruby + " " + filename) }
|
104
109
|
end
|
105
110
|
|
106
111
|
it "should override rubygems' require if rubygems is loaded after the fact...maybe by hooking to Gem::const_defined or something" do
|
@@ -109,7 +114,12 @@ describe "faster requires" do
|
|
109
114
|
|
110
115
|
it "should override rubygems' require if rubygems is loaded before the fact" do
|
111
116
|
ruby "files/gem_before.rb"
|
112
|
-
end
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should not double load gems" do
|
120
|
+
a = `#{@ruby} files/gem_after.rb 2>&1`
|
121
|
+
a.should_not match('already initialized')
|
122
|
+
end
|
113
123
|
|
114
124
|
it "should throw if you require it twice" do
|
115
125
|
Dir.chdir('files') do
|
@@ -120,20 +130,18 @@ describe "faster requires" do
|
|
120
130
|
it "should force require 'abc' to not load file called exactly abc" do
|
121
131
|
Dir.chdir('files') do
|
122
132
|
ruby 'require_non_dot_rb_fails.rb'
|
123
|
-
ruby 'require_non_dot_rb_fails.rb' # should succeed
|
124
133
|
end
|
125
134
|
end
|
126
135
|
|
127
|
-
it "should
|
136
|
+
it "should handle full path requires" do
|
128
137
|
Dir.chdir('files') do
|
129
138
|
ruby 'require_full_path.rb'
|
130
|
-
ruby 'require_full_path.rb'
|
131
139
|
end
|
132
140
|
end
|
133
141
|
|
134
|
-
it "should handle Pathname too" do
|
142
|
+
it "should handle Pathname requires, too" do
|
135
143
|
require 'pathname'
|
136
|
-
require Pathname.new('
|
144
|
+
require Pathname.new('pathname')
|
137
145
|
end
|
138
146
|
|
139
147
|
it "should work well with rubygems for gem libs (installed), themselves"
|
@@ -143,8 +151,4 @@ describe "faster requires" do
|
|
143
151
|
ruby 'files/requires_itself.rb'
|
144
152
|
end
|
145
153
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
154
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fast_require
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roger Pack
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
12
|
+
date: 2010-01-28 00:00:00 -07:00
|
13
13
|
default_executable: fast_require
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -67,10 +67,13 @@ files:
|
|
67
67
|
- spec/files/e.rb
|
68
68
|
- spec/files/fast.rb
|
69
69
|
- spec/files/gem_after.rb
|
70
|
+
- spec/files/gem_before.rb
|
70
71
|
- spec/files/large.rb
|
71
72
|
- spec/files/non_dot_rb.rb
|
72
73
|
- spec/files/require_full_path.rb
|
74
|
+
- spec/files/require_non_dot_rb_fails.rb
|
73
75
|
- spec/files/require_twice_in_dir_pwd.rb
|
76
|
+
- spec/files/requires_itself.rb
|
74
77
|
- spec/files/slow.rb
|
75
78
|
- spec/spec.fast_require.rb
|
76
79
|
has_rdoc: true
|