Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Subscription cleanup #1200

Merged
merged 14 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public AuthorityConstraintCallbackManager(uint componentId, World world)
public void InvokeCallbacks()
{
var changes = componentUpdateSystem.GetAuthorityChangesReceived(componentId);
for (int i = 0; i < changes.Count; ++i)
for (var i = 0; i < changes.Count; ++i)
{
if (changes[i].Authority == Authority.Authoritative)
{
Expand All @@ -38,7 +38,7 @@ public void InvokeCallbacks()
public void InvokeLossImminentCallbacks()
{
var changes = componentUpdateSystem.GetAuthorityChangesReceived(componentId);
for (int i = 0; i < changes.Count; ++i)
for (var i = 0; i < changes.Count; ++i)
{
if (changes[i].Authority == Authority.AuthorityLossImminent)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public CommandRequestCallbackManager(World world)
public void InvokeCallbacks()
{
var requests = commandSystem.GetRequests<T>();
for (int i = 0; i < requests.Count; ++i)
for (var i = 0; i < requests.Count; ++i)
{
ref readonly var request = ref requests[i];
callbacks.InvokeAll(request.GetEntityId().Id, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public CommandResponseCallbackManager(World world)
public void InvokeCallbacks()
{
var responses = commandSystem.GetResponses<T>();
for (int i = 0; i < responses.Count; ++i)
for (var i = 0; i < responses.Count; ++i)
{
ref readonly var response = ref responses[i];
callbacks.InvokeAll(response.GetRequestId(), response);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using Improbable.Gdk.Core;
using Improbable.Worker.CInterop;
using Unity.Entities;

namespace Improbable.Gdk.Subscriptions
Expand All @@ -23,9 +22,9 @@ public ComponentAddedCallbackManager(uint componentId, World world)
public void InvokeCallbacks()
{
var entities = componentUpdateSystem.GetComponentsAdded(componentId);
for (int i = 0; i < entities.Count; ++i)
foreach (var entityId in entities)
{
callbacks.InvokeAll(entities[i]);
callbacks.InvokeAll(entityId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,24 @@ public ComponentAuthorityCallbackManager(uint componentId, World world)
public void InvokeCallbacks()
{
var changes = componentUpdateSystem.GetAuthorityChangesReceived(componentId);
for (int i = 0; i < changes.Count; ++i)
for (var i = 0; i < changes.Count; ++i)
{
if (changes[i].Authority == Authority.Authoritative)
switch (changes[i].Authority)
{
callbacks.InvokeAll(changes[i].EntityId.Id, changes[i].Authority);
}
else if (changes[i].Authority == Authority.NotAuthoritative)
{
callbacks.InvokeAllReverse(changes[i].EntityId.Id, changes[i].Authority);
case Authority.Authoritative:
callbacks.InvokeAll(changes[i].EntityId.Id, changes[i].Authority);
break;
case Authority.NotAuthoritative:
callbacks.InvokeAllReverse(changes[i].EntityId.Id, changes[i].Authority);
break;
}
}
}

public void InvokeLossImminentCallbacks()
{
var changes = componentUpdateSystem.GetAuthorityChangesReceived(componentId);
for (int i = 0; i < changes.Count; ++i)
for (var i = 0; i < changes.Count; ++i)
{
if (changes[i].Authority == Authority.AuthorityLossImminent)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ComponentEventCallbackManager(World world)
public void InvokeCallbacks()
{
var updates = componentUpdateSystem.GetEventsReceived<T>();
for (int i = 0; i < updates.Count; ++i)
for (var i = 0; i < updates.Count; ++i)
{
ref readonly var update = ref updates[i];
callbacks.InvokeAll(update.EntityId.Id, update.Event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public void InvokeCallbacks()
{
// todo like entity stuff this should also be temporarily removed components
var entities = componentUpdateSystem.GetComponentsRemoved(componentId);
for (int i = 0; i < entities.Count; ++i)
foreach (var entityId in entities)
{
callbacks.InvokeAllReverse(entities[i]);
callbacks.InvokeAllReverse(entityId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ComponentUpdateCallbackManager(World world)
public void InvokeCallbacks()
{
var updates = componentUpdateSystem.GetComponentUpdatesReceived<T>();
for (int i = 0; i < updates.Count; ++i)
for (var i = 0; i < updates.Count; ++i)
{
ref readonly var update = ref updates[i];
callbacks.InvokeAll(update.EntityId.Id, update.Update);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public EntityAddedCallbackManager(World world)
public void InvokeCallbacks()
{
var entities = entitySystem.GetEntitiesAdded();
for (int i = 0; i < entities.Count; ++i)
foreach (var entityId in entities)
{
callbacks.InvokeAll(entities[i]);
callbacks.InvokeAll(entityId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public EntityRemovedCallbackManager(World world)
public void InvokeCallbacks()
{
var entities = entitySystem.GetEntitiesRemoved();
for (int i = 0; i < entities.Count; ++i)
foreach (var entityId in entities)
{
callbacks.InvokeAllReverse(entities[i]);
callbacks.InvokeAllReverse(entityId);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;

namespace Improbable.Gdk.Subscriptions
Expand All @@ -22,23 +23,25 @@ public void AddCallbackManager(TIndex index, TManager manager)
indexesToAdd.Add(index);
}

public List<TManager> GetManagers()
private void UpdateManagers()
{
foreach (var indexToAdd in indexesToAdd)
if (indexesToAdd.Count > 0)
zeroZshadow marked this conversation as resolved.
Show resolved Hide resolved
{
callbackManagers.Add(indexToCallbackManager[indexToAdd]);
}

indexesToAdd.Clear();
foreach (var indexToAdd in indexesToAdd)
{
callbackManagers.Add(indexToCallbackManager[indexToAdd]);
}

return callbackManagers;
indexesToAdd.Clear();
}
}

public void InvokeCallbacks()
public void InvokeEach(Action<TManager> callback)
zeroZshadow marked this conversation as resolved.
Show resolved Hide resolved
{
foreach (var manager in GetManagers())
UpdateManagers();
foreach (var manager in callbackManagers)
{
manager.InvokeCallbacks();
callback(manager);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public bool UnregisterCallback(ulong callbackKey)
public void InvokeCallbacks()
{
var workerFlagChanges = workerSystem.Diff.GetWorkerFlagChanges();
for (int i = 0; i < workerFlagChanges.Count; ++i)
for (var i = 0; i < workerFlagChanges.Count; ++i)
{
var pair = workerFlagChanges[i];
callbacks.InvokeAll(pair);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private void FlushDeferredOperations()
}
}

private struct WrappedCallback : IEquatable<WrappedCallback>
private readonly struct WrappedCallback : IEquatable<WrappedCallback>
{
public readonly ulong Key;
private readonly Action<T> action;
Expand Down Expand Up @@ -156,103 +156,4 @@ public override int GetHashCode()
}
}
}

// Slight adjustment to the component callbacks class
// todo check if this can have in params for ops (i.e. can it be used with a non readonly struct)
public class IndexedCallbacks<T>
{
private readonly Dictionary<long, Callbacks<T>> callbacks = new Dictionary<long, Callbacks<T>>();
private readonly Dictionary<ulong, long> callbackKeyToIndex = new Dictionary<ulong, long>();

public void Add(long index, ulong callbackKey, Action<T> value)
{
if (!callbacks.TryGetValue(index, out var callbacksForIndex))
{
callbacksForIndex = new Callbacks<T>();
callbacks.Add(index, callbacksForIndex);
}

callbackKeyToIndex.Add(callbackKey, index);
callbacks[index].Add(callbackKey, value);
}

public bool Remove(ulong callbackKey)
{
if (!callbackKeyToIndex.TryGetValue(callbackKey, out var index))
{
return false;
}

callbacks[index].Remove(callbackKey);
callbackKeyToIndex.Remove(callbackKey);
return true;
}

public void InvokeAll(long index, T op)
{
if (callbacks.ContainsKey(index))
{
callbacks[index].InvokeAll(op);
}
}

public void InvokeAllReverse(long index, T op)
{
if (callbacks.ContainsKey(index))
{
callbacks[index].InvokeAllReverse(op);
}
}
}

// Efficient ability to remove all callbacks for an index, but slow to remove a single callback
public class SingleUseIndexCallbacks<T>
{
private readonly Dictionary<long, Callbacks<T>> callbacks = new Dictionary<long, Callbacks<T>>();

public void Add(long index, ulong callbackKey, Action<T> value)
{
if (!callbacks.TryGetValue(index, out var callbacksForIndex))
{
callbacksForIndex = new Callbacks<T>();
callbacks.Add(index, callbacksForIndex);
}

callbacksForIndex.Add(callbackKey, value);
}

public void RemoveAllCallbacksForIndex(long index)
{
callbacks.Remove(index);
}

public bool Remove(ulong callbackKey)
{
foreach (var callback in callbacks)
{
if (callback.Value.Remove(callbackKey))
{
return true;
}
}

return false;
}

public void InvokeAll(long index, T op)
{
if (callbacks.TryGetValue(index, out var callback))
{
callback.InvokeAll(op);
}
}

public void InvokeAllReverse(long index, T op)
{
if (callbacks.TryGetValue(index, out var callback))
{
callback.InvokeAllReverse(op);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;

namespace Improbable.Gdk.Subscriptions
{
// Slight adjustment to the component callbacks class
// todo check if this can have in params for ops (i.e. can it be used with a non readonly struct)
zeroZshadow marked this conversation as resolved.
Show resolved Hide resolved
public class IndexedCallbacks<T>
{
private readonly Dictionary<long, Callbacks<T>> indexedCallbacks = new Dictionary<long, Callbacks<T>>();
private readonly Dictionary<ulong, long> callbackKeyToIndex = new Dictionary<ulong, long>();

public void Add(long index, ulong callbackKey, Action<T> value)
{
if (!indexedCallbacks.TryGetValue(index, out var callbacks))
{
callbacks = new Callbacks<T>();
indexedCallbacks.Add(index, callbacks);
}

callbackKeyToIndex.Add(callbackKey, index);
callbacks.Add(callbackKey, value);
}

public bool Remove(ulong callbackKey)
{
if (!callbackKeyToIndex.TryGetValue(callbackKey, out var index))
{
return false;
}

indexedCallbacks[index].Remove(callbackKey);
callbackKeyToIndex.Remove(callbackKey);
return true;
}

public void InvokeAll(long index, T argument)
{
if (indexedCallbacks.TryGetValue(index, out var callbacks))
{
callbacks.InvokeAll(argument);
}
}

public void InvokeAllReverse(long index, T argument)
{
if (indexedCallbacks.TryGetValue(index, out var callbacks))
{
callbacks.InvokeAllReverse(argument);
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading