Skip to content

Lazily loaded libraries fire user method_added/inherited hooks #139

Closed

Description

@Largo

Libraries IronRuby loads lazily run their definitions in the middle of user code: ErrorHighlight, DidYouMean and prism's Ruby half (loaded when the first error message is built), StringIO and others. A program's Module#method_added / singleton_method_added / inherited hooks then fire for thousands of library methods and classes. CRuby loads error_highlight and did_you_mean at boot, before any user code.

class Module; def method_added(n) = ($n = ($n || 0) + 1); end
begin; nosuch; rescue NameError => e; e.message; end
p $n   # CRuby: 1   IronRuby: 3005 (3234 after a detailed_message)

Seen in Experiments/Methods/Visibility/s.rb: 3,400 hook calls. Possible fixes: suppress user hooks while an internal library loads, or load these where user hooks can't be installed yet.

Found by running the scripts PR #119 deleted (Tests/Experiments, Tests/Legacy) on CRuby 4.0.9 and on IronRuby built from master c7f70cf (net10.0, Linux).

Activity

  1. Largo commented on Nov 8, 2026

    @Largo
    OwnerAuthor

    Closing: the premise was wrong. CRuby 4.0.9 fires the same hooks.

    class Module; def method_added(n) = ($n += 1); end
    $n = 0
    begin; nosuch; rescue NameError => e; e.message; end;                          p $n   # CRuby 0,    IronRuby 0
    begin; nosuch; rescue NameError => e; e.detailed_message(highlight: false); end; p $n   # CRuby 3004, IronRuby 3131

    error_highlight in CRuby 4.0 requires prism's Ruby half lazily when it first spots an error, so a program's method_added, const_added and inherited see thousands of prism definitions there too. The repro in this issue went through another path at the time.

    The remaining ~4% (3,131 vs 3,004) comes from StringIO and io/console, which are C in CRuby and Ruby in IronRuby. That is not worth hiding hooks for: muting them would make IronRuby less like CRuby for prism, which is the bulk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions