Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Add ConcurrentDictionary GetOrAdd/AddOrUpdate overloads with generic arg#1783

Merged
stephentoub merged 1 commit intodotnet:futurefrom
stephentoub:new_cd_overloads
May 23, 2015
Merged

Add ConcurrentDictionary GetOrAdd/AddOrUpdate overloads with generic arg#1783
stephentoub merged 1 commit intodotnet:futurefrom
stephentoub:new_cd_overloads

Conversation

@stephentoub
Copy link
Copy Markdown
Member

Adds one overload to each of GetOrAdd and AddOrUpdate. These overloads accept a generic argument that is passed through to any invocations of the supplied delegates, enabling developers to avoid delegate/closure allocations when more input is needed than just the key or existing value. The implementations are exact copies of the existing overloads then augmented with the additional parameter and threading that through to the delegate invocations.

For AddOrUpdate, there are two existing overloads with delegates, and #394 proposes that a new overload be added for each. I went back and forth on that, and for now this PR only provide a new overload for the one that accepts two delegates, as the other just didn't seem worth it (to my recollection we also didn't discuss both of these overloads in the API review... I hadn't actually realized that a new overload was proposed for each of them). If folks feel like it's really important to have the third one, though, I can add it.

Fixes https://github.com/dotnet/corefx/issues/394.

@stephentoub
Copy link
Copy Markdown
Member Author

cc: @justinvp, @AlfredoMS, @terrajobst

@justinvp
Copy link
Copy Markdown
Contributor

@stephentoub, thanks for putting this together!

for now this PR only provide a new overload for the one that accepts two delegates, as the other just didn't seem worth it

I agree; I'm good with just the single new overload for AddOrUpdate.

Question: Is there a need for separate TAddArg and TUpdateArg args for AddOrUpdate? Separate args weren't in the original proposal, but I now see that the extension method example you provided had separate args. The single TArg is sufficient for the cases I've run into and I like the simplicity of the single TArg, so I'm happy with the PR as-is. Just wanted to double-check.

Minor naming question: ImmutableInterlocked.GetOrAdd has a similar overload that takes a TArg, but instead of arg it is named factoryArgument. Should the parameter name be factoryArgument here as well to be consistent? I don't have a strong preference either way.

@stephentoub
Copy link
Copy Markdown
Member Author

The single TArg is sufficient for the cases I've run into and I like the simplicity of the single TArg, so I'm happy with the PR as-is.

Yeah, that was my reasoning as well (that, and if you really need different args you can package them up into a struct, and then just pay the copying cost of the one that isn't needed).

Minor naming question

No strong preference, either. @KrzysztofCwalina, suggestions?

@stephentoub
Copy link
Copy Markdown
Member Author

@dotnet-bot test this please

@KrzysztofCwalina
Copy link
Copy Markdown
Member

I agree that factoryArgument would be better. I think it clarifies what the argument is for.
Otherwise looks good! Thanks.

@stephentoub
Copy link
Copy Markdown
Member Author

Thanks, guys; fixed the new parameter's name.

@stephentoub
Copy link
Copy Markdown
Member Author

@dotnet-bot test this please

@justinvp
Copy link
Copy Markdown
Contributor

nit: factoryArgument or factoryArg?

@stephentoub
Copy link
Copy Markdown
Member Author

Oh, ok.

@stephentoub
Copy link
Copy Markdown
Member Author

Let's try that again ;)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't catch this earlier, but should this be TryUpdateInternal to reuse the once-computed hashcode?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it should be. This was just copy-and-paste from the current overload, which highlights that it, too, is calling TryUpdate instead of TryUpdateInternal. I'll fix this one here, but submit a separate PR to master to fix the current overload.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. And opened #1833 to address the existing TryUpdate calls.

Adds one overload to each of GetOrAdd and AddOrUpdate.  These overloads accept a generic argument that is passed through to any invocations of the supplied delegates, enabling developers to avoid delegate/closure allocations when more input is needed than just the key or existing value.  For AddOrUpdate, there are two existing overloads with delegates; this only provide a new overload for the one that accepts two delegates.
stephentoub added a commit that referenced this pull request May 23, 2015
Add ConcurrentDictionary GetOrAdd/AddOrUpdate overloads with generic arg
@stephentoub stephentoub merged commit cf28b5b into dotnet:future May 23, 2015
@stephentoub stephentoub deleted the new_cd_overloads branch May 23, 2015 18:43
stephentoub added a commit to stephentoub/corefx that referenced this pull request Aug 18, 2015
@karelz karelz modified the milestone: Future Jan 25, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants