-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Add a way to force-initialize a System.Lazy #27509
Comments
I do (Also, changing the syntax from |
...I have to ask, if you're force-initializing a Once you start doing things like Note that for the pre-initialization to be worth it, you have to either force the |
Also to me it's strange use lazy as "cache" system...i think that Lazy is useful to avoid "unuseful" allocation, preload seems opposite than lazyness. But maybe with complete "scenario" i could understand well the idea. |
I made this feature request because I recalled that I needed it. Indeed, I have my own Lazy implementation because One scenario might be a lazy initialized cache but you want the cache to be pre-initialized in the background but only after a few seconds have passed in order to not slow down the start of the application. Here, we cannot just use a I have now checked where I used this feature... It was in just two fairly esoteric scenarios that are not worth posting here. So I understand the interjection that this might not be very useful. Feel free to close this if the team decides against having this feature! |
@GSPP -
...the exception caching is considered a feature. As for worries about |
Retries cannot really be placed inside because retrying might take extremely long (or never complete). All threads consuming the lazy would be held up indefinitely. In a web app that quickly consumes the entire thread pool. With an async lazy the same issue exists. Unbounded resources will be consumed and incoming requests will be delayed indefinitely. Rather, failures must propagate out of the lazy. Yet the lazy must be restartable. Regarding the delay init scenario, |
This sounds like a separate api-suggesstion, perhaps propose that too? |
You mean a way to reset the lazy? I personally really needed that for caching scenarios and it was an important reason to create my own lazy. Implementing that could be difficult and force worse performance. Now, the lazy transitions only forward in time. With resetting it can transition back into old states (ABA problem). That makes low-synchronization code problematic to achieve. |
@GSPP, could you give a link to your implementation, please? |
@tsvx - I've become less convinced that this is a necessary new feature, especially since the reason you're quoting is a red herring. Essentially, |
It has been a while and this issue hasn't had any recent activity. Is there any reason the suggestion at https://github.com/dotnet/corefx/issues/32551#issuecomment-425735834 (use a discard) isn't satisfactory? |
I have laid down my case for this feature, and the discussion was pretty exhaustive. If the team is not convinced at this point, I think it's time to let this go 😄 |
Thanks for the suggestion and discussion. I'm going to close it. |
Lazy
should have a way to make it compute the value on demand. This could be as simple as this:This is useful if
Lazy
is used as a cache and you want to pre-initialize that cache. That pre-initialization could be done in the background byTask.Run(() => myLazy.ForceValue())
. I'm sure aForceValue
is handy in other situations as well. It does not clutter the class much and is trivial to implement.The text was updated successfully, but these errors were encountered: