r/programming • u/DanielRosenwasser • Mar 09 '23
TypeScript's Migration to Modules
https://devblogs.microsoft.com/typescript/typescripts-migration-to-modules141
Mar 10 '23
One nice surprise we found from using esbuild was that on-disk size was reduced by more than we expected. It turns out that a big reason for this is that esbuild uses 2 spaces for indentation in output instead of the 4 spaces that TypeScript uses. When gzipping, the difference is very small; but on disk, we saved a considerable amount.
Even though this is one of the bike-shed-iest things to bring up, it does make one wonder about the results of tab based indentation...
30
u/Bake_Jailey Mar 10 '23
It's smaller, of course, but esbuild is hardcoded to 2 spaces so that's what we get, and it'd be a pain to try and change that in a post-processing step without either trying to redo source maps, or, end up with a "release" build of TypeScript (which complicates the build).
1
u/hogfat Mar 10 '23
How much smaller would the functional diff have been if it were preceded by a format-only PR to get to two spaces?
7
u/Bake_Jailey Mar 10 '23
Great question. I can somewhat test this by taking the two packages and formatting them via dprint to 2 space indents. Comparing only the .js files that exist in both packages, here's the difference between 4.9 and 5.0, if both had been indented by 2 spaces:
4.9/: total 44M -rw-r--r-- 1 jabaile jabaile 2.8K Mar 10 09:06 cancellationToken.js -rw-r--r-- 1 jabaile jabaile 5.5M Mar 10 09:06 tsc.js -rw-r--r-- 1 jabaile jabaile 11M Mar 10 09:06 tsserver.js -rw-r--r-- 1 jabaile jabaile 11M Mar 10 09:06 tsserverlibrary.js -rw-r--r-- 1 jabaile jabaile 9.9M Mar 10 09:06 typescript.js -rw-r--r-- 1 jabaile jabaile 7.4M Mar 10 09:06 typingsInstaller.js -rw-r--r-- 1 jabaile jabaile 1.2K Mar 10 09:06 watchGuard.js 5.0/: total 33M -rw-r--r-- 1 jabaile jabaile 3.8K Mar 10 09:08 cancellationToken.js -rw-r--r-- 1 jabaile jabaile 5.7M Mar 10 09:08 tsc.js -rw-r--r-- 1 jabaile jabaile 8.2M Mar 10 09:08 tsserver.js -rw-r--r-- 1 jabaile jabaile 8.9M Mar 10 09:08 tsserverlibrary.js -rw-r--r-- 1 jabaile jabaile 8.3M Mar 10 09:08 typescript.js -rw-r--r-- 1 jabaile jabaile 1.9M Mar 10 09:08 typingsInstaller.js -rw-r--r-- 1 jabaile jabaile 2.4K Mar 10 09:08 watchGuard.jsYou can see that most things got smaller.
tscactually got slightly larger, buttypingsInstallergot hugely smaller thanks to tree shaking.29
Mar 10 '23
Yeah in every debate about tabs I've seen the space proponents say the extra size doesn't matter. And to be honest I would have agreed with them even though tabs are clearly superior.
This is quite surprising. Although... Surely you care about disk space that much you just use a minifier?
20
Mar 10 '23
Tabs for indentation, spaces for alignment
18
2
u/Uristqwerty Mar 10 '23
Tabs could also be used for alignment, if editors wanted to implement it: Grab a whitespace character that the compiler already ignores, and declare it means "set tab stop". Then, program the IDE to recognize when a line contains one or more, and have the first N tab characters input jump to the same column as the prior's tab stops.
Now, things will even remain automagically aligned after length-changing refactors! Nested blocks can use <tab><tabstop> sequences to further cut down on filesize!
1
u/vytah Mar 10 '23
You don't need extra control characters if you want a bit more restrictive scenario.
Here's an untested algorithm:
For each line, check if it contains a tab character.
Assume all tab-less lines are properly laid out.
Consider all the other lines, separated by the tab-less lines into groups of consecutive lines that contain tabs.
For each such group:
Take all the prefixes before the first tab character and measure their lengths.
Pad them with spaces to length max(max(prefix_lengths)+minimum_tab_size, indent_size), where minimum_tab_size and indent_size are UI-defined parameters
Treat remainders of the lines after the first tab character as a line list and run this algorithm recursively. Recursion will stop, as its depth is limited by the highest number of tabs in a line.
This should work the same as usual method if tabs are used only for indentation, and would automatically align columns in groups of lines, provided there's some separation between them. So for example Python, with its lack of block-ending lines, would work suboptimally, but most other languages would do well.
This is how it would work like (assume that tab characters were where the arrows are and that I haven't made a mistake in the algorithm):
int main() { → int→ count→ = 1; → double→ result→= 1; → if (count == 1) { → → printf(""); → } → count→ = 0; → result→= 0; }-2
u/lord_braleigh Mar 10 '23
How can you properly align if different programmers can have different tab stop sizes?
7
5
u/fioralbe Mar 10 '23
the solution is to only align things at the same indentation level; that is each line matches the regex
/\t*[^\t]*/This makes it easy to make alignments like
while (true) { <tab>const aaaaaaa = 1; <tab>const aaaaaa = 1; <tab>const aaaaa = 1; <tab>const aaaa = 1; <tab>const aaa = 1; <tab>const aa = 1; <tab>const a = 1; }5
u/AlteaDown Mar 10 '23
And then someone comes along, and they think,
hmmm...
aaaaaaaisn't that great of a variable name. This is more like abbbbbbbbb.So they refactor rename
aaaaaaatobbbbbbbbb, and then in their git diff they end up changing every one of the lines in the while loop as well....or they YOLO and break the formatting. Then this is repeated everywhere in the codebase, and everyone is frustrated with the very strange and inconsistent formatting.
1
u/fioralbe Mar 11 '23
I am not arguing that this alignment is good, i am only arguing that mixing tab and spaces is not an issue if you always use tabs for indentation and spaces for alignment.
The only request is that you need to only align within the same indentation block.
Personally I do align code like that at times where there are important repetitions:
var field_foo1 = some().kind().of.stuff.foo1 var field_foofoo1 = some().otherkind().of.stuff.foo1 var field_foofoofoo1 = some().kind().of.stuff.foo1
is less readable than
var field_foo1 = some().kind().of.stuff.foo1 var field_foofoo1 = some().otherkind().of.stuff.foo1 var field_foofoofoo1 = some().kind().of.stuff.foo1
as this highlights the difference between similar expression
0
Mar 10 '23
[deleted]
6
Mar 10 '23
spaces for alignment
0
Mar 10 '23
[deleted]
2
Mar 10 '23
You're getting downvoted because it absolutely does work with multi-line trailing comments.
And probably also because multi-line trailing comments are awful style.
1
Mar 10 '23
You mean comments at the end of the line?
They're pretty awful even when you use spaces. You should pretty much never use single-line trailing comments let alone multi-line ones.
3
u/GrayLiterature Mar 10 '23
I disagree with your right to tab based indentation, but god dammit I will fight for your right to do so.
149
u/danielbiegler Mar 10 '23
What was years in the making turned into a hefty pull request with over 282,000 lines changed.
The PR description:
minor tweaks
+282000 -617327
15
u/therearesomewhocallm Mar 10 '23
Jokes aside, it was actually +282,700 −284,842, and there were lots of details listed in the commit message(s).
30
u/I_AM_GODDAMN_BATMAN Mar 10 '23
i like huge pull request like this. it makes git blame unreliable, git log useless, the tribal knowledge within destroyed, and the dead body I hid gone forever.
8
u/LyD- Mar 10 '23
You have to make sure to squash all PRs on merge too. Otherwise, individual commits get merged into the main branch and they might accidentally show future developers why that line of code was changed or where that dead body came from.
Even better,
git logwill be linear! You don't want people to see where branches were created or merged, that would be far too confusing. You definitely don't want a commit to give you a deterministic copy of the codebase at the time it was originally created, either. We can all relive the glory days of SVN!If any nerds disagree with you, do this: type
git log, show how the commits from totally different branches are out of order, and make sly comments about how they're big nerds if they try to explain. Make sure everyone knows you don't understand git by loudly proclaiming it every time the topic comes up.2
u/Tarmen Mar 10 '23 edited Mar 10 '23
What's the problem with git log? I use it in two modes:
git log --all --graph --oneline --decorateto get a graph view
git log -L<start>,<end>:file --no-patchto get all commits which affected some source span, possibly with-G regexto filter to commits whose diffs match the regexAnd neither is affected by monster commits. In fact I've written a vim plugging wrapper for the second because it deals with monster commits and formating noise so much better than git blame.
Is there some other cool invocation that I'm missing and which would be affected by large rewrites?
0
Mar 10 '23
[deleted]
11
u/I_AM_GODDAMN_BATMAN Mar 10 '23
Neat, didn't know about
blame.ignoreRevsFile. I'll remember next time on huge codebase changes.10
u/Bake_Jailey Mar 10 '23
This PR was also one we didn't squash; in fact the first commit in the branch (more or less) was a huge automated commit to dedent the entire repo, which keeps git happy and diffs clean.
You can see the entire stack of changes I merged visualized kinda like it'd look in Gerrit here: https://github.com/jakebailey/TypeScript/pull/1
1
u/confusedpublic Mar 10 '23
The Python auto formatted black recommends using that feature when you first apply black.
40
u/danielbiegler Mar 10 '23
We found that often 10%-15% of our parse time could be dropped from switching to var everywhere. This translated to up to 5% of our end-to-end compile time being just these let/const checks!
This one surprised me tbh. That's wild! Let's hope esbuild can help with that in the future. Esbuild switching to tabs instead of spaces would be neat too.
What an incredible feat you guys. Amazing work. Also compiling everything for this humongous blog post must have taken forever, hats off to you too. The sheer scope of this whole endeavour is daunting but you persevered! Cheers everyone!
15
u/a_flat_miner Mar 10 '23
Didn't they spend the better part of a decade+ telling us to use 'let' instead?
23
u/duxdude418 Mar 10 '23 edited Mar 10 '23
The maintainability reasons to use
letandconststill hold for most codebases. If you’re writing something low level like a compiler or its ouptut, then we can talk about optimizations that go counter to best practices.6
Mar 10 '23
Even then, you should probably use let anyway, and have a build step that performs optimizations like that.
16
24
23
u/gredr Mar 10 '23
One nice surprise we found from using esbuild was that on-disk size was reduced by more than we expected. It turns out that a big reason for this is that esbuild uses 2 spaces for indentation in output instead of the 4 spaces that TypeScript uses. When gzipping, the difference is very small; but on disk, we saved a considerable amount.
You know what uses 50% less than two spaces (and has the nice side effect of not requiring us to all agree on how wide our indentation should be)?
Yeah, tabs.
1
-10
u/mechpencillover Mar 10 '23
RemindMe! 1 minute
1
u/RemindMeBot Mar 10 '23
I will be messaging you in 1 minute on 2023-03-10 08:32:16 UTC to remind you of this link
CLICK THIS LINK to send a PM to also be reminded and to reduce spam.
Parent commenter can delete this message to hide from others.
Info Custom Your Reminders Feedback 0
Mar 10 '23
[deleted]
5
u/mechpencillover Mar 10 '23
Ahhh.....sorry I just stumbled on this RemindMe bot and kinda had novice's curiousity to check if it actually works (that it obviously does lol). I randomly checked on this post which I wasn't intending to be reminded of lmao
0
-4
u/elteide Mar 10 '23 edited Mar 12 '23
I wish MS create a native or a .net typescript version
Why the downvotes? am I missing something? hahahaah
7
0
5
u/uuuuuuuaaaaaaa Mar 10 '23
Glad to see a focus on Typescript’s performance. This is the only language where I’ve experienced autocomplete slowing down to a crawl after a project reaches a certain size.