mixpanel / mixpanel-swift

Official iOS (Swift) Tracking Library for Mixpanel Analytics
https://mixpanel.com
Apache License 2.0
434 stars 234 forks source link

Use NSLock instead of DispatchQueue #554

Closed muukii closed 1 year ago

muukii commented 2 years ago

Thanks for this open-sourced library!

In the case of performing very small tasks, performing GCD takes a pretty large cost rather than actual operation inside the critical scope. So I propose this PR that uses NSLock to get mutual-exclusion.

Referencing this note: https://gist.github.com/tclementdev/6af616354912b0347cdf6db159c37057

queue.async() is wasteful if the dispatched block is small (< 1ms), as it will most likely require a new thread due to libdispatch's overcommit behavior. Prefer locking to protect shared state (rather than switching the execution context).

And I found some cases which are not protected from concurrent operations.

⚠️ This modification is still very part of this library. It affects consistency against the code entirely.