3 ms·
I know it's fairly pointless performance bashing but I just couldn't stop myself :) # In the crystal-lang/crystal repo $ find . -name '*.cr' -print0 |
by RX14 9y ago
I know it's fairly pointless performance bashing but I just couldn't stop myself :)
# In the crystal-lang/crystal repo
$ find . -name '*.cr' -print0 | wc -l --files0-from=- | tail -n 1
252745 total
$ time bin/crystal tool format
Using compiled compiler at `.build/crystal'
bin/crystal tool format 0.62s user 0.04s system 106% cpu 0.625 total
and only 48ms to formal the longest file in crystal (the parser)!
Performance makes little practical difference as long as formatting a single file is fast enough to be on-save, but it does highlight the very interesting way that the crystal formatter is implemented. I'm not sure how rustfmt is implemented but crystal's formatter parses the file into the AST, and then does a single visitor pass of the file, in order, with a lexer in tow. It then basically reconstructs the entire file from scratch using the data both from the AST and the lexer (the AST visitor pass and lexer position have to be kept in sync). And surprisingly enough, this doesn't even make the formatter "too strict" in the way that it wipes out all existing style information as one would expect. It's a really cool - if a little messy - tool and one of I think my favorite parts of crystal (it doesn't have any config options either).