Tom S wrote:
Andrei Alexandrescu wrote:
Tom S wrote:
Don wrote:
bearophile wrote:
While writing code that works on matrices I have found something curious...

Here's what I think is going on. AFAIK, D hasn't got any special code for initializing jagged arrays. So
        auto A = new double[][](N, N);
involves N+1 memory allocations.
As well as being slow, this is going to give horrible locality of reference in the memory allocation. You'll suffer cache misses on almost every array access!

I don't think this has anything to do with bounds checking.

Nah, it's about NaNs :)

Version #1 initializes C to NaN, Version #2 initializes it to 0. The 'init mats randomly' loop doesn't touch C at all, thus all the latter additions leave C at NaN, causing lots of FP exceptions.

We actually had something like this happen in Deadlock a few times in the animation subsystem. Brought the framerate down to a single digit :D

I swear I wrote my previous message before reading this :o). Damn you're right. I hadn't seen the bug in initialization. But since version #2 uses malloc, isn't that memory garbage (possibly, but not necessarily, zero)?

Damn, you're right too ;) I always forget whether malloc or calloc initializes to zero. According to my tests, DMD/Tango's malloc returns mostly zeroes in bearophile's test anyway.

In my experience malloc returns zeroed memory around the start of an application (due to the OS applying simple security considerations). Later on, as memory is reused, there's no more zeroes - it's whatever you left in there.

Andrei

Reply via email to