Skip to content

Commit

Permalink
Skip unnecessary lookup in EventRegistrationTokenTable<T> (#1440)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 authored Jan 13, 2024
1 parent 7269faf commit ae69551
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/cswinrt/strings/WinRT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,11 +1066,21 @@ private EventRegistrationToken AddEventHandlerNoLock(T handler)
// Get a registration token, making sure that we haven't already used the value. This should be quite
// rare, but in the case it does happen, just keep trying until we find one that's unused.
EventRegistrationToken token = GetPreferredToken(handler);

#if NET6_0_OR_GREATER
// When on .NET 6+, just iterate on TryAdd, which allows skipping the extra
// lookup on the last iteration (as the handler is added rigth away instead).
while (!m_tokens.TryAdd(token, handler))
{
token = new EventRegistrationToken { Value = token.Value + 1 };
}
#else
while (m_tokens.ContainsKey(token))
{
token = new EventRegistrationToken { Value = token.Value + 1 };
}
m_tokens[token] = handler;
#endif

return token;
}
Expand Down

0 comments on commit ae69551

Please sign in to comment.