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

Precompilation? #130

Open
nebrelbug opened this issue Aug 8, 2019 · 12 comments
Open

Precompilation? #130

nebrelbug opened this issue Aug 8, 2019 · 12 comments

Comments

@nebrelbug
Copy link

I'd love to use Moo for a library I'm building, but it needs to be extremely lightweight (even more than 4KB minzipped).

Is there a possibility of adding an option to precompile, a step which would basically moo.compile a grammar and then output a generated, more lightweight JS file that could be tweaked and customized?

@tjvr
Copy link
Collaborator

tjvr commented Aug 8, 2019

It's not something we've thought about.

I haven't checked in detail, but I would guess about half of Moo's source code deals with compilation. The other half is used at runtime. Would 1–2K be small enough? (Of course, this is in addition to the tokenizer RegExp itself.)

Sent with GitHawk

@nebrelbug
Copy link
Author

Yes, I think it would. I was also thinking that once I generated a new JS file, I could tweak some things by hand, like removing features I don't need.

@bd82
Copy link
Contributor

bd82 commented Aug 8, 2019

Would Tree-Shaking assist here?

@nebrelbug
Copy link
Author

@bd82 I don't think so, because I want to find a way to have some things (like building the RegExp) happen before runtime.

It would be great if there was some way to just save the lexer in a separate JS file after generation.

@tjvr
Copy link
Collaborator

tjvr commented Aug 9, 2019

Just for fun, here's a Gist which provides a silly (albeit working) approach to compiling a Moo lexer.

It's silly because it extracts the Lexer and LexerIterator class definitions from inside moo.js in a rather gross way. I doubt we'd ever consider merging this code 🙃 If we wanted to support this properly, we'd probably want to split up moo.js into two or three parts: in particular, you'd want the runtime structures (i.e. the Lexer class) to be separate from everything that builds the tokenizer, so that you can import just the runtime in your code.

Some stats:

  • moo.js is 17682 bytes, 4949 gzipped.
  • A tiny example tokenizer is 5981 bytes, 1817 gzipped.

@nebrelbug
Copy link
Author

Thanks @tjvr, that's pretty nifty! I agree the code would probably not be clean enough to merge, but I really like the idea of separate runtime structures.

@nebrelbug
Copy link
Author

Hi @Tvjr! I'm considering using Moo to build a template engine, and wondered if you considered moving the runtime structure outside of the main moo.js?

@nebrelbug
Copy link
Author

Also, I've recently been digging into the source code. Could you explain what fast is?

@nathan
Copy link
Collaborator

nathan commented Sep 26, 2019

Could you explain what fast is?

It comes from #40 / #103. It makes single-character tokens significantly faster.

@nebrelbug
Copy link
Author

@nathan thanks!

I seem to remember once running a benchmark that showed that str[0] actually ended up being faster than str.charCodeAt(0). Is there a reason why charCodeAt was chosen?

@nebrelbug
Copy link
Author

@chocolateboy did your thumbs down mean you didn't approve of str.charCodeAt, or you didn't like my comment?

@tjvr
Copy link
Collaborator

tjvr commented Sep 29, 2019

Is there a reason why charCodeAt was chosen?

Benchmarks at the time showed that it was slightly faster. It's certainly possible that's changed.

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

No branches or pull requests

4 participants