Skip to content

Method name lookup - #1844

Closed
skatkov wants to merge 6 commits into
ruby:masterfrom
skatkov:method-name-lookup
Closed

skatkov wants to merge 6 commits into
ruby:masterfrom
skatkov:method-name-lookup

Conversation

@skatkov

@skatkov skatkov commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

This PR speeds up method-name lookups in RDoc and makes the method collection read-only so callers cannot accidentally invalidate the lookup cache.

What changed

Previously, each lookup scanned the whole method list. Now, lib/rdoc/code_object/context/method_list.rb maintains a lazy index mapping each name to its matching methods.

  • The index is built on the first lookup.
  • Adding methods indexes only the appended entries—not the entire list again.
  • Removing or replacing methods invalidates the index.
  • Renaming a method invalidates every index containing it, including after module promotion or shared aliases.
  • Mutable name strings fall back to scanning, because their contents can change without calling name=.

The index keeps methods in list order. Class/instance filtering happens at lookup time, preserving the existing selection behavior. Attributes remain outside this index.

flowchart TD
    Lookup["Context method lookup"] --> List["Internal MethodList"]
    List --> Names{"All names frozen?"}

    Names -->|Yes| Index["Lazy name index<br/>Index appended entries only"]
    Names -->|No| Scan["Scan current method names"]

    Index --> Candidates["Matching methods in list order"]
    Scan --> Candidates
    Candidates --> Result["Return first match<br/>Apply class/instance filter if requested"]

    Add["add_method"] -.->|Extend on next lookup| Index
    Change["Removal, replacement,<br/>or method rename"] -.->|Invalidate| Index

    Reader["context.method_list"] --> Snapshot["Frozen Array snapshot<br/>Method objects remain mutable"]
    List -.->|Copy contents| Snapshot
Loading

Public API change

Context#method_list now returns a frozen Array snapshot, rather than exposing the mutable backing collection.

Callers change membership through:

context.add_method(method)
context.remove_method(method)
context.replace_methods(methods)

Existing writers in Store loading, live-preview cleanup, visibility filtering, and class promotion/merging were migrated to these operations. replace_methods changes the documentation list only; it deliberately leaves methods_hash registrations unchanged.

This is a breaking change for callers that directly mutate method_list. Method objects themselves are not frozen, so renaming remains supported.

Parser to return frozen method names.

Ruby, C and RBS parser to forcefully return frozen strings for method names. This greatly improves speed improvements.

-> TODO: Add more information about this

Other generators

I have tested this branch with other generators - rdoc-markdown, sdoc and hanna. All seem to be working just fine with these breaking changes with no modifications required.

Open Questions

  • Why does the parser produce mutable strings for method names? Speed up would be so much faster, if that was not the case. Needs investigations...
  • method_list now returns frozen array, this works with Aliki and rdoc-markdown. But would that cause any issues with hanna, sdoc?

Keep name buckets private behind scalar lookups with optional predicates. Preserve scan behavior for mutable name strings and use validity tokens for explicitly renamed methods.

Discard derived cache state when copying or marshaling the collection, with focused coverage for lookup boundaries, mutable names, and direct serialization.
Store methods in MethodList and route all four name lookup paths through its scalar lookup API while preserving ordering and singleton matching.

Cover additions, renames, filtering, shared collections, and mutations, together with class promotion, store loading, merging, and live-preview replacement and removal.
@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Documentation preview

View the preview

Commit: 8648339

@skatkov skatkov closed this Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant