GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add string interning option by AArnott · Pull Request #1285 · MessagePack-CSharp/MessagePack-CSharp

Conversation

@AArnott

@AArnott AArnott commented Jul 12, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

This isn't on by default, and while this first trivial implementation allocates every string and passes it through string.Intern(string), many optimizations could be made to it either in this library directly or by apps through overridding the virtual method.

Points to decide on:

  • Should the collection be built into the formatter, leaving MessagePackSerializationOptions unchanged?
  • Should our initial, default implementation be smarter about which strings to intern (e.g. strings not longer than x characters)?
  • Should we avoid the initial string allocation by creating our own Dictionary<ReadOnlyMemory<byte>, string> interned string collection? I'm thinking "yes" but we may want to micro-benchmark to make sure it's really a net win.
  • As written, is this a behavior breaking change because the StandardResolver now includes a string resolver, such that if any app adds a string resolver, it used to work if their resolver worked after the StandardResolver but now must test before it?
  • Perf concerns from always using a formatter for string, even when the option to intern hasn't been turned on?
  • Some very well-developed string interning functionality was developed for MSBuild and exposed for everyone via the Microsoft.NET.StringTools package. Can we take a dependency on that? An alternative would be a source copy, but I would loathe to own something like that.
  • Measure and report on perf impact of string interning on deserialization.

Closes #634

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

pCYSl5EDgo reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@AArnott AArnott added the enhancement label Jul 12, 2021
@AArnott AArnott added this to the v2.3 milestone Jul 12, 2021
@AArnott
AArnott requested a review from neuecc July 12, 2021 16:31
@AArnott AArnott self-assigned this Jul 12, 2021
@AArnott
AArnott force-pushed the fix634 branch from 07f5e3a to 86710ee Compare July 12, 2021 17:01
@AArnott
Add string interning option
2c9a73d
This isn't on by default, and while this first trivial implementation allocates every string and passes it through `string.Intern(string)`, many optimizations could be made to it either in this library directly or by apps through overridding the virtual method.

Closes MessagePack-CSharp#634
@AArnott
AArnott force-pushed the fix634 branch 2 times, most recently from 57a513d to e422d78 Compare July 12, 2021 20:31
neuecc
neuecc reviewed Jul 13, 2021
View reviewed changes

