I’ve had programs where splitting the problem using multiple goroutines ran slower than 1 goroutine because of some global variable access, which when removed, made the program much faster than the single goroutine version, as expected.
So - does Go do anything at all to code that is being run as a goroutine … like adding mutexes around things it sees as being potential races problems? If not, what would explain the slowdown in that case?
Go does not add “mutexes” as it cannot read minds to figure out what a program should do.
One possible explanation is garbage collection. Global variables are always on the heap, thus every garbage collection cycle (in go this is constantly, non freezing your program) has a bit more work to do because of that variable. Now, on global variable is nothing by itself but it if has a lot of pointer fileds which point to other variables, each pointer will be scanned through be the garbage collector.
that doesn’t account for it, as in the example, the global variable was an int, used to accumulate a count.
the goroutines were updating the count. When I removed that global variable update, it sped up.
I’ll have to try and re-create the specific example - it was a mandlebot generator.
When all goroutines access the same variable, cache invalidation might play a role.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.