Skip to content

Commit

Permalink
Add Count and Size properties to StringCache (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
wazzamatazz authored Jun 26, 2024
1 parent b926640 commit 7ed06d7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/DataCore.Adapter.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
const DataCore.Adapter.StringCache.UseNativeInternSwitchName = "Switch.DataCore.Adapter.StringCache.UseNativeIntern" -> string!
DataCore.Adapter.StringCache
static DataCore.Adapter.StringCache.Clear() -> void
static DataCore.Adapter.StringCache.Count.get -> int
static DataCore.Adapter.StringCache.Get(string? str) -> string?
static DataCore.Adapter.StringCache.Intern(string? str) -> string?
static DataCore.Adapter.StringCache.NativeInternEnabled.get -> bool
static DataCore.Adapter.StringCache.Size.get -> long
static System.DCAStringExtensions.GetFromStringCache(this string! str) -> string!
static System.DCAStringExtensions.InternToStringCache(this string! str) -> string!
System.DCAStringExtensions
46 changes: 44 additions & 2 deletions src/DataCore.Adapter.Core/StringCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,33 @@ public static class StringCache {


/// <summary>
/// Interns the specified string.
/// The number of strings in the cache.
/// </summary>
/// <remarks>
/// <see cref="Count"/> will always return -1 if native interning is enabled.
/// </remarks>
public static int Count => NativeInternEnabled
? -1
: s_strings.Count;

/// <summary>
/// The total size of all cached strings, in bytes.
/// </summary>
private static long s_size;

/// <summary>
/// The total size of all cached strings, in bytes.
/// </summary>
/// <remarks>
/// <see cref="Size"/> will always return -1 if native interning is enabled.
/// </remarks>
public static long Size => NativeInternEnabled
? -1
: s_size;


/// <summary>
/// Retrieves the interned reference to the specified string.
/// </summary>
/// <param name="str">
/// The string to intern.
Expand All @@ -86,7 +112,22 @@ public static class StringCache {

return NativeInternEnabled
? string.Intern(str)
: s_strings.GetOrAdd(str, str);
: s_strings.GetOrAdd(str, OnAddToCache);
}


/// <summary>
/// Updates the recorded size of the cache to include the specified string.
/// </summary>
/// <param name="str">
/// The string being added to the cache.
/// </param>
/// <returns>
/// The string being added to the cache.
/// </returns>
private static string OnAddToCache(string str) {
s_size += (str.Length * sizeof(char));
return str;
}


Expand Down Expand Up @@ -125,6 +166,7 @@ public static void Clear() {
}

s_strings.Clear();
s_size = 0;
}

}
Expand Down

0 comments on commit 7ed06d7

Please sign in to comment.