Replies: 1 comment 1 reply
-
Checking string intern is actually a deceivingly expensive operation. The idea of StringPool is that if you are using it, then any strings which would be interned are already in the pool and so interning just means double-checking every string added/retrieved |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I just "relearned" about string interning, and I also now had a look at the
StringPool
implementation, and noticed that there's not check if a string has an "interned version" before adding it to the pool. Could this be a way of saving some memory, and possibly increase performance for some operations?Note; I'm not suggesting to actually intern strings, just check if they already are, and if so use that reference.
dotnet/src/CommunityToolkit.HighPerformance/Buffers/StringPool.cs
Lines 433 to 447 in 0a2e8ed
e.g.
Of course, the same should be done for the other
Add
/GetOrAdd
methods too.The main reason I'm asking is that I currently have a static constructor in one of my classes that does the following for all strings expected by my parser:
Since the strings added by the constructor are literals, they would have been interned by the compiler(/JIT?), and a
StringPool
would find and use the interned version, even after aReset
, or when a newStringPool
is used.From what I understand about
String.IsInterned
:The first step of
String.Equals
is to do aReferenceEquals
, so there should be some performance gains where equality is compared. I'm not 100% sure, but e.g. switch statements.Beta Was this translation helpful? Give feedback.
All reactions