Readit News logoReadit News
sophiabits · 2 years ago
There’s a project called “dprint” which you can use in the interim if Prettier is a bottleneck :) It’s a Rust-based code formatting platform with a Prettier plugin (among others). It’s a lot faster than running Prettier directly [1]

The dprint plugin wraps Prettier under the hood so compatibility is good. The perf wins come from formatting files in parallel and incrementally.

[1] https://david.deno.dev/posts/faster-prettier-with-dprint/

downvotetruth · 2 years ago
"I care that people can have a faster version of prettier, not how it's implemented." & "... for any project written in Rust" are contradictory.
topspin · 2 years ago
That was in the context of a question about whether external crates may be used. Rust is clearly implied.
lacker · 2 years ago
Looks like the bounty is at 20k now with contribution from @rauchg https://twitter.com/rauchg/status/1723400569392656771
maxloh · 2 years ago
Prettier runs on a single thread only. Running it on each thread could make it significantly faster [0].

[0]: https://github.com/microsoft/parallel-prettier

Alifatisk · 2 years ago
The performance gain is massive!
lakpan · 2 years ago
I don’t really get it. Prettier isn’t something that you need to run that often on the entire repo; you’re probably just running it on save in a handful of milliseconds or on a pre-commit on hopefully less than 10 files.

There are plenty other tools that often run on the entire repo, like tests, lint, build, type-checking, etc. focus on those. Bun showed that there’s a lot of space for improvement.

sophiabits · 2 years ago
Adding on to the other comment about CI setups, when I benchmarked removing eslint-plugin-prettier from my work’s ESLint configuration we saw a 37.5% speedup. On our fairly slow CI agents that ended up being pretty significant (~34s per run).

I _wanted_ to drop the plugin and have Prettier automatically run on as a pre-commit hook (as you suggested) but in the end I lost :) We wound up keeping the plugin and eating the slower pipeline time.

Of course there’s more overhead than just Prettier in this case—the ESLint plugin itself will be contributing towards the added runtime!—but depending on your codebase and CI machines, Prettier can definitely slow things down quite a bit. CI taking over a minute or two is a big drag on developer productivity imo.

Ideal world is that everyone is on board with code formatting as a pre-commit task that only touches modified files so it doesn’t bloat CI unnecessarily, but it’s not always possible :(

a-ungurianu · 2 years ago
I’ve done some research on this in the past. The reason that is so slow is because the eslint plugin runs prettier, does a diff between the result of prettier and the real file and then converts each diff entry into a eslint issues. Most of the pain comes from this remarshaling cost
cpojer · 2 years ago
I ran into the same issue and have been separating the prettier and eslint steps ever since: https://cpojer.net/posts/fastest-frontend-tooling-in-2022#es...
liamfd · 2 years ago
What we did to avoid that bottleneck was just turn off the prettier eslint rule in CI and just run the `prettier --check` command they recommend in their docs.

We also have it set to only run in changed files which helps a lot.

We generally don't link pre-commit hooks for standards though, hence the CI focus. Too easy to circumvent, and I'd rather pay for an external machine to check it when it matters (pre-merge by doing it on PR commits) than block my devs when it doesn't (every time they commit to a non-main branch).

biorach · 2 years ago
Many organisations have a CI step that runs a linter or formatter on all the code and fails if the tool makes changes.

The idea is to catch cases where contributors failed to run the tool on save.

On large code bases this can be time consuming.

zegl · 2 years ago
The trick to make CI in large codebases fast, or how to make any system fast, is to only test what’s changed.

In CI you only need to lint the files that have changed, or run the tests that depend on code that has changed etc.

This way the time it takes to execute the tests scales with the amount of changes, and not with the total amount of code.

volent · 2 years ago
I believe you don't see the big picture.

What you just described is running on thousands of machines several times a day.

the_other · 2 years ago
Or, better still, just remove prettier and free yourself to write more expressive code again.
tuananh · 2 years ago
it's very common to have a lint/format step that make sure all files are well-formatted in Pull request.
crabmusket · 2 years ago
This is such a waste of electricity IMO. Just occasionally batch-fix all files for syntax. Semantic linting can run in CI, as it's about catching bugs. But formatting is for long-term consistency and doesn't need to be done on every commit.
cryptos · 2 years ago
> The main issue is that none of them match the long tail of formatting logic of prettier. I'm putting up a $10k bounty for any project written in Rust that passes > 95% of the prettier JavaScript tests.

95 % is not the "long tail" in my opinion. Maybe 100 % is too hard, but 95 % is rather low.

zImPatrick · 2 years ago
Why does everything need to be written in rust these days?
rafaelmn · 2 years ago
TBH this sounds like a great candidate for rewrite - likely millions of hours of runtime/hour. Prettier is written on a platform with a history of such rewrites getting 10x perf increases. The only alternative that comes to mind is Go but I'm not that offended by Rust choice - has pros and cons.
tubthumper8 · 2 years ago
It's not just speed, but also correctness. A quick search in the Prettier repo shows plenty of issues for "undefined is not a function", "cannot read property X of undefined/null", and other such errors that are avoidable in a language with a good type system.
citrin_ru · 2 years ago
It’s a great candidate for a rewrite buy why not Go, Zig, Ocaml, Haskell e. t. c.?
eyeris · 2 years ago
The swc project reimplemented some of the js build toolchain in rust which led to some great speed ups.

I’d imagine that this call for rewriting prettier is inspired by swc

Not sure if updating prettier will benefit as much as transpilation.

[0] https://swc.rs/docs/benchmarks

pjmlp · 2 years ago
Any AOT compiled language will do, but only Rust will give extra hacker credo points.
ramon156 · 2 years ago
As a rust dev I'm just as perplexed. I've seen preprocessors and stuff like tailwind being rewritten in Rust aswell. Why though (
Tade0 · 2 years ago
As a (nominally) front-end developer the RIIR movement's energy is similar to how the front-end community approaches things.

Overall Rust and its community have many traits that make them very approachable to people not expected to know anything about systems programming, namely:

-Package manager with a familiar philosophy behind it (and not the sort of hell you have to deal with setting up a C++ development environment).

-Outputs WASM without much bureaucracy and advertises it.

-Friendly compiler messages. I don't recall seeing "perhaps" in a compiler error message before.

The people designing Rust put much effort into making the language as approachable as possible and that's the net effect.

That is not to say this is going to be necessarily successful, but so far the enthusiasm is there.

mtsr · 2 years ago
Some dev workflow tools really benefit from being very fast. And parsing and ast building and the like are actually things that rust is quite good for.

That doesn’t mean it couldn’t be done in other languages, but it’s a weighing of trade-offs. And of course, groups of people, such as developers on socials, are quite sensitive to popularity.

biorach · 2 years ago
> I've seen preprocessors and stuff like tailwind being rewritten in Rust aswell. Why though

a) speed. On large codebases each preprocessor (and there may be many) can take a long time

b) Rust has several features which make it quite suitable for writing pasrers and suchlike

fullspectrumdev · 2 years ago
If it speeds up a build/CI pipeline it’s a worthwhile investment for a lot of folks
civilitty · 2 years ago
johnisgood · 2 years ago
It is a sad moment for me, because I have more difficulties understanding Rust than Haskell, and believe me, Haskell is very difficult already. I know C, Lua, OCaml, Go, and the like, but Rust? Not really. What I hate the most is reference implementations being in Rust instead of C. C is almost like a pseudo-code.
darthrupert · 2 years ago
Because everything is written in nodejs.
yoav · 2 years ago
Can you pls rewrite your comment in rust.
yipbub · 2 years ago
Is this going to be a race? Multiple people implementing it in parallel and one claims the prize?