News for hackers. Stuff that matters.
Re:The secret life of NaN by skybrian on Thursday April 05, 2018 @05:28PM (#16768915)
Go is sort of the opposite of this. If you do something like:

func sum(a, b float64) interface{} { return a+b }

It will allocate memory for the float, because an interface type always contains two pointers. [1]

It's pretty crazy that the second word in a Go interface has to be a pointer. But I suppose if dynamic types were used more, they would be optimized more.

[1] https://www.darkcoding.net/software/go-the-price-of-interfac...

Re:The secret life of NaN by zaarn on Friday April 06, 2018 @05:35AM (#16772027)
I think it's more about not having exceptions. If the second word is always a pointer then you can make code that blindly assumes such and optimize for that.

If it may not be a pointer you have to branch, which can be add some overhead to the lucky path (every pointer deref will have to wait for type comparison in machine code)

If the float has just been written, the data should be in cache so the performance penalty is probably not too bad in most cases.

Re:The secret life of NaN by skybrian on Friday April 06, 2018 @05:11PM (#16777484)
Well, the problem is when you have a []interface{} that contains a lot of float64 mixed with other things. If the size of the slice is large, I don't think you can assume it's all cached.
Re:The secret life of NaN by zaarn on Monday April 09, 2018 @02:34AM (#16790873)
If the slice is large, it's less likely to be cached either way.
Re:The secret life of NaN by skybrian on Tuesday April 10, 2018 @05:30PM (#16805652)
True, but there is also locality of reference (having everything in a contiguous block uses less cache than pointer-chasing) and garbage collection pressure.

In principle, using NaN boxing or a similar technique to store non-floats, you should be able to store floats in an []interface{} as efficiently as in a []float64. Scripting languages do this, but not Go.