Skip to content

Commit

Permalink
650-1250x Perf improvement for accessing properties from the Property…
Browse files Browse the repository at this point in the history
…Store

Previously getting a single entry would take around 130-250ms. This change drops it down to 0.2ms

Co-Authored-By: Requi <[email protected]>
  • Loading branch information
JustArion and RequiDev committed Feb 6, 2025
1 parent 7395d5c commit bc2b415
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions NAudio.Wasapi/CoreAudioApi/PropertyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,27 @@ public PropertyStoreProperty this[int index]
/// <returns>True if found</returns>
public bool Contains(PropertyKey key)
{
for (int i = 0; i < Count; i++)
{
PropertyKey ikey = Get(i);
if ((ikey.formatId == key.formatId) && (ikey.propertyId == key.propertyId))
{
return true;
}
}
return false;
var result = storeInterface.GetValue(ref key, out var propVariant);
return result >= 0 && (VarEnum)propVariant.vt != VarEnum.VT_EMPTY;
}

/// <summary>
/// Checks if the property exists
/// </summary>
/// <param name="key">Looks for a specific key</param>
/// <param name="obj">The value of the property wrapped in a type cast</param>
/// <typeparam name="T">The type to cast the property as</typeparam>
/// <returns>True if found</returns>
public bool TryGetValue<T>(PropertyKey key, out T obj)
{
obj = default;
var result = storeInterface.GetValue(ref key, out var propVariant);

if (result < 0 || (VarEnum)propVariant.vt == VarEnum.VT_EMPTY)
return false;

obj = (T)propVariant.Value;
return true;
}

/// <summary>
Expand All @@ -87,16 +99,8 @@ public PropertyStoreProperty this[PropertyKey key]
{
get
{
for (int i = 0; i < Count; i++)
{
PropertyKey ikey = Get(i);
if ((ikey.formatId == key.formatId) && (ikey.propertyId == key.propertyId))
{
Marshal.ThrowExceptionForHR(storeInterface.GetValue(ref ikey, out var result));
return new PropertyStoreProperty(ikey, result);
}
}
return null;
Marshal.ThrowExceptionForHR(storeInterface.GetValue(ref key, out var propVariant));
return new PropertyStoreProperty(key, propVariant);
}
}

Expand Down

0 comments on commit bc2b415

Please sign in to comment.