Skip to content

Commit 108fd04

Browse files
hsbtclaude
andcommitted
Lazy load error_highlight, did_you_mean, and syntax_suggest
Defer requiring these gems from boot time to first Exception#detailed_message call. They only enhance error display, so loading them eagerly is unnecessary overhead. On Apple M1 Pro, ruby -e1 boot time drops from 114ms to 30ms (with user-installed gems), matching --disable-* flag performance. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 44b99d6 commit 108fd04

1 file changed

Lines changed: 37 additions & 15 deletions

File tree

gem_prelude.rb

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,42 @@
88
require 'bundled_gems'
99
end if defined?(Gem)
1010

11-
begin
12-
require 'error_highlight'
13-
rescue LoadError
14-
warn "`error_highlight' was not loaded."
15-
end if defined?(ErrorHighlight)
11+
if defined?(ErrorHighlight) || defined?(DidYouMean) || defined?(SyntaxSuggest)
12+
module Ruby::DetailedError # :nodoc:
13+
@loaded = false
1614

17-
begin
18-
require 'did_you_mean'
19-
rescue LoadError
20-
warn "`did_you_mean' was not loaded."
21-
end if defined?(DidYouMean)
15+
def self.loaded? = @loaded
2216

23-
begin
24-
require 'syntax_suggest/core_ext'
25-
rescue LoadError
26-
warn "`syntax_suggest' was not loaded."
27-
end if defined?(SyntaxSuggest)
17+
def self.load
18+
return if @loaded
19+
@loaded = true
20+
21+
begin
22+
require 'error_highlight'
23+
rescue LoadError
24+
warn "`error_highlight' was not loaded."
25+
end if defined?(ErrorHighlight)
26+
27+
begin
28+
require 'did_you_mean'
29+
rescue LoadError
30+
warn "`did_you_mean' was not loaded."
31+
end if defined?(DidYouMean)
32+
33+
begin
34+
require 'syntax_suggest/core_ext'
35+
rescue LoadError
36+
warn "`syntax_suggest' was not loaded."
37+
end if defined?(SyntaxSuggest)
38+
end
39+
40+
def detailed_message(...)
41+
return super if Ruby::DetailedError.loaded?
42+
Ruby::DetailedError.load
43+
# Re-dispatch to pick up the newly prepended methods from the gems
44+
detailed_message(...)
45+
end
46+
end
47+
48+
Exception.prepend(Ruby::DetailedError)
49+
end

0 commit comments

Comments
 (0)