Skip to content

[MLA-1742] backport SideChannel GC reduction #4915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 6, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions com.unity.ml-agents/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Please refer to "Information that is passively collected by Unity" in the

### Bug Fixes
#### com.unity.ml-agents (C#)
- Removed unnecessary memory allocations in `SensorShapeValidator.ValidateSensors()` (#4915)
- Removed unnecessary memory allocations in `SideChannelManager.GetSideChannelMessage()` (#4915)


## [1.0.6] - 2020-11-13
Expand Down
7 changes: 6 additions & 1 deletion com.unity.ml-agents/Runtime/Sensors/SensorShapeValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ public void ValidateSensors(List<ISensor> sensors)
{
// Check for compatibility with the other Agents' Sensors
// TODO make sure this only checks once per agent
Debug.Assert(m_SensorShapes.Count == sensors.Count, $"Number of Sensors must match. {m_SensorShapes.Count} != {sensors.Count}");
Debug.AssertFormat(
m_SensorShapes.Count == sensors.Count,
"Number of Sensors must match. {0} != {1}",
m_SensorShapes.Count,
sensors.Count
);
for (var i = 0; i < Mathf.Min(m_SensorShapes.Count, sensors.Count); i++)
{
var cachedShape = m_SensorShapes[i];
Expand Down
26 changes: 26 additions & 0 deletions com.unity.ml-agents/Runtime/SideChannels/SideChannelsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ internal static byte[] GetSideChannelMessage()
/// <returns></returns>
internal static byte[] GetSideChannelMessage(Dictionary<Guid, SideChannel> sideChannels)
{
if (!HasOutgoingMessages(sideChannels))
{
// Early out so that we don't create the MemoryStream or BinaryWriter.
// This is the most common case.
return Array.Empty<byte>();
}

using (var memStream = new MemoryStream())
{
using (var binaryWriter = new BinaryWriter(memStream))
Expand All @@ -140,6 +147,25 @@ internal static byte[] GetSideChannelMessage(Dictionary<Guid, SideChannel> sideC
}
}

/// <summary>
/// Check whether any of the sidechannels have queued messages.
/// </summary>
/// <param name="sideChannels"></param>
/// <returns></returns>
static bool HasOutgoingMessages(Dictionary<Guid, SideChannel> sideChannels)
{
foreach (var sideChannel in sideChannels.Values)
{
var messageList = sideChannel.MessageQueue;
if (messageList.Count > 0)
{
return true;
}
}

return false;
}

/// <summary>
/// Separates the data received from Python into individual messages for each registered side channel.
/// </summary>
Expand Down