Blazor WebAssembly perf: Optimize multiple-attributes overwrite detection #24467
Labels
area-blazor
Includes: Blazor, Razor Components
Done
This issue has been fixed
feature-blazor-wasm
This issue is related to and / or impacts Blazor WebAssembly
Milestone
This is part of #22432
Currently, catch-all parameters (a.k.a. additional attributes) are disproportionately expensive because of the extra time we spend normalizing the list of parameters each time we pass them to child components. We walk through the set of unmatched attributes and build a dictionary so we can detect which ones were overwritten by others. This is particularly expensive because of the cost of hashing each of the attribute name strings.
As a faster alternative, we can use a different data structure (other than
Dictionary<string, int>
) that is more optimized for the case where most lookups will not be a match (i.e., most parameters aren't overwritten by others). In the prototype below,SimpleStringIntDict
has an API likeDictionary<string, int>
, but works without having to callkey.GetHashCode()
. Instead it provides its own extremely basic and fast hash function that doesn't consider all the characters in the string. Since in most cases each lookup results in "no match", there's no drawback (in those cases) in having such a simplistic hash. This would only perform worse thanDictionary<string, int>
in extraordinary cases, e.g., there are a large number of different parameters that all clash on this simplistic hash, and they all get overwritten many times within the same invocation. I don't expect there to be any reasonable use case like that.Prototype implementation is at 0c09859. The default ComplexTable benchmark unfortunately doesn't make heavy use of additional attributes, but if it's changed to pass three additional attributes per
<Cell>
, this optimization improves its timings by 17%. So I regard this optimization as very valuable for cases where people do use additional attributes heavily.The text was updated successfully, but these errors were encountered: