-
-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Option to speedup Scoped services creation without concurrent Func or Lazy dependencies in object graph #40
Comments
This issue may help with #27 |
Did some related work in #1 |
Isn't the point of I guess there is a narrow use case where you might want to share the T in Lazy across threads, but that seems like a bad idea. |
Another option is to allow the Registrar to specify the LazyThreadSafetyModeview=netframework-4.7.2 or to build an expression tree for LazyInitialier.EnsureInitialized which builds the computation graph. Right? |
I am not sure I am following you. Btw, I am not renaming |
Ok, feeling dumb but I will try to assign scoped or any the same expressions into variable and use them in global |
My thought was that , as an end user of your library, I may want to "bring my own threading model" so that you don't have to guess as to whether I have concurrent dependencies.
I was not looking at the problem from this perspective. I was just thinking, how can we remove concurrency altogether.
If I understand you, you're doing a compiler writer trick called Single Static Assignment. Effectively, every node in an expression tree is assigned to once, which makes constant folding analysis easier. The big win behind SSA form is it's trivial to determine constant fold opportunities, because every value in the tree is a constant, so you're literally just looking for duplicates to "lift" up to the top of the tree, thereby reducing memory allocations. |
Yes, it fits. So much of cool stuff to learn :) |
You're welcome, buddy! I should be thanking you for such a great library. I would trade 99% of the theory I know to be as good an engineer as you. If you want to learn more about SSA Form, books by Andrew Appel on writing compilers in and for ML are a great resource. He also has a bunch of brilliantly lucid research papers on compiler system design that can be applied to all kinds of non-compiler systems. In particular, he has a technical note on how to bootstrap a compiler which to me is still the gold standard. |
There is also a performance problem to consider when creating a scoped dependencies in the scoped sevice, that can be directly mitigated by using fan-out ImMapArray. When first created dependency will be added to scoped items ImMap, it effectively will cause to retry the holder sergice creation. Because upper service should be stored in the same items collection. Having split / slots of items should help cause the dependency and holder can be stored independently. |
As a result, I ended up compiling nested lambdas once. |
Scoped service in DryIoc is created via nested lambda
Scope.GetOrAdd(id, () => new ScopedX())
;Then inside
GetOrAdd
there is alock
held to ensure that() => new ScopedX()
is called only once.That guarantee ensures the service is created once.
Now, let's imagine the case when it may save us:
Now we have a concurrent logic which may access
XZ.Value
andXY.Value
in parallel. In this case, without alock
andlambda
, there is a chance to createScopeX
multiple times.BUT I would presume, that the case is rare and may be known before-hand.
Another option would be that creating a
ScopeX
multiple time is fine, we need just ensure that we select the same instance to be used further, and another instance is dropped (e.g. first created instance wins).So we may provide an option to simplify an expression by removing the nested lambda and creating service as
Scope.GetOrTrySet(id, new ScopedX())
. This option will greatly simplify expression compilation and call stack as well.Following will do:
The text was updated successfully, but these errors were encountered: