[Bug] Improve performance in Serialize and Deserialize methods in AdalCacheOperations
See original GitHub issueWhich version of MSAL.NET are you using? Latest
Problem
Serialize
and Deserialize
methods in AdalCacheOperations
create a lot of LOH allocations.
Context
AdalCacheOperations
are used to serialize refresh tokens and enable reuse between ADAL and MSAL, which is not needed if only MSAL is used.
Workaround
- Call
WithLegacyCacheCompatibility(false)
to disable those ADAL related calls. It’s a migration feature, which is not needed when using only MSAL.
Possible soliton Set the legacy cache compatibility to false by default in MSAL.
Possible solution
Change Serialize
to
public static Stream Serialize(IDictionary<AdalTokenCacheKey, AdalResultWrapper> tokenCacheDictionary)
{
Stream stream = new SegmentedMemoryStream();
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(SchemaVersion);
writer.Write(tokenCacheDictionary.Count);
StringBuilder reusedBuilder = new StringBuilder();
foreach (KeyValuePair<AdalTokenCacheKey, AdalResultWrapper> kvp in tokenCacheDictionary)
{
writer.Write(string.Format(
CultureInfo.InvariantCulture,
"{1}{0}{2}{0}{3}{0}{4}",
Delimiter,
kvp.Key.Authority,
kvp.Key.Resource,
kvp.Key.ClientId,
(int)kvp.Key.TokenSubjectType));
reusedBuilder.Clear();
writer.Write(kvp.Value.Serialize(reusedBuilder));
}
stream.Position = 0;
return stream;
}
Change Deserialize
to
using (Stream stream = new MemoryStream(state))
{
BinaryReader reader = new BinaryReader(stream);
int blobSchemaVersion = reader.ReadInt32();
int count = reader.ReadInt32();
for (int n = 0; n < count; n++)
{
string keyString = reader.ReadString();
string[] kvpElements = keyString.Split(new[] { Delimiter }, StringSplitOptions.None);
AdalResultWrapper resultEx = AdalResultWrapper.Deserialize(reader.ReadString());
AdalTokenCacheKey key = new AdalTokenCacheKey(
kvpElements[0],
kvpElements[1],
kvpElements[2],
(TokenSubjectType)int.Parse(kvpElements[3], CultureInfo.CurrentCulture),
resultEx.Result.UserInfo);
dictionary.Add(key, resultEx);
}
}
Issue Analytics
- State:
- Created a year ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
How to improve performance for serializing and ...
So look at the expected size in bytes of your serialized graph, divide it by the speed, and you will get a lower...
Read more >BinaryFormatter serialization methods are obsolete and ...
Serialize and Deserialize methods on BinaryFormatter, Formatter, and IFormatter are now obsolete as warning. Additionally, BinaryFormatter ...
Read more >Possible Solutions to Poor Serialization Performance
We obtained some further improvements in speed by 'trimming' unnecessary object references between objects in the session state prior to ...
Read more >Serialize and deserialize JSON using C# - .NET
Deserialization reconstructs an object from the serialized form. The System.Text.Json library design emphasizes high performance and low memory ...
Read more >How to make the fastest .NET Serializer with .NET 7 / C# 11 ...
var bin = MemoryPackSerializer.Serialize(v); var val = MemoryPackSerializer.Deserialize<Person>(bin);. The biggest advantage of Source Generator ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@bgavrilMS Which suggestion Dynamically disable this in code or Improve the Adal Serialize/Deserialize perf
There’s no public API to obsolete, since this ADAL stuff is done internally by default? We could log a warning specifying to disable this or if they want it enabled, then explicitly call
WithLegacyCacheCompatibility(true)
which will remove the log warning too.I would rather deprecate ADAL cache compat completely. We can do it from CCA asap, probably from pca as well because pca can get SSO via WAM. @jmprieur thoughts?