faster_require 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,12 +1,15 @@
1
- require 'faster_rubygems' if RUBY_VERSION < "1.9'"
1
+ require 'rubygems' if RUBY_VERSION < "1.9'"
2
2
  require 'jeweler'
3
3
  Jeweler::Tasks.new do |s|
4
4
  s.name = "faster_require"
5
- s.summary = "A tool designed to speedup library loading in Ruby by caching library locations"
5
+ s.summary = "Speed library loading in Ruby"
6
+ s.description = "A tool designed to speedup library loading in Ruby by caching library locations"
6
7
  s.email = "rogerdpack@gmail.com"
7
8
  s.homepage = "http://github.com/rdp/faster_require"
8
9
  s.authors = ["Roger Pack"]
9
- s.add_development_dependency 'rspec'
10
+ s.add_development_dependency 'redparse'
11
+ s.add_development_dependency 'jeweler'
12
+ s.add_development_dependency 'rspec', '>= 2'
10
13
  s.add_development_dependency 'sane'
11
14
  s.add_development_dependency 'ruby-prof'
12
15
  # s.add_dependency
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.2
1
+ 0.6.0
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require '../lib/faster_require'
3
+ require 'stringio'
4
+ require 'redparse'
5
+ require 'ruby_parser'
6
+ RedParse
7
+ RubyParser
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'stringio'
3
+ require 'redparse'
4
+ require 'ruby_parser'
5
+ RedParse
6
+ RubyParser
@@ -1,13 +1,15 @@
1
+ require 'rbconfig'
2
+
1
3
  module FastRequire
2
4
  $FAST_REQUIRE_DEBUG ||= $DEBUG # can set it via $DEBUG, or by itself
3
5
 
4
6
  def self.setup
5
7
  @@dir = File.expand_path('~/.ruby_faster_require_cache')
6
-
7
8
  Dir.mkdir @@dir unless File.directory?(@@dir)
8
- @@loc = @@dir + '/' + RUBY_VERSION + '-' + RUBY_PLATFORM + '-' +
9
- sanitize(File.expand_path($0).gsub(/[\/:]/, '_')) +
10
- sanitize(Dir.pwd)
9
+
10
+ parts = [File.basename($0), RUBY_VERSION, RUBY_PLATFORM, File.basename(Dir.pwd), Dir.pwd, File.dirname($0), File.expand_path(File.dirname($0))].map{|part| sanitize(part)}
11
+ loc_name = (parts.map{|part| part[0..5]} + parts).join('-')[0..75] # try to be unique, but short...
12
+ @@loc = @@dir + '/' + loc_name
11
13
  end
12
14
 
13
15
  def self.sanitize filename
@@ -17,7 +19,7 @@ module FastRequire
17
19
  FastRequire.setup
18
20
 
19
21
  def self.load filename
20
- @@require_locs = Marshal.restore( File.open(filename, 'rb') {|f| f.read})
22
+ @@require_locs = Marshal.restore( File.open(filename, 'rb') {|f| f.read}) rescue {}
21
23
  end
22
24
 
23
25
  if File.exist?(@@loc)
@@ -40,12 +42,12 @@ module FastRequire
40
42
 
41
43
  if add_dot_rb
42
44
  tests << partial_name + '.rb'
43
- tests << partial_name + '.so'
45
+ tests << partial_name + '.' + RbConfig::CONFIG['DLEXT']
44
46
  end
45
47
 
46
48
  tests.each{|b|
47
- # assume that .rb.rb is...valid...
48
- if File.file?(b) && ((b[-3..-1] == '.rb') || (b[-3..-1] == '.so'))
49
+ # assume that .rb.rb is...valid...?
50
+ if File.file?(b) && ((b[-3..-1] == '.rb') || (b[-3..-1] == '.' + RbConfig::CONFIG['DLEXT']))
49
51
  return File.expand_path(b)
50
52
  end
51
53
  }
@@ -53,15 +55,15 @@ module FastRequire
53
55
  for dir in $:
54
56
  if File.file?(b = (dir + '/' + partial_name))
55
57
  # make sure we require a file that has the right suffix...
56
- if (b[-3..-1] == '.rb') || (b[-3..-1] == '.so')
58
+ if (b[-3..-1] == '.rb') || (b[-3..-1] == '.' + RbConfig::CONFIG['DLEXT'])
57
59
  return File.expand_path(b)
58
60
  end
59
61
 
60
62
  end
61
63
  end
62
64
 
63
- if add_dot_rb && (partial_name[-3..-1] != '.rb') && (partial_name[-3..-1] != '.so')
64
- guess_discover(partial_name + '.rb') || guess_discover(partial_name + '.so')
65
+ if add_dot_rb && (partial_name[-3..-1] != '.rb') && (partial_name[-3..-1] != '.' + RbConfig::CONFIG['DLEXT'])
66
+ guess_discover(partial_name + '.rb') || guess_discover(partial_name + '.')
65
67
  else
66
68
  nil
67
69
  end
@@ -87,7 +89,6 @@ module FastRequire
87
89
  @@require_locs[$0] = File.expand_path($0) # so when we run into it on a require, we will skip it...
88
90
  @@already_loaded[File.expand_path($0)] = true
89
91
 
90
-
91
92
  # XXXX within a very long depth to require fast_require,
92
93
  # require 'a' => 'b' => 'c' => 'd' & fast_require
93
94
  # then
@@ -124,19 +125,43 @@ module FastRequire
124
125
  @@require_locs.clear
125
126
  setup
126
127
  end
127
-
128
+ # require 'ruby-debug'
128
129
  def require_cached lib
129
- lib = lib.to_s unless lib.is_a?(String) # might not be zactly 1.9 compat...
130
+ lib = lib.to_s # might not be zactly 1.9 compat... to_path ??
131
+ # p 'doing require ' + lib
130
132
  if known_loc = @@require_locs[lib]
131
- return false if @@already_loaded[known_loc]
133
+ if @@already_loaded[known_loc]
134
+ p 'already loaded ' + known_loc if $FAST_REQUIRE_DEBUG
135
+ return false
136
+ end
132
137
  @@already_loaded[known_loc] = true
133
- if known_loc =~ /.so$/
138
+ if known_loc =~ /\.#{RbConfig::CONFIG['DLEXT']}$/
134
139
  puts 'doing original_non_cached_require on .so full path ' + known_loc if $FAST_REQUIRE_DEBUG
135
- original_non_cached_require known_loc # not much we can do there...too bad...
140
+ original_non_cached_require known_loc # not much we can do there...too bad...well at least we pass it a full path though :P
136
141
  else
137
- puts 'doing eval on ' + lib + '=>' + known_loc if $FAST_REQUIRE_DEBUG
138
- $LOADED_FEATURES << known_loc # *must*
139
- return eval(File.open(known_loc, 'rb') {|f| f.read}, TOPLEVEL_BINDING, known_loc) || true # note the b here--this means it's reading .rb files as binary, which *typically* works--if it breaks re-save the offending file in binary mode, or file an issue on the tracker...
142
+ unless $LOADED_FEATURES.include? known_loc
143
+ if known_loc =~ /rubygems.rb$/
144
+ puts 'requiring rubygems ' + known_loc if $FAST_REQUIRE_DEBUG
145
+ original_non_cached_require(known_loc) # normal require so rubygems doesn't freak out when it finds itself already in $LOADED_FEATURES :P
146
+ else
147
+ if $FAST_REQUIRE_DEBUG
148
+ puts 'doing cached loc eval on ' + lib + '=>' + known_loc
149
+ end
150
+ $LOADED_FEATURES << known_loc
151
+ # fakely add the load path, too, so that autoload for the same file will work <sigh> [rspec2]
152
+ no_suffix_full_path = known_loc.gsub(/\.[^.]+$/, '')
153
+ no_suffix_lib = lib.gsub(/\.[^.]+$/, '')
154
+ libs_path = no_suffix_full_path.gsub(no_suffix_lib, '')
155
+ libs_path = File.expand_path(libs_path) # strip off trailing '/'
156
+ $: << libs_path unless $:.index(libs_path)
157
+ # load(known_loc, false) # too slow
158
+ eval(File.open(known_loc, 'rb') {|f| f.read}, TOPLEVEL_BINDING, known_loc) # note the rb here--this means it's reading .rb files as binary, which *typically* works...maybe unnecessary?
159
+ # --if it breaks re-save the offending file in binary mode, or file an issue on the tracker...
160
+ return true
161
+ end
162
+ else
163
+ puts 'ignoring already loaded? ' + known_loc if $FAST_REQUIRE_DEBUG
164
+ end
140
165
  end
141
166
  else
142
167
  # we don't know the location--let Ruby's original require do the heavy lifting for us here
@@ -171,7 +196,7 @@ module FastRequire
171
196
  # calc location, expand, map back
172
197
  where_found = FastRequire.guess_discover(lib, true)
173
198
  if where_found
174
- puts 'inferred ghost loc:' + lib + '=>' + where_found if $FAST_REQUIRE_DEBUG
199
+ puts 'inferred lib loc:' + lib + '=>' + where_found if $FAST_REQUIRE_DEBUG
175
200
  @@require_locs[lib] = where_found
176
201
  # unfortunately if it's our first pass
177
202
  # and we are in the middle of a "real" require
@@ -202,7 +227,7 @@ module Kernel
202
227
 
203
228
  if(defined?(@already_using_faster_require))
204
229
  raise 'twice not allowed...'
205
- # *shouldn't* get here...unless I'm wrong...
230
+ # *shouldn't* ever get here...unless I'm wrong...
206
231
  else
207
232
  @already_using_faster_require = true
208
233
  include FastRequire
File without changes
@@ -1,4 +1,4 @@
1
1
  $FAST_REQUIRE_DEBUG = 1
2
- a = File.dirname(File.expand_path(__FILE__)) + '/../../lib/fast_require.rb'
2
+ a = File.dirname(File.expand_path(__FILE__)) + '/../../lib/faster_require.rb'
3
3
  load a
4
4
  load a
@@ -0,0 +1 @@
1
+ rm -rf %HOMEPATH%/.ruby_faster_require_cache
@@ -1,4 +1,5 @@
1
- require 'rubygems'
2
1
  require File.dirname(__FILE__) + "/../../lib/faster_require.rb"
3
- require 'spec/autorun'
4
- a = Spec
2
+ require 'rubygems'
3
+ require 'rspec'
4
+ a = RSpec
5
+ p 'success'
@@ -0,0 +1,6 @@
1
+ # different rubygems orders
2
+ require 'rubygems'
3
+ require File.dirname(__FILE__) + "/../../lib/faster_require.rb"
4
+ require 'rspec'
5
+ a = RSpec
6
+ p 'success'
@@ -1,3 +1,4 @@
1
+ $: << '.'
1
2
  require '../lib/faster_require.rb'
2
3
  require 'rubygems'
3
4
  Gem::Specification
@@ -1,3 +1,4 @@
1
+ $: << '.'
1
2
  require 'rubygems'
2
3
  Gem::Specification
3
4
 
@@ -1,6 +1,7 @@
1
+ $: << '.'
1
2
  $:.unshift 'bin'
2
3
  require File.dirname(File.expand_path(__FILE__)) + '/../../lib/faster_require.rb'
3
4
  require 'non_dot_rb.rb' # succeed
4
- require 'non_dot_rb' # fails (well, returns false), we think it succeeds, but it in the wrong place, so when we run the second time, it loads the wrong file
5
+ raise if require 'non_dot_rb' # fails (well, returns false), we think it succeeds, but it in the wrong place, so when we run the second time, it loads the wrong file
5
6
 
6
7
  # unless my gem is working right
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ require File.dirname(__FILE__) + "/../../lib/faster_require.rb"
3
+ require 'rspec'
4
+ a = RSpec
5
+ raise 'bad' if FastRequire.constants.grep(/spec/i).length > 0
@@ -1,3 +1,3 @@
1
1
  require 'rubygems'
2
- require 'spec/autorun'
3
- a = Spec
2
+ require 'rspec'
3
+ a = RSpec
@@ -1,28 +1,32 @@
1
- # $FAST_REQUIRE_DEBUG
1
+ # can set this: $FAST_REQUIRE_DEBUG
2
+
2
3
  if RUBY_VERSION < '1.9'
3
- require 'faster_rubygems'
4
+ require 'rubygems' # faster_rubygems, perhaps?
4
5
  end
6
+
5
7
  require 'sane'
6
8
  require 'benchmark'
7
9
 
8
-
9
10
  unless RUBY_PLATFORM =~ /java/
10
- require_relative '../lib/faster_require'
11
- cached = '.cached_spec_locs' + RUBY_VERSION
12
- # use it for our own local test specs
13
- FastRequire.load cached if File.exist? cached
14
- require 'spec/autorun'
15
- FastRequire.save cached
11
+ require_relative '../lib/faster_require'
12
+ cached = '.cached_spec_locs' + RUBY_VERSION
13
+ # use it for our own local test specs
14
+ begin
15
+ require 'spec/autorun'
16
+ rescue LoadError
17
+ # rspec 2
18
+ require 'rspec'
19
+ end
20
+ FastRequire.load cached if File.exist? cached
21
+ FastRequire.save cached
16
22
  else
17
23
 
18
24
  require 'spec/autorun'
19
-
20
- require_relative '../lib/faster_require'
25
+ require_relative '../lib/faster_require'
21
26
 
22
27
  end
23
28
 
