© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
15 replies
DoctorGurke

❔ Excessive runtime allocation using Dictionary with struct key TryGetValue

Doing some optimization testing using spatial partitioning of 2D scenes and have hit a weird runtime allocation problem that I've tracked down to Dictionary.TryGetValue.
I am storing Chunk instances in a Dictionary with a custom int2 struct type that implements IEquatable.

Here's my index type:
public struct ChunkCoord : IEquatable<ChunkCoord>
        {
            public int X { get; set; }
            public int Y { get; set; }

            public ChunkCoord(int x, int y)
            {
                X = x;
                Y = y;
            }

            public bool Equals(ChunkCoord coord)
            {
                return X == coord.X && Y == coord.Y;
            }
        }
public struct ChunkCoord : IEquatable<ChunkCoord>
        {
            public int X { get; set; }
            public int Y { get; set; }

            public ChunkCoord(int x, int y)
            {
                X = x;
                Y = y;
            }

            public bool Equals(ChunkCoord coord)
            {
                return X == coord.X && Y == coord.Y;
            }
        }


Any tips to fix or avoid this?
devenv_m8oSert9Tj.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Serializing/Deserializing a Dictionary with a struct key not working
C#CC# / help
15mo ago
✅ Access a runtime Dictionary easier
C#CC# / help
17mo ago
❔ Should dictionary values be value types (struct)
C#CC# / help
4y ago
Finding Index of Dictionary Key
C#CC# / help
2y ago