statsig-io / unity-sdk

Statsig SDK for Unity projects
ISC License
1 stars 4 forks source link

Slow Initialization with Big Player Prefs #5

Closed sergio-genies closed 2 months ago

sergio-genies commented 3 months ago

We are currently facing an initialization issue due to the PersistentStore class. We heavily rely on Statsig, which results in a large response. Based on unity documentation, PlayerPrefs.SetString() recommends a value of 2KB or smaller, but Statsig surpasses this limit. Consequently, the updateUserValues method becomes very slow when processing a 90KB JSON file, taking around 3700ms to complete.

On our side, we're doing our due diligence to properly deprecate unused flags. However, the correct usage of PlayerPrefs would limit us to keeping only 10 feature flags, which is not ideal.

Screenshot 2024-07-30 at 9 43 27 p m

sergio-genies commented 3 months ago

Maybe just have PlayerPrefs as an async method

internal void updateUserValues(StatsigUser user, string values)
{
    try
    {
        var stopwatch = new Stopwatch();
        stopwatch.Start();
        var cacheKey = getUserValueKey(user);
        if (cacheKey == currentUserCacheKey)
        {
            ParseAndSaveInitResponse(values);
        }

        storeDataPersistently(cacheKey, values);
        Debug.Log($"[Statsig] Player Prefs completed in {stopwatch.ElapsedMilliseconds} ms.");
    }
    catch (Exception e)
    {
    }
}

internal async Task storeDataPersistently(string cacheKey, string values)
{
    await Task.Run(() =>
    {
        PlayerPrefs.SetString(cacheKey, values);
        PlayerPrefs.Save();
    });
}
sroyal-statsig commented 2 months ago

@sergio-genies We just released sdk version 0.8.0 which adds an option (EnableAsyncCacheWrites) to StatsigOptions which will enable the logic you recommended for the fix.