24
-
25
- describe "faster requires" do
29
+ describe "requires faster" do
26
30
 
27
31
  before do
28
32
  FastRequire.clear_all!
@@ -73,12 +77,14 @@ describe "faster requires" do
73
77
  assert(system("#{OS.ruby_bin} large.rb"))
74
78
  end
75
79
  end
80
+
81
+ it "could cache file contents, too, in theory...oh my"
76
82
 
77
83
  it "should not re-save the cache file if it hasn't changed [?]"
78
84
 
79
- it "should require .so files still, and only load them once" do
85
+ it "should load .so files still, and only load them once" do
80
86
  # ruby-prof gem
81
- 2.times { require 'ruby_prof'; RubyProf } # .so
87
+ 2.times { require 'ruby_prof.so'; RubyProf }
82
88
  assert $LOADED_FEATURES.length == (@old_length + 1)
83
89
  end
84
90
 
@@ -102,9 +108,11 @@ describe "faster requires" do
102
108
  assert system("ruby -I../../lib e.rb")
103
109
  assert system("ruby -C.. -I../lib files/e.rb")
104
110
  end
111
+ # require 'ruby-debug'
112
+ # debugger
105
113
  assert Dir[FastRequire.dir + '/*'].length == 3
106
- assert Dir[FastRequire.dir + '/*spec_files_d*'].length == 1 # use full path
107
- assert Dir[FastRequire.dir + '/*spec_files_e*'].length == 2 # different dirs
114
+ assert Dir[FastRequire.dir + '/*d.rb*'].length == 1 # use full path
115
+ assert Dir[FastRequire.dir + '/*e.rb*'].length == 2 # different Dir.pwd's
108
116
  end
109
117
 
110
118
  context "should work with ascii files well" do # most are binary, so...low prio
@@ -114,7 +122,8 @@ describe "faster requires" do
114
122
  private
115
123
 
116
124
  def ruby filename
117
- 3.times { assert system(@ruby + " " + filename) }
125
+ command = @ruby + " " + filename
126
+ 3.times { raise command unless system(command) }
118
127
  end
119
128
 
120
129
  it "should override rubygems' require if rubygems is loaded after the fact...maybe by hooking to Gem::const_defined or something" do
@@ -157,7 +166,20 @@ describe "faster requires" do
157
166
 
158
167
  it "should disallow a file requiring itself" do
159
168
  ruby 'files/requires_itself.rb'
160
- ruby 'files/requires_itself.rb'
161
169
  end
162
170
 
171
+ it "should put modules in the right place" do
172
+ Dir.chdir('files') do
173
+ ruby 'should_put_modules_in_right_place.rb'
174
+ end
175
+ end
176
+
177
+ it "should" do
178
+ Dir.chdir('files') do
179
+ ruby 'fast2.rb'
180
+ end
181
+ end
182
+
183
+
184
+
163
185
  end
@@ -0,0 +1,9 @@
1
+ slow = true
2
+ file_name = File.expand_path('./yo.rb')
3
+ File.open(file_name, 'w') do |f| f.write '33333'; end
4
+
5
+ if !slow
6
+ 1000.times { eval File.read(file_name) } # ascii mode
7
+ else
8
+ 1000.times { load file_name } # ascii mode
9
+ end
@@ -0,0 +1,10 @@
1
+ 33333
2
+ 33333
3
+ 33333
4
+ 33333
5
+ 33333
6
+ 33333
7
+ 33333
8
+ 33333
9
+ 33333
10
+ 33333
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faster_require
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ hash: 7
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 6
9
+ - 0
10
+ version: 0.6.0
5
11
  platform: ruby
6
12
  authors:
7
13
  - Roger Pack
@@ -9,40 +15,80 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2010-02-02 00:00:00 -07:00
18
+ date: 2011-02-25 00:00:00 -07:00
13
19
  default_executable: faster_require
14
20
  dependencies:
15
21
  - !ruby/object:Gem::Dependency
16
- name: rspec
22
+ name: redparse
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
17
33
  type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: jeweler
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
20
40
  requirements:
21
41
  - - ">="
22
42
  - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
23
46
  version: "0"
24
- version:
47
+ type: :development
48
+ version_requirements: *id002
25
49
  - !ruby/object:Gem::Dependency
26
- name: sane
50
+ name: rspec
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 7
58
+ segments:
59
+ - 2
60
+ version: "2"
27
61
  type: :development
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: sane
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
30
68
  requirements:
31
69
  - - ">="
32
70
  - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
33
74
  version: "0"
