© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
101 replies
ero

❔ Lazily evaluating Dictionary in update loop with good performance

Situation

I have an update loop which receives some data which is Dictionary-like. Due to a version difference, I receive two types of data. The receiving of this data is not a concern and is relatively cheap.
The two types of data I receive are as follows:

- an array of type
(TKey, TValue)[]
(TKey, TValue)[]
,
- two arrays of types
TKey[]
TKey[]
and
TValue[]
TValue[]
.

I cannot change these input types (no, I cannot change the array of tuples to an array of
KeyValuePair<TKey, TValue>
KeyValuePair<TKey, TValue>
s).

I know with certainty that the keys are unique, and I know with certainty that they map to exactly one value.

Goal

I want to use this received data just like a Dictionary (an
IReadOnlyDictionary<TKey, TValue>
IReadOnlyDictionary<TKey, TValue>
to be precise) without actually building the Dictionary the moment I receive it.

By this I mean, I do not want to populate a
Dictionary<TKey, TValue>
Dictionary<TKey, TValue>
with the data, which may be quite large. Doing this would potentially delay the update loop unnecessarily (not actually needing all the data, since only a selection of it will be accessed).

Problem

I can safely have two types of implementations for the two different types of data received.

The issue now is making accesses performant (like the hashing in Dictionaries usually is).

My current idea is the naïve way of making the evaluation linear and caching anything we've already seen. For the ContainsKey implementation, it may be possible to implement a
HashSet<TKey>
HashSet<TKey>
. This, however, would add an extra allocation, would need to do potentially unnecessary computation, and would require some pretty bad logic for the tuple array data.

I'm simply not sure how to implement this very well such that the lazy evaluation is not simply linear with a cache. Any ideas?
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
Next page

Similar Threads

✅ Lazily combine sets
C#CC# / help
3y ago
Update SQL database rows with a dictionary [Answered]
C#CC# / help
4y ago
✅ Evaluating arbitrary XML in MSBuild?
C#CC# / help
2y ago