Intern strings when using the MessagePackFormatter #907
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This feature is available in MessagePack but not on by default because it has a (small) perf cost when deserializing. But we want it on for RPC by default.
String interning means that we reuse string objects that are equal by value rather than creating new ones. We don't use
string.Intern
here because that keeps them in memory forever, and because that requires allocating a string before switching it for the long-lived instance. The way MessagePack (and this PR) does it requires no allocations at all if the string has been interned before. And the interning collection (which comes from MSBuild) is a weak intern, meaning string will not be held in memory for the life of the process.I also use my
CommonString
type in a few more places. This has the effect of never deserializing even the first string, nor taking the perf hit of an interned string lookup. It memorizes the encoding and decoding between chars and bytes and reuses them each time.