34
- version:
75
+ type: :development
76
+ version_requirements: *id004
35
77
  - !ruby/object:Gem::Dependency
36
78
  name: ruby-prof
37
- type: :development
38
- version_requirement:
39
- version_requirements: !ruby/object:Gem::Requirement
79
+ prerelease: false
80
+ requirement: &id005 !ruby/object:Gem::Requirement
81
+ none: false
40
82
  requirements:
41
83
  - - ">="
42
84
  - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
43
88
  version: "0"
44
- version:
45
- description:
89
+ type: :development
90
+ version_requirements: *id005
91
+ description: A tool designed to speedup library loading in Ruby by caching library locations
46
92
  email: rogerdpack@gmail.com
47
93
  executables:
48
94
  - faster_require
@@ -54,6 +100,8 @@ files:
54
100
  - README
55
101
  - Rakefile
56
102
  - VERSION
103
+ - benchmark/benchmark.rb
104
+ - benchmark/benchmark_slow.rb
57
105
  - bin/faster_require
58
106
  - lib/faster_require.rb
59
107
  - lib/rubygems_plugin.rb
@@ -61,9 +109,11 @@ files:
61
109
  - spec/files/attempt_double_load.rb
62
110
  - spec/files/b.rb
63
111
  - spec/files/c.rb
112
+ - spec/files/clear.bat
64
113
  - spec/files/d.rb
65
114
  - spec/files/e.rb
66
115
  - spec/files/fast.rb
116
+ - spec/files/fast2.rb
67
117
  - spec/files/gem_after.rb
68
118
  - spec/files/gem_before.rb
69
119
  - spec/files/large.rb
@@ -72,51 +122,66 @@ files:
72
122
  - spec/files/require_non_dot_rb_fails.rb
73
123
  - spec/files/require_twice_in_dir_pwd.rb
74
124
  - spec/files/requires_itself.rb
125
+ - spec/files/should_put_modules_in_right_place.rb
75
126
  - spec/files/slow.rb
76
127
  - spec/spec.fast_require.rb
128
+ - spec/files/a.rb
129
+ - spec/temp/slow.rb
130
+ - spec/temp/yo.rb
77
131
  has_rdoc: true
78
132
  homepage: http://github.com/rdp/faster_require
79
133
  licenses: []
80
134
 
81
135
  post_install_message:
82
- rdoc_options:
83
- - --charset=UTF-8
136
+ rdoc_options: []
137
+
84
138
  require_paths:
85
139
  - lib
86
140
  required_ruby_version: !ruby/object:Gem::Requirement
141
+ none: false
87
142
  requirements:
88
143
  - - ">="
89
144
  - !ruby/object:Gem::Version
145
+ hash: 3
146
+ segments:
147
+ - 0
90
148
  version: "0"
91
- version:
92
149
  required_rubygems_version: !ruby/object:Gem::Requirement
150
+ none: false
93
151
  requirements:
94
152
  - - ">="
95
153
  - !ruby/object:Gem::Version
154
+ hash: 3
155
+ segments:
156
+ - 0
96
157
  version: "0"
97
- version:
98
158
  requirements: []
99
159
 
100
160
  rubyforge_project:
101
- rubygems_version: 1.3.5
161
+ rubygems_version: 1.3.7
102
162
  signing_key:
103
163
  specification_version: 3
104
- summary: A tool designed to speedup library loading in Ruby by caching library locations
164
+ summary: Speed library loading in Ruby
105
165
  test_files:
106
- - spec/files/attempt_double_load.rb
166
+ - spec/files/a.rb
107
167
  - spec/files/a_requires_b.rb
168
+ - spec/files/attempt_double_load.rb
108
169
  - spec/files/b.rb
109
170
  - spec/files/c.rb
110
171
  - spec/files/d.rb
111
172
  - spec/files/e.rb
112
173
  - spec/files/fast.rb
174
+ - spec/files/fast2.rb
113
175
  - spec/files/gem_after.rb
114
176
  - spec/files/gem_before.rb
115
177
  - spec/files/large.rb
116
178
  - spec/files/non_dot_rb.rb
117
- - spec/files/requires_itself.rb
118
179
  - spec/files/require_full_path.rb
119
180
  - spec/files/require_non_dot_rb_fails.rb
120
181
  - spec/files/require_twice_in_dir_pwd.rb
182
+ - spec/files/requires_itself.rb
183
+ - spec/files/should_put_modules_in_right_place.rb
121
184
  - spec/files/slow.rb
122
185
  - spec/spec.fast_require.rb
186
+ - spec/temp/slow.rb
187
+ - spec/temp/yo.rb