Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazily evaluate languages #2768

Closed
RunDevelopment opened this issue Feb 17, 2021 · 2 comments
Closed

Lazily evaluate languages #2768

RunDevelopment opened this issue Feb 17, 2021 · 2 comments

Comments

@RunDevelopment
Copy link
Member

Motivation
Adding a new language to Prism takes time. While Prism grammars itself are cheap, our thousands of regexes are not, so loading a large number of grammars can take quite some time.

While Prism evaluates grammars, page loading and rendering are blocked. This means that having Prism choosing a Prism bundle with more languages hurts performance even if those languages end up unused.

Description
Add a way for Prism to lazily evaluate languages. (This should be doable with #2736.)

The performance gain is quite significant. Simply loading a Prism bundle (min) with all languages included takes about 200ms on my machine. Loading that same bundle with lazy evaluation only takes 10ms.

This can be a game-changer, especially on low-end and mobile devices.

Everything is eagerly evaluated:

image

Only Markup, JS, CSS, and C-like are eagerly evaluated:
(Everything else is wrapped into a function.)

image


Obviously, Prism Core is always eagerly evaluated.

@mAAdhaTTah
Copy link
Member

mAAdhaTTah commented Feb 17, 2021

I think we'd need to dig into the perf impacts a bit. If we lazily evaluate a grammar that ends up being needed immediately anyway, are we getting any benefits from lazy eval? My suspicion is a loaded language is likely to be used on a given page so the benefits of lazy eval may be limited.

@RunDevelopment
Copy link
Member Author

If we lazily evaluate a grammar that ends up being needed immediately anyway, are we getting any benefits from lazy eval?

Probably not. It's mainly useful when you have a large number of grammars and might not need them immediately.

The main use case for this is large bundles. With "large", I mean all languages. Those large bundles are easy to use because they have everything. Users don't have to worry about Autoloader or language dependencies. Everything just works.
Bundle size doesn't matter too much with large bundles (because Prism also works when loaded async) but Prism blocking the page is a problem.

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

No branches or pull requests

2 participants