Currently, when deref is called in Delay.java, a lock on the Delay is always acquired.
This is wasteful as most of the time you just want to read the val.
The attached patch changes this behaviour to the following:
- val is initialised to a known secret value. (During its constructor so it is visible from any thread).
- When deref is called, val is checked to the known secret value.
- If it is not the secret value, then it has to be the value computed by the function and we return it.
- If it is the secret value, then we lock this object and revert to the current behaviour.
This is faster than what is done currently and can be very much faster if there is contention on the delay.