Skip to content

Commit 5b68add

Browse files
committed
Optimized AsAttributeDictionary() method
There won't be any duplicates if this is isn't a `BaseTopic`, so only check for duplicates if `count` is greater than one. Also, it's faster to remove the `_excludedAttributes` after the fact than it is to check them for every attribute.
1 parent df86a0e commit 5b68add

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

OnTopic/Attributes/AttributeCollection.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,19 @@ public void SetValue(
159159
/// <returns>A new <see cref="AttributeDictionary"/> containing attributes</returns>
160160
public AttributeDictionary AsAttributeDictionary(bool inheritFromBase = false) {
161161
var sourceAttributes = (AttributeCollection?)this;
162-
var attributes = new AttributeDictionary();
163-
while (sourceAttributes is not null) {
162+
var attributes = new AttributeDictionary();
163+
var count = 0;
164+
while (sourceAttributes is not null && ++count < 5) {
164165
foreach (var attribute in sourceAttributes) {
165-
if (!_excludedAttributes.Contains(attribute.Key) && !attributes.ContainsKey(attribute.Key)) {
166-
attributes.Add(attribute.Key, attribute.Value);
166+
if (count is 1 || !attributes.ContainsKey(attribute.Key)) {
167+
attributes.TryAdd(attribute.Key, attribute.Value);
167168
}
168169
}
169170
sourceAttributes = inheritFromBase? sourceAttributes.AssociatedTopic.BaseTopic?.Attributes : null;
170171
}
172+
foreach (var attribute in _excludedAttributes) {
173+
attributes.Remove(attribute);
174+
}
171175
return attributes;
172176
}
173177

0 commit comments

Comments
 (0)