@neuecc neuecc left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing me to Microsoft.NET.StringTools.
I now understand the concept and implementation of WeakInternedString.
Very interesting and inspiring!
Implementing this into the serializer will get a huge positive impact.
(Certainly, I'm curious the additional dictionary lookup/weakreference lookup cost)

In any case, I am very positive that this implementation will be included.

//

It would be preferable if it were not included in the options, but it is going to be difficult to use.
If the default is on, it's fine, but it's not good to turn it off (it's difficult to have a Resolver with a standard string formatter).

Add new reference, if you are going to include it in the standard, I would be concerned about Unity's support for it, but since it is a simple lib, you may as well include it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Resolvers/StandardResolver.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@AArnott
Switch string interning to Microsoft.NET.StringTools
0f1a56b
This gets us a few things:
1. Intern strings without wastefully allocating them first in the common fast paths.
1. Switch to a string collection that retains only weak references.
@AArnott

AArnott commented Jul 13, 2021

Copy link
Copy Markdown
Collaborator Author

(Certainly, I'm curious the additional dictionary lookup/weakreference lookup cost)

I'll measure it and report back.

Add new reference, if you are going to include it in the standard, I would be concerned about Unity's support for it, but since it is a simple lib, you may as well include it.

I'm pushing an update that should fix Unity support. We already have a dependency on System.Memory and one other. This adds exactly one more (which depends on the same two that we already depended on).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AArnott

AArnott commented Jul 13, 2021

Copy link
Copy Markdown
Collaborator Author

It would be preferable if it were not included in the options, but it is going to be difficult to use.
If the default is on, it's fine, but it's not good to turn it off (it's difficult to have a Resolver with a standard string formatter).

Well, I can remove all changes from the Options class and if our default interning behavior isn't what people want, they can override our string resolver. Is that good?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AArnott
AArnott force-pushed the fix634 branch from e422d78 to 3fb02ab Compare July 13, 2021 23:14
@AArnott
String interning on by default
f5edd63
Also revert the Options changes. If the app needs to customize string interning, they can do so by changing the resolver to their own formatter.
@AArnott
AArnott force-pushed the fix634 branch from 3fb02ab to f5edd63 Compare July 14, 2021 13:10
@AArnott
Fix interning of empty strings
a266ea2
This caused a test failure on `net472`.
@AArnott
AArnott marked this pull request as ready for review July 14, 2021 14:05
@neuecc

neuecc commented Jul 15, 2021

Copy link
Copy Markdown
Member

Changes confirmed.
Yes, in this case, I think the person who wants to make the change has a strong will, so the change by Resolver seems to be sufficient.

AArnott and pCYSl5EDgo reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AArnott

AArnott commented Jul 25, 2021

Copy link
Copy Markdown
Collaborator Author

The perf impact is significant:
Using PerfNetFramework, looking at the 10000 iteration of Person[] deserialize test:

Test Before After
MessagePack for C# 162.78 ms 376.28 ms
MessagePack for C# (LZ4) 173.57 ms 385.75 ms
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AArnott

AArnott commented Jul 25, 2021

Copy link
Copy Markdown
Collaborator Author

It looks to me like this may be inescapable perf cost if you want interning. So the question is, should we have it off by default? Should activating it be by applying a resolver that will pick this as a string formatter, or should this be a bool property on MessagePackSerializationOptions?

Name                                                                                                                                                        Inc %   IncExc % Exc
messagepack!StringInterningFormatter.Deserialize                                                                                                            11.2   569  2.9 146
+ microsoft.net.stringtools!WeakStringCacheInterner.InternableToString                                                                                        6.1   311  0.8  42
|+ microsoft.net.stringtools!WeakStringCacheInterner.Intern                                                                                                   5.3   268  0.3  14
||+ microsoft.net.stringtools!WeakStringCache.GetOrCreateEntry                                                                                                5.0   254  0.4  18
|| + microsoft.net.stringtools!Microsoft.NET.StringTools.WeakStringCache+StringWeakHandle.GetString(value class Microsoft.NET.StringTools.InternableString&)  1.5    76  0.2   8
|| + microsoft.net.stringtools!InternableString.GetHashCode                                                                                                   1.4    71  1.4  70
|| + system.collections.concurrent.il!System.Collections.Concurrent.ConcurrentDictionary`2[System.Int32,System.__Canon].TryGetValueInternal(!0,int32,!1&)     0.9    46  0.8  41
|| + coreclr!?                                                                                                                                                0.7    37  0.7  33
|| + system.collections.concurrent.il!System.Collections.Concurrent.ConcurrentDictionary`2[System.Int32,System.__Canon].TryGetValue(!0,!1&)                   0.1     3  0.1   3
|| + system.collections.concurrent.il!System.Collections.Concurrent.ConcurrentDictionary`2[System.Int32,System.__Canon].TryAdd(!0,!1)                         0.0     1  0.0   0
|| + system.collections.concurrent.il!System.Collections.Concurrent.ConcurrentDictionary`2[System.Int32,System.__Canon].get_Count()                           0.0     1  0.0   0
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@neuecc

neuecc commented Jul 26, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Oh, sure, it can be hard to make it the default.
How about adding WithOption to Resolver as well?
WithInternedString, WithImmutableCollections, etc...
(or make this an extension method to MessagePackSerializerOptions)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AArnott

AArnott commented Jul 27, 2021

Copy link
Copy Markdown
Collaborator Author

I think the easiest opt-in would be based on a MessagePackSerializerOptions property. That would have the added benefit of any formatter dealing with strings knowing whether string interning is desirable, although I don't know of any other formatter that would care.
So for an option that really is formatter-specific as this arguably is, putting the option on the formatter (while keeping it immutable, so it would be copy-on-write style) may make more sense -- it just makes it both harder to discover and harder to activate since a resolver will also have to change to point at the new formatter.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AArnott AArnott modified the milestones: v2.3, v2.4 Jul 29, 2021
@cd21h

cd21h commented Jul 30, 2021

Copy link
Copy Markdown
Contributor

I'd love to have this, but with option to opt-in particular property.
For example, having having Address class with State, City and Street, I'd like to intern State and City only.

Maybe we should define an attribute to opt-in for string interning.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AArnott

AArnott commented Aug 22, 2021

Copy link
Copy Markdown
Collaborator Author

@Shatl That's an interesting idea. I'll look into it.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AArnott
AArnott force-pushed the fix634 branch from 78d4ac7 to 30ed3fe Compare August 22, 2021 02:50
@AArnott
Remove string interning by default
6e4fd8d
It is still available via the `StringInterningFormatter`, which can be turned on by default by aggregating a custom resolver,
or by specifying this on a `[Key]` or `[DataMember]` member:

```cs
[MessagePackFormatter(typeof(StringInterningFormatter))]
```
@AArnott
AArnott force-pushed the fix634 branch from 30ed3fe to 6e4fd8d Compare August 22, 2021 02:54
@AArnott
Document string interning
6635df6
This was referenced Jun 11, 2026
Bump MessagePack from 2.2.85 to 2.5.301 xc-link/license-server#1
Open
Bump MessagePack from 1.9.3 to 2.5.301 itn3000/mqnettests#2
Open
Bump MessagePack from 1.7.3.7 to 2.5.301 KikeE36/MasterMemory#1
Open
Bump the nuget group with 2 updates korgano/CustomAmmoCategories#1
Open
Bump the nuget group with 1 update IntelIntent/BinaryPack#1
Open
Bump MessagePack from 2.2.85 to 2.5.301 shahabganji/EasyCompressor#1
Open
Bump MessagePack from 1.9.11 to 2.5.301 shahabganji/SmartEnum#1
Open
Bump MessagePack from 2.3.85 to 2.5.301 gengle/MessagePipe#3
Closed
Bump the nuget group with 1 update pa-0/ahk.ucrio#1
Open
Bump MessagePack from 2.1.90 to 2.5.301 sniten/psi#2
Open
Bump the nuget group with 1 update abri-herdmaster/HerdMASTER.Dotmim.Sync#5
Open
Bump MessagePack from 2.1.90 to 2.5.301 Cysharp/Ulid#113
Open
Bump MessagePack from 2.2.113 to 2.5.301 x0oo11235oo0x/dotnet-algorand-sdk#1
Open
Bump MessagePack and System.Memory nmaier/NMaier.Serializers#1
Open
Bump MessagePack from 2.3.85 to 2.5.301 curiosity-ai/wiktionary#2
Open
Bump MessagePack from 2.1.115 to 2.5.301 generatives/VoidTraveler#1
Open
Bump MessagePack and System.Memory Philippe-Laval/catalyst#1
Open
Bump MessagePack from 2.1.115 to 2.5.301 starchen/Brighter#5
Open
Bump the nuget group with 1 update pawelkasperek/AlterNats#1
Open
Bump MessagePack from 1.9.11 to 2.5.301 ConnectionMaster/dd-trace-dotnet#788
Open
Bump MessagePack from 1.9.3 to 2.5.301 IllusionMods/ZipStudio#5
Merged
This was referenced Jun 26, 2026
Bump MessagePack from 2.3.112 to 2.5.301 nanoframework/nf-Visual-Studio-extension#936
Closed
Bump the nuget group with 3 updates infinispan/Infinispan.Hotrod.Core#56
Open
@dependabot dependabot Bot mentioned this pull request Sep 15, 2026
Bump MessagePack from 1.9.11 to 2.5.301 f2calv/vs-nuget-issue#1
Closed
This was referenced Oct 19, 2026
Bump MessagePack from 2.3.85 to 2.5.303 gengle/MessagePipe#4
Open
Bump MessagePack from 2.2.85 to 2.5.303 umbraco/Umbraco.Cms.Integrations#347
Open
Bump MessagePack from 2.3.112 to 2.5.303 nanoframework/nf-Visual-Studio-extension#956
Closed
Bump MessagePack to 2.5.303 Senparc/Senparc.CO2NET#347
Open
Bump the nuget group with 5 updates maximumstock/exilence-next#4
Open
@dependabot dependabot Bot mentioned this pull request Nov 7, 2026
Bump MessagePack from 1.7.3.4 to 2.5.303 automatica-core/automatica#136
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@neuecc neuecc neuecc left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@AArnott AArnott

Labels

enhancement

Projects

None yet

Milestone

v2.4

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@AArnott @neuecc @cd21h
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.