Skip to content

speedup: cache entry points#2979

Merged
birkenfeld merged 1 commit intopygments:masterfrom
brondsem:cache_entry_points
Feb 20, 2026
Merged

speedup: cache entry points#2979
birkenfeld merged 1 commit intopygments:masterfrom
brondsem:cache_entry_points

Conversation

@brondsem
Copy link
Copy Markdown
Contributor

I found that multiple calls to get_lexer_for_filename were somewhat slow in my environment, due to find_plugin_lexers discovering entry points every time it ran.

Its interesting that pkg_resources (used in the past) seems to cache entry points: https://github.com/pypa/pkg_resources/blob/main/__init__.py#L3212 But importlib.metadata does not.

I did some simple testing like this:

start = time.perf_counter_ns()
for i in range(100):
    get_lexer_for_filename("some.py", encoding="chardet")
    get_lexer_for_filename("some.c", encoding="chardet")
    get_lexer_for_filename("some.cpp", encoding="chardet")
    get_lexer_for_filename("some.java", encoding="chardet")
    try:
        get_lexer_for_filename("some.fasdf", encoding="chardet")
    except pygments.util.ClassNotFound:
        pass
end = time.perf_counter_ns()
print(f"Time: {(end - start) / 1_000_000} ms")

On my system I got these timings:

  • Pygments 2.12 (using pkg_resources) took about 1,400ms
  • Pygments 2.13-2.19.2 (importlib) took about 14,000ms
  • 2.19.2 with a cache added took about 800-1100ms

It seems to me that adding this caching would not be a problem if pkg_resources cached entry points in the past. I'm not sure how someone would modify entry points at runtime anyway.

@birkenfeld
Copy link
Copy Markdown
Member

That's a very good idea IMO.

@birkenfeld birkenfeld merged commit 9b50756 into pygments:master Feb 20, 2026
15 checks passed
@Anteru Anteru added this to the 2.20.0 milestone Mar 26, 2026
@Anteru Anteru added the A-api area: changes to Pygments API or cmdline label Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-api area: changes to Pygments API or cmdline

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants