question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Improve performance in Serialize and Deserialize methods in AdalCacheOperations

See original GitHub issue

Which 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:closed
  • Created a year ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
pmaytakcommented, Jul 20, 2022

Looks like this is blocking some folks, so implement the suggestion.

@bgavrilMS Which suggestion Dynamically disable this in code or Improve the Adal Serialize/Deserialize perf

Any reason we are not marking it as deprecated now with a warning recommending to turn it off? Could save CPU and memory resources.

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.

1reaction
bgavrilMScommented, May 17, 2022

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found