Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Fix per frame memory allocation in stats manager and social manager (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jplafonta authored and jasonsandlin committed Aug 24, 2017
1 parent 70d1690 commit 9a1d62e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
15 changes: 12 additions & 3 deletions CSharpSource/Source/api/Social/Manager/MockSocialManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class MockSocialManager : ISocialManager
{
private static Random rng = new Random();
private List<SocialEvent> events;
private static readonly List<SocialEvent> emptyEventsList = new List<SocialEvent>();

internal MockSocialManager()
{
Expand Down Expand Up @@ -132,9 +133,17 @@ private void InitUserForOnlinePresence(ref XboxSocialUser groupUser)

public IList<SocialEvent> DoWork()
{
List<SocialEvent> currentEvents = this.events;
this.events = new List<SocialEvent>();
return currentEvents;
List<SocialEvent> returnList = null;
if (this.events.Count > 0)
{
returnList = this.events;
this.events = new List<SocialEvent>();
}
else
{
returnList = emptyEventsList;
}
return returnList;
}

private static XboxSocialUser CreateUser(ulong id = 0)
Expand Down
15 changes: 11 additions & 4 deletions CSharpSource/Source/api/Social/Manager/SocialManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class SocialManager : ISocialManager
private readonly Dictionary<XboxLiveUser, HashSet<WeakReference>> userGroupsMap = new Dictionary<XboxLiveUser, HashSet<WeakReference>>(new XboxUserIdEqualityComparer());

private Queue<SocialEvent> eventQueue = new Queue<SocialEvent>();
private static readonly List<SocialEvent> emptyEventList = new List<SocialEvent>();

private SocialManager()
{
Expand Down Expand Up @@ -180,13 +181,19 @@ public void UpdateUserGroup(XboxLiveUser user, XboxSocialUserGroup group, List<u

public IList<SocialEvent> DoWork()
{
Queue<SocialEvent> eventQueueSnapshot = this.eventQueue;
this.eventQueue = new Queue<SocialEvent>();

List<SocialEvent> events;
lock (this.syncRoot)
{
events = eventQueueSnapshot.ToList();
if (this.eventQueue.Count > 0)
{
events = this.eventQueue.ToList();
this.eventQueue.Clear();
}
else
{
events = emptyEventList;
}

foreach (SocialGraph graph in this.userGraphs.Values)
{
graph.DoWork(events);
Expand Down
11 changes: 10 additions & 1 deletion CSharpSource/Source/api/Stats/Manager/MockStatsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class MockStatsManager : IStatsManager
{
private StatsValueDocument statValueDocument;
private List<StatEvent> statEventList;
private static readonly List<StatEvent> emptyStatEventList = new List<StatEvent>();
private MockLeaderboardService leaderboardService;

internal MockStatsManager()
Expand Down Expand Up @@ -97,7 +98,15 @@ public void RequestFlushToService(XboxLiveUser user, bool isHighPriority = false

public List<StatEvent> DoWork()
{
var copyList = this.statEventList.ToList();
List<StatEvent> copyList = null;
if (this.statEventList.Count > 0)
{
copyList = this.statEventList.ToList();
}
else
{
copyList = emptyStatEventList;
}

this.statValueDocument.DoWork();
this.statEventList.Clear();
Expand Down
12 changes: 11 additions & 1 deletion CSharpSource/Source/api/Stats/Manager/StatsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class StatsManager : IStatsManager

private readonly Dictionary<string, StatsValueDocument> userDocumentMap;
private readonly List<StatEvent> eventList;
private static readonly List<StatEvent> emptyEventList = new List<StatEvent>();
private readonly CallBufferTimer<XboxLiveUser> statTimer;
private readonly CallBufferTimer<XboxLiveUser> statPriorityTimer;

Expand Down Expand Up @@ -241,7 +242,16 @@ public List<StatEvent> DoWork()
{
lock (this.userDocumentMap)
{
var copyList = this.eventList.ToList();
List<StatEvent> copyList = null;
if (this.eventList.Count > 0)
{
copyList = this.eventList.ToList();
}
else
{
copyList = emptyEventList;
}

foreach (var userContextPair in this.userDocumentMap)
{
userContextPair.Value.DoWork();
Expand Down

0 comments on commit 9a1d62e

Please sign in to comment.