HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Optimizing this method (HashSet.RemoveWhere performance issue)

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thismethodhashsetissueremovewhereoptimizingperformance

Problem

I have the following method:

public static HashSet GetDeletedPaths(HashSet old, HashSet current)
{
    var deleted = new HashSet(old);
    deleted.RemoveWhere(x => current.Any(y => y.Path == x.Path));
    return deleted;
}


PathDetails is a struct that, among other things, contains Path of type String. This method takes the longest in my application and I was wondering if there were any good ways of improving its performance. These hash sets are used elsewhere in a more hashset-friendly way, so the method must take in two sets and return a set.

Solution

Instead of using the RemoveWhere() method together with the Any() method you should consider to just use ExceptWith() method.

public static HashSet GetDeletedPaths(HashSet old, HashSet current)
{
    var deleted = new HashSet(old);
    deleted.ExceptWith(current);
    return deleted;
}


Your approach calls for each item in deleted the Any() of current whereas ExceptWith() just calls for each item in current the Remove() method of deleted.

Code Snippets

public static HashSet<PathDetails> GetDeletedPaths(HashSet<PathDetails> old, HashSet<PathDetails> current)
{
    var deleted = new HashSet<PathDetails>(old);
    deleted.ExceptWith(current);
    return deleted;
}

Context

StackExchange Code Review Q#80059, answer score: 4

Revisions (0)

No revisions yet.