News for hackers. Stuff that matters.
Re:Optimizing Ray Tracing in Haskell by snawaz on Sunday July 19, 2020 @12:02PM (#23890230)
That is puzzling to me as well, particularly because `top` and `htop` on my machine don't even touch `50 MB`. That means, the first figure must be something else and I don't find a good explanation for that. I came across people who suggested to ignore that, e.g https://stackoverflow.com/questions/61666819/haskell-how-to-...

If anyone knows the exact reason for that, then please enlighten me as well. Even this otherwise awesome tutorial ignores that value: https://youtu.be/R47959rD2yw?t=1306

Re:Optimizing Ray Tracing in Haskell by sukilot on Sunday July 19, 2020 @01:02PM (#23890650)
The runtime allocates and frees heap memory while performing local calculations.
Re:Optimizing Ray Tracing in Haskell by snawaz on Sunday July 19, 2020 @01:33PM (#23890883)
> The runtime allocates and frees heap memory while performing local calculations.

1) If it allocates, why does `top` not show it?

2) Why does it allocate SO MUCH when it uses only a fraction? Looks like a very bad strategy to allocate 2800 times more memory than actually used?

Re:Optimizing Ray Tracing in Haskell by CyberDildonics on Sunday July 19, 2020 @03:21PM (#23891683)
You are looking at the current amount of memory that the process is using in top, but your profiling is probably showing the total allocated while the process ran. This means there are probably LOTS of memory allocations that get freed immediately instead of the program allocating memory once and reusing that heap memory. In the case of an image of this size you could probably just put everything on the stack.

When optimizing, excess heap allocations are the first thing to look at, since they are usually inside inner loops, which almost always means they are not necessary. It definitely should not be ignored by someone looking for speed.

Re:Optimizing Ray Tracing in Haskell by snawaz on Monday July 20, 2020 @04:50PM (#23901756)
> In the case of an image of this size you could probably just put everything on the stack.

That sounds interesting and will probably make it more efficient. But how do I put things on the stack? As per this answer on Stackoverflow, the runtime allocates memory on the heap to call functions (and probably programmers don't have any control over that?):

https://stackoverflow.com/a/5132862/415784

Re:Optimizing Ray Tracing in Haskell by CyberDildonics on Monday July 20, 2020 @05:46PM (#23902250)
I don't know how to avoid heap memory allocations in haskell or make sure memory is on the stack, but both are very easy to do in C++.
Re:Optimizing Ray Tracing in Haskell by snawaz on Monday July 20, 2020 @06:19PM (#23902480)
>I don't know how to avoid heap memory allocations in haskell or make sure memory is on the stack, but both are very easy to do in C++.

Yup. I know it's easy and the default behavior in C++.

This Haskell page says, it is pretty common to allocate and deallocate memory immediately, and that GHC handles this pretty efficiently, which probably explains why Haskellers dont seem to care about this (though it seems weird to me).

https://wiki.haskell.org/GHC/Memory_Management