Shy got curious and changed the leaf list in the Go program to be like the C code. This saved about a second. The new runtime is 3.22 seconds. That'sI was a little more careful allocating memory on the stack in C and represented the leaf list for the topological sort as a n x 3 array rather than an array of slices. Maybe this last change accounts for most of the improvement. It's difficult to tell without testing.
3.22 / 1.23 = 2.62
times slower than the C code. Shy looked shyly at the ground.
Scratchy stretched, yawned and meowed something about fish. At this I instrumented the code and found that 240879 heap allocations are made during a run but no fish.
Unfortunately, there is no explicit way to allocate an array sized at runtime from the stack in Go and all the slices appear to escape to the heap. Maybe that's where the fish went as well.
According to Scratchy, one of the design goals for the Go programming language was to make any type of performance engineering extremely difficult. The other objective was to hide all C-style preprocessing directives as magic comments with no syntax checking. I'm not certain about that, but somehow trying to write well-performing Go code reminds me of my attempts in the 80's to write well-performing Basic programs. It's as if the language were designed to prevent such things.
Statistics: Posted by ejolson — Sun Jun 01, 2025 5:05 am