Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify dictionary values without allocating

I need to modify all of the values in a Dictionary. Typically, modifying a Dictionary while enumerating it throws an exception. There are various ways to work around that, but all of the answers I've seen involve allocating temporary storage. See Editing dictionary values in a foreach loop for an example.

I would like to modify all the values without allocating any memory. Writing a custom struct enumerator the for the values that disregarded the dictionary version would be fine, but since all the important members of the dictionary are private, this seems impossible.

like image 880
Zachary Burns Avatar asked Dec 21 '25 15:12

Zachary Burns


1 Answers

You're definitely getting into some nitty-gritty performance optimization here.

Based on the additional information you've given in the comments, it sounds like the best approach (short of upgrading your memory so you can handle a little more allocation) will probably be to take the Dictionary source code and make a new class specifically for this purpose, which doesn't increment the version field if it's only changing a value.

like image 185
StriplingWarrior Avatar answered Dec 23 '25 04:12

StriplingWarrior