Reusing Dictionary objects

I have a few CustomEvent calls that are reasonably complicated (having 7 to 10 different parameters on each), so in an attempt to stop memory allocations from occurring, I reuse the dictionaries.

I have one Dictionary per CustomEvent type. Example:

        mMatchCompleteDictionary = new Dictionary<string, object>
        {
            { "matchDefId", 0 },
            { "aeSpent", 0 },
            { "aeEarned", 0 },
            { "nanosEarned", 0 },
            { "nanosEarnedNavita", 0 },
            { "pulsarsPlaced", 0 },
            { "pulsarsSalvaged", 0 },
            { "arraysPlaced", 0 },
            { "arraysSalvaged", 0 },
            { "duration", 0 }
        };

Then I simply reset all of the data in the Dictionary each time I go to submit the CustomEvent, instead of creating a new Dictionary each time. Example:

        mMatchCompleteDictionary["matchDefId"]                = matchDef.defId;
        mMatchCompleteDictionary["aeSpent"]                = matchStats.ReadValue(GameStatType.AE_SPENT);
        mMatchCompleteDictionary["aeEarned"]                = matchStats.ReadValue(GameStatType.AE_EARNED);
        mMatchCompleteDictionary["nanosEarned"]            = matchStats.ReadValue(GameStatType.NANOS_EARNED);
        mMatchCompleteDictionary["nanosEarnedNavita"]        = matchStats.ReadValue(GameStatType.NANOS_EARNED_NAVITA);
        mMatchCompleteDictionary["pulsarsPlaced"]            = matchStats.ReadValue(GameStatType.PULSARS_BUILT);
        mMatchCompleteDictionary["pulsarsSalvaged"]        = matchStats.ReadValue(GameStatType.PULSARS_SALVAGED);
        mMatchCompleteDictionary["arraysPlaced"]            = matchStats.ReadValue(GameStatType.EDC_ARRAYS_BUILT);
        mMatchCompleteDictionary["arraysSalvaged"]            = matchStats.ReadValue(GameStatType.EDC_ARRAYS_SALVAGED);
        mMatchCompleteDictionary["duration"]                = matchStats.ReadValue(GameStatType.MATCH_PLAYTIME);

UnityAnalytics.CustomEvent("matchComplete", mMatchCompleteDictionary);

I just want to make sure it's safe to do that - are the Dictionary objects that get passed to CustomEvent cached internally in UnityAnalytics, or are they immediately written out/copied somewhere?

Thanks!

Eh, I wouldn't do this. Even if they are copying the dictionary or the info in the dictionary (allowing you free access to edit it after the function call) you don't know if a patch will come out in the future that will change this. Leading to a very hard to debug (but it use to work!) bug.

@ZenFri - it is safe to do pooling/reusing of the Dictionary passed to CustomEvent. Since we immediately serialize this object once we receive it.

[quote=“kentunity”, post:3, topic: 579244]
@ZenFri - it is safe to do pooling/reusing of the Dictionary passed to CustomEvent. Since we immediately serialize this object once we receive it.
[/quote]
Awesome, thanks for the reply and confirmation.

We’ve managed to get our game down to allocating very little memory while it’s running, so I’m happy that the analytics will work this way. :slight_smile: