fix: use a sync.Map to prevent "concurrent map read and map write" fa… by tinmrn · Pull Request #1923 · go-task/task

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the go docs of sync.Map:

The Map type is optimized for two common use cases: (1) when the entry for a given
key is only ever written once but read many times, as in caches that only grow,
or (2) ...

I think that applies here, because potentially a lot of lines get written and only relatively few prefixes will be used.

I think speed is important here since task execution blocks waiting for output to be written. But I have no strong opinion about either way, and we would have to benchmark to check which is actually faster.

One drawback of sync.Map over a mutex is that sync.Map is not type safe, but I think its usage is easy to oversee in this case.