Repository: dotnet/roslyn
File: src/Compilers/CSharp/Portable/Syntax/InternalSyntax/CSharpSyntaxNode.cs (C#)

Code before:
        /// </summary>
        /// <returns>
        /// A CSharpSyntaxNode derived from StructuredTriviaSyntax, with the structured view of this trivia node. 
        /// If this trivia node does not have structure, returns null.
        /// </returns>
        /// <remarks>
        /// Some types of trivia have structure that can be accessed as additional syntax nodes.
        /// These forms of trivia include: 
        ///   directives, where the structure describes the structure of the directive.
        ///   documentation comments, where the structure describes the XML structure of the comment.
        ///   skipped tokens, where the structure describes the tokens that were skipped by the parser.
        /// </remarks>
        public override SyntaxNode GetStructure(Microsoft.CodeAnalysis.SyntaxTrivia trivia)
        {
            if (!trivia.HasStructure)
            {
                return null;
            }

            var parent = trivia.Token.Parent;
            if (parent is null)
            {
                return CSharp.Syntax.StructuredTriviaSyntax.Create(trivia);
            }

            var structsInParent = s_structuresTable.GetValue(parent, static _ => new List<(CodeAnalysis.SyntaxTrivia, SyntaxNode)>(capacity: 1));
            lock (structsInParent)
            {
                foreach (var (childTrivia, childStructure) in structsInParent)
                {
                    if (childTrivia.Equals(trivia))
                    {
                        return childStructure;
                    }
                }

                var structure = CSharp.Syntax.StructuredTriviaSyntax.Create(trivia);
                structsInParent.Add((trivia, structure));

                return structure;
            }
        }
    }
}


Review comment (suggestion):
alternatively, why not have this just be a map from parent to SmallDictionary?  SmallDict is alreayd teeny.  It's just an AVL tree.  So with a single key/value, it's just one root node.  

Code after:
        // 99.6% contain 4 or fewer structured trivia
        // 100% contain 7 or fewer structured trivia
        private static readonly ConditionalWeakTable<SyntaxNode, SmallDictionary<CodeAnalysis.SyntaxTrivia, SyntaxNode>> s_structuresTable
            = new ConditionalWeakTable<SyntaxNode, SmallDictionary<CodeAnalysis.SyntaxTrivia, SyntaxNode>>();

        /// <summary>
        /// Gets the syntax node represented the structure of this trivia, if any. The HasStructure property can be used to 
        /// determine if this trivia has structure.
        /// </summary>
        /// <returns>
        /// A CSharpSyntaxNode derived from StructuredTriviaSyntax, with the structured view of this trivia node. 
        /// If this trivia node does not have structure, returns null.
        /// </returns>
        /// <remarks>
        /// Some types of trivia have structure that can be accessed as additional syntax nodes.
        /// These forms of trivia include: 
        ///   directives, where the structure describes the structure of the directive.
        ///   documentation comments, where the structure describes the XML structure of the comment.
        ///   skipped tokens, where the structure describes the tokens that were skipped by the parser.
        /// </remarks>
        public override SyntaxNode GetStructure(Microsoft.CodeAnalysis.SyntaxTrivia trivia)
        {
            if (!trivia.HasStructure)
            {
                return null;
            }

            var parent = trivia.Token.Parent;
            if (parent is null)
            {
                return CSharp.Syntax.StructuredTriviaSyntax.Create(trivia);
            }

            SyntaxNode structure;
            var structsInParent = s_structuresTable.GetOrCreateValue(parent);
            lock (structsInParent)
            {
                if (!structsInParent.TryGetValue(trivia, out structure))
                {
                    structure = CSharp.Syntax.StructuredTriviaSyntax.Create(trivia);
                    structsInParent.Add(trivia, structure);
                }
            }

            return structure;
        }
    }
}