Module: FastRequire

Included in:
Kernel
Defined in:
lib/fast_require.rb

Constant Summary collapse

@@require_locs =
{}
@@already_loaded =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.already_loadedObject

XXXX within a very long depth to require fast_require, require ‘a’ => ‘b’ => ‘c’ => ‘d’ & fast_require

then
=> 'b.rb'

it works always



97
98
99
# File 'lib/fast_require.rb', line 97

def self.already_loaded
  @@already_loaded
end

.clear_all!Object



121
122
123
124
125
126
# File 'lib/fast_require.rb', line 121

def self.clear_all!
  require 'fileutils'
  FileUtils.rm_rf @@dir if File.exist? @@dir
  @@require_locs.clear
  setup
end

.default_saveObject



113
114
115
# File 'lib/fast_require.rb', line 113

def self.default_save
  self.save @@loc
end

.dirObject



105
106
107
# File 'lib/fast_require.rb', line 105

def self.dir
  @@dir
end

.guess_discover(partial_name, add_dot_rb = false) ⇒ Object

try to see where this file was loaded from, from $: partial_name might be abc.rb, or might be abc partial_name might be a full path, too



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fast_require.rb', line 34

def self.guess_discover partial_name, add_dot_rb = false
  
  # test for full path first
  # unfortunately it has to be a full separate test
  # for windoze sake, as drive letter could be different than slapping a '/' on the dir to test list...
  tests = [partial_name]
  
  if add_dot_rb
    tests << partial_name + '.rb'
    tests << partial_name + '.so'
  end
  
  tests.each{|b|
    # assume that .rb.rb is...valid...
    if File.file?(b) && ((b[-3..-1] == '.rb') || (b[-3..-1] == '.so'))
      return File.expand_path(b)
    end
  }
    
  for dir in $:
    if File.file?(b = (dir + '/' + partial_name))
      # make sure we require a file that has the right suffix...
      if (b[-3..-1] == '.rb')  || (b[-3..-1] == '.so')
        return File.expand_path(b)
      end

    end
  end

  if add_dot_rb && (partial_name[-3..-1] != '.rb') && (partial_name[-3..-1] != '.so')
    guess_discover(partial_name + '.rb') || guess_discover(partial_name + '.so')
  else
    nil
  end
end

.load(filename) ⇒ Object



19
20
21
# File 'lib/fast_require.rb', line 19

def self.load filename
  @@require_locs = Marshal.restore( File.open(filename, 'rb') {|f| f.read})
end

.require_locsObject



101
102
103
# File 'lib/fast_require.rb', line 101

def self.require_locs
  @@require_locs
end

.resetup!Object



195
196
197
# File 'lib/fast_require.rb', line 195

def self.resetup!
  eval "module ::Kernel; alias :require :require_cached; end"
end

.sanitize(filename) ⇒ Object



13
14
15
# File 'lib/fast_require.rb', line 13

def self.sanitize filename
  filename.gsub(/[\/:]/, '_')
end

.save(to_file) ⇒ Object



117
118
119
# File 'lib/fast_require.rb', line 117

def self.save to_file
  File.open(to_file, 'wb'){|f| f.write Marshal.dump(@@require_locs)}
end

.setupObject



4
5
6
7
8
9
10
11
# File 'lib/fast_require.rb', line 4

def self.setup
  @@dir = File.expand_path('~/.ruby_fast_require_cache')

  Dir.mkdir @@dir unless File.directory?(@@dir)
  @@loc = @@dir + '/' + RUBY_VERSION + '-' + RUBY_PLATFORM + '-' + 
     sanitize(File.expand_path($0).gsub(/[\/:]/, '_')) + 
     sanitize(Dir.pwd)
end

Instance Method Details

#require_cached(lib) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/fast_require.rb', line 128

def require_cached lib
  lib = lib.to_s unless lib.is_a?(String) # might not be zactly 1.9 compat...
  if known_loc = @@require_locs[lib]
    return false if @@already_loaded[known_loc]
    @@already_loaded[known_loc] = true
    if known_loc =~ /.so$/
      puts 'doing original_non_cached_require on .so full path ' + known_loc if $FAST_REQUIRE_DEBUG
      original_non_cached_require known_loc # not much we can do there...too bad...
    else
      puts 'doing eval on ' + lib + '=>' + known_loc if $FAST_REQUIRE_DEBUG
      $LOADED_FEATURES << known_loc # *must*
      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...
    end
  else
    # we don't know the location--let Ruby's original require do the heavy lifting for us here
    old = $LOADED_FEATURES.dup
    if(original_non_cached_require lib)
      # debugger might land here the first time you run a script and it doesn't have a require
      # cached yet...
      new = $LOADED_FEATURES - old
      found = new.last

      # incredibly, in 1.8.6, this doesn't always get set to a full path
      if RUBY_VERSION < '1.9'
        if !File.file?(found)
          # discover the full path.
          dir = $:.find{|path| File.file?(path + '/' + found)}
          found = dir + '/' + found
        end
        found = File.expand_path(found);
      end
      puts 'found new loc:' + lib + '=>' + found if $FAST_REQUIRE_DEBUG
      @@require_locs[lib] = found
      @@already_loaded[found] = true
      return true
    else
      puts 'already loaded, apparently' + lib if $FAST_REQUIRE_DEBUG
      # this probably was something like
      # the first pass was require 'regdeferred'
      # now it's a different require 'regdeferred.rb'
      # which fails (or vice versa)
      # so figure out why
      # calc location, expand, map back
      where_found = FastRequire.guess_discover(lib, true)
      if where_found
        puts 'inferred ghost loc:' + lib + '=>' + where_found if $FAST_REQUIRE_DEBUG
        @@require_locs[lib] = where_found
        # unfortunately if it's our first pass
        # and we are in the middle of a "real" require
        # that is circular
        # then $LOADED_FEATURES or (AFAIK) nothing will have been set
        # for us to be able to assert that
        # so...I think we'll end up
        # just fudging for a bit
        #	raise 'not found' unless @@already_loaded[where_found] # should have already been set...I think...
      else
        if $FAST_REQUIRE_DEBUG
          # happens for enumerator XXXX
          puts 'unable to infer' + lib + ' in ' if $FAST_REQUIRE_DEBUG
          @@already_loaded[found] = true # hacky
        end
      end
      return false # XXXX test all these return values
    end
  end
end