Factory icon indicating copy to clipboard operation
Factory copied to clipboard

Minor performance improvement for LazyInjected

Open fabiankr opened this issue 1 month ago • 0 comments

In a project where we are heavily using FactoryKit I noticed an issue in combination with Swift Concurrency.

One class that we are accessing through a factory has a slow init and due to the usage of the globalRecursiveLock it can easily block all cooperative queues when many instances are resolved in async contexts leading to thread contention. You can see this in the screenshots.

Ultimately, we need to refactor our code and move the initialization out of the init. But this PR is a very minor performance improvement with @LazyInjected. It always calls the reset(options:) function when initializing with options: .none. So at least here we can skip the lock. In the end it still needs the lock when resolving the dependency, but at least it's only locking once instead of twice then.

screenshot1 screenshot2

fabiankr avatar Nov 26 '25 14:11 fabiankr