-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Description
Currently the System.Lazy implementation has three member fields, and the hot path of getting the value after it has been created involves 1 or 2 conditionals ( including the .net framework implementation ).
My suggestion is to have only one field that references an object that can supply the value, and the state/mode is represented by the type of object referenced. So initially it references a object that will construct the required value, while when it is in the value set state it references an object that just returns the store value, and for the error state, throws the captured exception.
So it replaces conditions(s) with a virtual call.
My suggested implementation can be seen here:
https://gist.github.com/odewdney/3835c52528295d0d39e76e6e31bb6fa3
BenchmarkDotNet suggest a 10% saving in cpu and memory against the .net framework implementation - could be more depending on usage modelling - on a tight loop calling Lazy.Value