Skip to content

Prefer Dictionary.Remove(key, out value) over Dictionary.this[key], followed by Dictionary.Remove(key) #33800

Open
dotnet/sdk
#57173

Description

@terrajobst

Dictionary.ContainsKey<K, V(key), followed by Dictionary<K, V.this[key], followed by Dictionary<K, V>.Remove(key) can be combined into a single Remove call, using the overload accepting out TValue value.

Category: Performance

Activity

  1. added
    api-suggestion
    code-analyzer
    on Mar 19, 2020
  2. added this to the Future milestone on Mar 20, 2020
  3. added
    code-fixer
    on Mar 21, 2020
  4. jeffhandley commented on Mar 21, 2020

    @jeffhandley
    Member

    Estimates:

    • Analyzer: Medium
    • Fixer: Medium
  5. carlossanlop commented on Jan 8, 2021

    @carlossanlop
    Contributor

    This is closely related to #33797

    Suggested severity: Info

    // Before
            public void RemoveValue(Dictionary<string, int> data, string key)
            {​​
                if (data.ContainsKey(key))
                {
                    // Dictionary.ContainsKey<K, V(key),
                    // followed by Dictionary<K, V.this[key],
                    // followed by Dictionary<K, V>.Remove(key) can be combined into a single Remove call,
                    // using the overload accepting out TValue value.
                    Foo(data[key]);
                    data.Remove(key);
                }
            }​​
    
    // After
            public void RemoveValue(Dictionary<string, int> data, string key)
            {​​
                if (data.Remove(key, out int value))
                {
                    Foo(value);
                }
            }​​

    Applying the below example in the analyzer might be more complicated:

    // Before
            public void RemoveValue(Dictionary<string, int> data, string key)
            {​​
                if (data.ContainsKey(key))
                {
                    int x = data[key];
                    data.Remove(key);
                    Foo(x);
                }
            }​​
    
    // After
            public void RemoveValue(Dictionary<string, int> data, string key)
            {​​
                if (data.Remove(key, out int value))
                {
                    Foo(value);
                }
            }​​

    We should also consider the case where the data is used multiple times, and when there's unrelated code in the way:

    // Before
            public void RemoveValue(Dictionary<string, int> data, string key)
            {​​
                if (data.ContainsKey(key))
                {
                    Foo(data[key]);
                    UnrelatedMethodCall();
                    Bar(data[key]);
                    data.Remove(key);
                }
            }​​
    // After
            public void RemoveValue(Dictionary<string, int> data, string key)
            {​​
                if (data.Remove(key, out int value))
                {
                    Foo(value);
                    UnrelatedMethodCall();
                    Bar(value);
                }
            }​​
  6. added
    needs-further-triage
    on Jan 8, 2021
  7. added
    api-ready-for-review
    and removed
    api-suggestion
    needs-further-triage
    on Feb 23, 2021
  8. buyaa-n commented on Jun 3, 2021

    @buyaa-n
  9. added
    enhancement
    on Jul 20, 2021
  10. bartonjs commented on Jul 27, 2021

    @bartonjs
    Member

    Video

    We came up with a lot of cases where the fixer can cause surprising functional changes, so this really only applies when there's an indexer followed by a call to Remove with no other method calls in between (because any method call could have modified the dictionary). As a specific example, in #33800 (comment) if Foo throws then in the before case the dictionary is not modified, but in the after case it is.

    The case of x = dict[key]; dict.Remove(key); isn't safe to replace, because the exception is lost.

    Conclusion:

    If we find a dictionary call to TryGetValue that is immediately followed by a call to Remove (and the dictionary and the key are both simple variable or parameter expressions) then suggest collapsing them to Remove(key, out T). ("immediate" definitely means that there are no method calls or delegate invocations between those two expressions)

    • Category: Performance
    • Severity: Information
  11. added
    api-approved
    and removed
    api-ready-for-review
    on Jul 27, 2021
  12. added
    help wanted
    on Sep 14, 2021
  13. steveberdy commented on Oct 15, 2021

    @steveberdy
    Contributor

    @buyaa-n may I have this assigned to me? Thanks.

  14. buyaa-n commented on Oct 18, 2021

    @buyaa-n
    Contributor

    Thank you @steveberdy, sorry for the delayed response, it is assigned to you now, JFYI there are several analyzer proposals for dictionary, their implementations preferred to be written in one place (with different diagnostic ID when needed). I see only one of them merged into repo please try to add your implementation to that

  15. added
    api-approved
    and removed
    api-approved
    help wanted
    on Oct 26, 2021
  16. removed their assignment
    on Sep 14, 2022
  17. steveberdy commented on Sep 14, 2022

    @steveberdy
    Contributor

    I'm sorry. I don't really have time to make this analyzer. I've unassigned myself.

  18. added
    help wanted
    on Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-System.Collectionscode-analyzercode-fixerenhancementhelp wanted

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions