Optimize PlayerPrefs event ID scanning when sending event batches #152
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR aims to fix performance issues when sending batches of events with high auto-increment IDs.
The performance issue stems from many string concatenations (
"Event" + dataIndex.ToString()
) and scanningPlayerPrefs
for every ID between 0 and the next ID. This leads to a massive amount of GC allocations. The attached image is a snapshot of the Unity profiler which shows the issue in an extreme case: many events were sent throughout many app sessions.The solution to this is to reset the auto-increment ID to 0 after sending all batches successfully (I determine this by checking
dataIndex == maxIndex
when deleting data). I'm assuming that this ID is only used for local storage purposes to keep track of unsuccessful batches and nothing else.The alternative fix is to store the starting index so the ID scan doesn't have to start from index 0 every time it sends out a batch of events. The starting index can be determined as the lowest event index that wasn't sent yet.
Either method should mitigate/solve the issue, but we would want to at least keep the resetting the ID to 0 fix since it has less complexity.