-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Implementation for Dictionary.TryAdd #9923
Conversation
|
|
@jkotas Yeah, that's because of the codeformatter changes that recently went through (somewhat glad to see those conflicts, actually). |
|
I fixed the conflicts for real after verifying that it compiled on my machine. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jamesqo, are you adding tests for this new API in corefx side right?
|
@safern Yes. |
|
When you have the corefx PR up we can go ahead and merge this one. That rule helps us not forget .. |
|
@danmosemsft The issues are not closed until the API is fully exposed in corefx, but sure, I'll submit the corefx tests (which I'm currently working on) alongside this PR. |
|
@jamesqo I shouldn't have said forget -- the problem is if the corefx change never goes in, someone has to go do the work to rollback the coreclr change. You are completely reliable of course :), but that has happened recently with another contributor. So better to know the corefx change is ready to go. |
|
@jkotas do you have any concerns about any subtle bad effect this may accidentally have on codegen? given this is rather critical code. |
|
When in doubts about perf ... measure! Baseline (average of multiple runs): 9438ms So there is about 5% regression for |
|
Should also check |
@jkotas, 37 / 9438 is 0.3%, not 5%. |
Read the numbers Jan shared again? 😄 |
|
@stephentoub Wow, that should have taken me less than 5 minutes of staring at the comment to figure that out. Will fix the implementation and run tests to make sure perf doesn't regress again. |
|
@jkotas I removed the extra branch I had accidentally introduced in Add, before: 12545.4 I also modified it to use the indexer per @danmosemsft's concerns. Indexer, before: 12484.4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enum.IsDefined is too slow, even for a debug check in this method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jkotas OK. I can remove the assert then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
switch? I wouldn't do that. If the C# and JIT compilers decide to generate a jump table based switch (and it looks like they do that unfortunately) both perf and code size will suffer.
Besides, the code ends up being rather convoluted. The None case breaks to return false while the OverwriteExisting case returns true directly. And ThrowOnExisting breaks but of course it doesn't really do that in reality.
Just use if
if (behavior == InsertionBehavior.ThrowOnExisting)
ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException(key);
if (behavior == InsertionBehavior.None)
return false;
Debug.Assert(behavior == InsertBehavior.OverwriteExisting); // if you want
entries[i].value = value;
version++;
return true; There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mikedn Sure, thanks for the advice. I modified your example to check for OverwriteExisting first because that will be the most common branch.
|
@jkotas Does this PR need anything else to go forward? |
|
Could you please resolve conflicts in dotnet/corefx#16642 ? |
|
@jkotas Should get around to it within next day or so. Am busy today. |
* Add Dictionary.TryAdd * Fix the conflicts for real * Remove branch from Add(TKey, TValue) * Tweak naming * Remove a slow assert * PR feedback
Progress towards https://github.com/dotnet/corefx/issues/1942
/cc @stephentoub, @jkotas, @danmosemsft, @ianhays