Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
74735: util/mon: add optional threadsafety to BoundAccount r=dt a=dt BoundAccount is often passed around in the implementation of a processor or task along side the maps, slices, etc that it tracks so that it can be Grow()ed and Shrink()ed as they are mutated. However, while most SQL processors and the libraries they use are all single threaded, many bulk and cdc processors are not. The fact that boundAccount is not threadsafe can then make it more difficult to use in these areas. A wrapper that adds threadsafety could help but this is difficult to use as functions that take a BoundAccount by name cannot be passed a wrapper, since it is not an interface. Another approach is to make a new boundAccount for each goroutine, but this requires then careful merging of each of those back to the account that was originally passed. Instead, this change adds _optional_ threadsafety to mon.BoundAccount by allowing caller to configure a field of it with a given mutex. When this field is non-nil, the usage methods like Grow, Shrink, and Resize will acquire and release it when called, making them threadsafe. Lifecycle methods, like Init, Close or Empty are not included here. Checking for this mutex in existing, single-threaded users will add some additional overhead; BenchmarkBoundAccountGrow shows this to be about 1.2ns on my machine. name old time/op new time/op delta BoundAccountGrow-10 2.21ns ± 4% 3.42ns ± 3% +55.23% (p=0.000 n=10+9) Release note: none. Co-authored-by: David Taylor <[email protected]>
- Loading branch information