Skip to content
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

A question about coding choices used here. #14

Closed
truegoodwill opened this issue Mar 9, 2021 · 4 comments
Closed

A question about coding choices used here. #14

truegoodwill opened this issue Mar 9, 2021 · 4 comments
Assignees

Comments

@truegoodwill
Copy link

Hi @StephenCleary, thank you, I'm really enjoying reading and learning from this code and applying it to my own projects. I have a question which could possibly lead to a small improvement in the BoundActionField ... I'm not sure ... here goes.

You have the following property getter ... I understand perfectly ... it's simply checking what's stored in _field.

public bool IsEmpty => Interlocked.CompareExchange(ref _field, null, null) == null;

But later in the TryUpdateContext method you have the following line of code:

var original = Interlocked.CompareExchange(ref _field, _field, _field);

As far as I can interpret, its purpose is exactly the same as in the property getter ... to grab the value of _field. But here you've used _field, _field for the next two parameters instead of null, null. I don't understand why. It seems "bad" to me, in comparison to supplying nulls. Is there a deliberate reason to this choice, or just randomness?

Thank you.

@truegoodwill
Copy link
Author

PS: I've been reading ImmutableInterlocked.cs as well.

Is there anything you would change about your code after reading this? In particular, I noted that this class uses Volatile.Read where you would use Interlocked.CompareExchange(ref _value, null, null) ... is there a difference / comparative advantage?

@StephenCleary StephenCleary self-assigned this Nov 17, 2021
@StephenCleary
Copy link
Owner

Yes, it should be using null, null instead of _field, _field. Thanks!

Regarding Volatile vs CompareExchange, they're pretty much equivalent. The CompareExchange technically doesn't have to do a full memory fence like Volatile.Read does, but I believe it does anyway for historical reasons.

@truegoodwill
Copy link
Author

Thank you

@StephenCleary
Copy link
Owner

Fixed in v2.3.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants