-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Compiler takes too much time and memory to build project #12879
Comments
Hi! In general the answer is "Yes, we know it, the compiler is slow and there's nothing you can do about it" I compiled that project on my machine:
So about 40 seconds? But it's the first compilation. Subsequent compilations should take less time:
So about 20 seconds. It's still a lot: you'll always have to wait at least 20 seconds to compile the entire program. The way Crystal exists, there's no way around it. My machine in a Mac M1. So one solution could be: get a better machine. The other solution would be to introduce incremental or modular compilation, but that's impossible without greatly changing the language, and it also involves a lot of thinking and effort. It's unlikely to happen soon, or ever. |
Also about the memory: the compiler will hold the entire program (your code but also libs) in memory, for every compilation. That's the way Crystal works. So it's bound to use lots and lots of memory. And, like before, there's nothing you can do about it. |
I was somewhat familiar with the time issue due to Crystal's type inference and the like, but perhaps the memory issue is more approachable? I tried doing a memory profile, unsurprisingly I suppose most allocations seem related to ASTNode. Perhaps that structure can be optimized. I think even if you put the whole program in memory, I feel like the memory usage should be lower than this. |
I tried to do that in the past but it led to subtle bugs. Im any case, such optimizations will be good for a while but then your program keeps growing and it will inevitably use more and more memory. Without modular compilation all such optimization, in the grand scheme of things, are useless. |
Have any experiments been done with using a custom memory allocator such as mimalloc? From another compiler project, switching to it had a significant performance effect. |
I think someone tried using jemalloc but I don't know much about it. That said, the memory allocator is boehm GC and I don't know if that can be integrated with another allocator. |
Actually I think @jwoertink did tests with different allocators for building Lucky and I remember something about large speedups |
That was actually @wyhaines that I copied it from 🥳 but jemalloc gives you a small boost, and hoard gives you a huge boost. Though, I believe there's some other tradeoffs that I can't remember at the moment. I think you just point your |
Citing @BlobCodes from #13060 (comment)
Yes, that's definitely a route for performance improvements. There have also been some efforts about switching the GC implementation, which would affect runtime performance of all Crystal programs. But optimizing the compiler architecture would be a good thing |
fwiw, i just made changes to the project above to address build times. a huge contributing factor was the inlining of the functions taking blocks. using procs instead of blocks cut the executable size by about a third. i don't think this is a general criticism of the implementation of blocks—i just happened to be passing blocks to large functions in generated code. there's definitely more to criticize in my code. |
Bug Report
While trying to build this project, I noticed that Crystal takes a long time, and a lot of memory to finish compiling. See the stats below of running
command time -l crystal build src/ktistec/server.cr
(-v
on Linux):That's 2GB of memory and close to 3 minutes to compile. I noticed this issue on a server with limited RAM as the build was failing there. Running
cloc --include-lang crystal src lib
returns:2GB for 32k lines of code seems excessive. Is this expected?
The text was updated successfully, but these errors were encountered: