1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/ext
schneems 51070ee5c4 Faster Pathname FileUtils methods
Currently when calling any of the "FileUtils" methods on pathname `require` is called every time even though that library might already be loaded. This is slow:

We can speed it up by either checking first if the constant is already defined, or by using autoload.

Using defined speeds up the action by about 300x and using autoload is about twice as fast as that (600x faster than current require method).

I'm proposing we use autoload:

```ruby
require 'benchmark/ips'

Benchmark.ips do |x|
  autoload(:FileUtils, "fileutils")
  x.report("require") { require 'fileutils' }
  x.report("defined") { require 'fileutils' unless defined?(FileUtils) }
  x.report("autoload") { FileUtils }

  x.compare!
end

# Warming up --------------------------------------
#              require     3.624k i/100ms
#              defined     1.465M i/100ms
#             autoload     2.320M i/100ms
# Calculating -------------------------------------
#              require     36.282k (± 2.4%) i/s -    184.824k in   5.097153s
#              defined     14.539M (± 2.0%) i/s -     73.260M in   5.041161s
#             autoload     23.100M (± 1.9%) i/s -    115.993M in   5.023271s

# Comparison:
#             autoload: 23099779.2 i/s
#              defined: 14538544.9 i/s - 1.59x  (± 0.00) slower
#              require:    36282.3 i/s - 636.67x  (± 0.00) slower
```

Because this autoload is scoped to Pathname it will not change the behavior of existing programs that are not expecting FileUtils to be loaded yet:

```
ruby -rpathname -e "class Pathname; autoload(:FileUtils, 'fileutils'); end; puts FileUtils.exist?"
Traceback (most recent call last):
-e:1:in `<main>': uninitialized constant FileUtils (NameError)
```
2021-08-30 15:18:11 +09:00
..
-test- Include ruby.h before internal headers to suppress -Wundef warnings 2021-08-09 19:25:18 +09:00
bigdecimal Fixed 'maybe_unused' attribute 2021-07-06 01:13:54 +09:00
cgi/escape Fix -Wundef warnings for HAVE_RB_EXT_RACTOR_SAFE 2021-05-04 14:56:55 +02:00
continuation dependency updates 2021-04-13 14:30:21 +09:00
coverage dependency updates 2021-04-13 14:30:21 +09:00
date [ruby/date] Add zontab.list dependency 2021-08-22 20:28:52 +09:00
digest [ruby/digest] Also drop to support Ruby 2.4 2021-07-28 11:48:36 +09:00
etc dependency updates 2021-04-13 14:30:21 +09:00
fcntl dependency updates 2021-04-13 14:30:21 +09:00
fiddle [ruby/fiddle] Improve "offsetof" calculations (https://github.com/ruby/fiddle/pull/90) 2021-08-24 16:18:22 +09:00
io [ruby/io-console] Move FFI console under lib 2021-04-22 11:53:13 +09:00
json Update the latest version of json.gemspec from flori/json 2021-07-29 15:57:58 +09:00
monitor Distinguish signal and timeout [Bug #16608] 2021-07-25 13:09:03 -07:00
nkf Fix -Wundef warnings in core extensions 2021-05-04 14:56:55 +02:00
objspace [Feature #18045] Remove T_PAYLOAD 2021-08-25 09:28:21 -04:00
openssl [ruby/openssl] Strip trailing spaces 2021-07-18 18:04:50 +09:00
pathname Faster Pathname FileUtils methods 2021-08-30 15:18:11 +09:00
psych [ruby/psych] fix: use git repository link for LibYAML in docs 2021-07-29 15:54:41 +09:00
pty dependency updates 2021-04-13 14:30:21 +09:00
racc/cparse [ruby/racc] Add missing check for rb_block_call() 2021-07-18 20:38:34 +09:00
rbconfig/sizeof dependency updates 2021-04-13 14:30:21 +09:00
readline Added missing declarations in readline.h bundled with macOS 10.13 2021-07-09 08:52:35 +09:00
ripper dependency updates 2021-04-13 14:30:21 +09:00
rubyvm Added depend files 2019-07-14 01:31:29 +09:00
socket undefine alloc functions for C extensions 2021-08-20 08:30:06 +09:00
stringio [ruby/stringio] Suppress a sign-compare warning 2021-07-08 16:43:22 +09:00
strscan [ruby/strscan] Replace "iff" with "if and only if" (#18) 2021-05-06 16:21:14 +09:00
syslog dependency updates 2021-04-13 14:30:21 +09:00
win32 Fix some typos by spell checker 2021-04-26 10:07:41 +09:00
win32ole Fix _MSC_VER warnings 2021-06-03 00:07:25 +09:00
zlib [ruby/zlib] Synchronize access to zstream to prevent segfault in multithreaded use 2021-07-28 13:40:03 +09:00
.document Remove unnecessary files from .document 2020-12-20 16:00:13 +09:00
extmk.rb transcode-tblgen.rb: make silent a little when just -v 2020-12-29 17:45:19 +09:00
Setup Removed sdbm entries from toolchanins 2020-06-19 08:26:47 +09:00
Setup.atheos Removed sdbm entries from toolchanins 2020-06-19 08:26:47 +09:00
Setup.nt Removed sdbm entries from toolchanins 2020-06-19 08:26:47 +09:00