Skip to content

Commit

Permalink
Merge pull request #5 from ElectroJr/moar_pvs_pool
Browse files Browse the repository at this point in the history
Remove _previousIndices and re-use viewer arrays
  • Loading branch information
metalgearsloth authored Nov 20, 2023
2 parents 5c90359 + 92b6bd5 commit 84f7d63
Show file tree
Hide file tree
Showing 17 changed files with 517 additions and 160 deletions.
2 changes: 1 addition & 1 deletion MSBuild/Robust.Engine.Version.props
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project>
<!-- This file automatically reset by Tools/version.py -->
<PropertyGroup><Version>179.0.0</Version></PropertyGroup>
<PropertyGroup><Version>180.0.0</Version></PropertyGroup>
</Project>
Expand Down
19 changes: 19 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ END TEMPLATE-->
*None yet*


## 180.0.0

### Breaking changes

* Removed some obsolete methods from EntityLookupSystem.

### New features

* PhysicsSystem.TryGetNearest now supports chain shapes.
* Add IPhysShape methods to EntityLookupSystem rather than relying on AABB checks.
* Add some more helper methods to SharedTransformSystem.
* Add GetOrNew dictionary extension that also returns a bool on whether the key existed.
* Add a GetAnchoredEntities overload that takes in a list.

### Other

* Use NetEntities for the F3 debug panel to align with command usage.


## 179.0.0

### Breaking changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ protected override void FrameUpdate(FrameEventArgs args)
return;

var mapSystem = _entityManager.System<SharedMapSystem>();
var xformSystem = _entityManager.System<SharedTransformSystem>();

if (_mapManager.TryFindGridAt(mouseWorldMap, out var mouseGridUid, out var mouseGrid))
{
Expand All @@ -80,7 +81,7 @@ protected override void FrameUpdate(FrameEventArgs args)
{
mouseGridPos = new EntityCoordinates(_mapManager.GetMapEntityId(mouseWorldMap.MapId),
mouseWorldMap.Position);
tile = new TileRef(EntityUid.Invalid, mouseGridPos.ToVector2i(_entityManager, _mapManager), Tile.Empty);
tile = new TileRef(EntityUid.Invalid, mouseGridPos.ToVector2i(_entityManager, _mapManager, xformSystem), Tile.Empty);
}

var controlHovered = UserInterfaceManager.CurrentlyHovered;
Expand All @@ -90,35 +91,35 @@ protected override void FrameUpdate(FrameEventArgs args)
Mouse Pos:
Screen: {mouseScreenPos}
{mouseWorldMap}
{mouseGridPos}
{_entityManager.GetNetCoordinates(mouseGridPos)}
{tile}
GUI: {controlHovered}");

_textBuilder.AppendLine("\nAttached Entity:");
var controlledEntity = _playerManager?.LocalPlayer?.ControlledEntity ?? EntityUid.Invalid;
_textBuilder.AppendLine("\nAttached NetEntity:");
var controlledEntity = _playerManager.LocalSession?.AttachedEntity ?? EntityUid.Invalid;

if (controlledEntity == EntityUid.Invalid)
{
_textBuilder.AppendLine("No attached entity.");
_textBuilder.AppendLine("No attached netentity.");
}
else
{
var entityTransform = _entityManager.GetComponent<TransformComponent>(controlledEntity);
var playerWorldOffset = entityTransform.MapPosition;
var playerWorldOffset = xformSystem.GetMapCoordinates(entityTransform);
var playerScreen = _eyeManager.WorldToScreen(playerWorldOffset.Position);

var playerCoordinates = entityTransform.Coordinates;
var playerRotation = entityTransform.WorldRotation;
var playerRotation = xformSystem.GetWorldRotation(entityTransform);
var gridRotation = entityTransform.GridUid != null
? _entityManager.GetComponent<TransformComponent>(entityTransform.GridUid.Value)
.WorldRotation
? xformSystem.GetWorldRotation(entityTransform.GridUid.Value)
: Angle.Zero;

_textBuilder.Append($@" Screen: {playerScreen}
{playerWorldOffset}
{playerCoordinates}
{_entityManager.GetNetCoordinates(playerCoordinates)}
Rotation: {playerRotation.Degrees:F2}°
EntId: {controlledEntity}
GridUid: {entityTransform.GridUid}
NEntId: {_entityManager.GetNetEntity(controlledEntity)}
Grid NEntId: {_entityManager.GetNetEntity(entityTransform.GridUid)}
Grid Rotation: {gridRotation.Degrees:F2}°");
}

Expand Down
10 changes: 7 additions & 3 deletions Robust.Server/BaseServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -660,10 +660,14 @@ public void Cleanup()
{
// Write down exception log
var logPath = _config.GetCVar(CVars.LogPath);
var relPath = PathHelpers.ExecutableRelativeFile(logPath);
Directory.CreateDirectory(relPath);
var pathToWrite = Path.Combine(relPath,
if (!Path.IsPathRooted(logPath))
{
logPath = PathHelpers.ExecutableRelativeFile(logPath);
}

var pathToWrite = Path.Combine(logPath,
"Runtime-" + DateTime.Now.ToString("yyyy-MM-dd-THH-mm-ss") + ".txt");
Directory.CreateDirectory(logPath);
File.WriteAllText(pathToWrite, _runtimeLog.Display(), EncodingHelpers.UTF8);
}

Expand Down
67 changes: 34 additions & 33 deletions Robust.Server/GameStates/PvsSystem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -112,7 +112,6 @@ private readonly ObjectPool<Stack<NetEntity>> _stackPool
RobustTree<NetEntity> tree)?> _previousTrees = new();

private readonly HashSet<(int visMask, IChunkIndexLocation location)> _reusedTrees = new();
private readonly List<(int, IChunkIndexLocation)> _previousIndices = new();

private EntityQuery<EyeComponent> _eyeQuery;
private EntityQuery<MetaDataComponent> _metaQuery;
Expand Down Expand Up @@ -467,8 +466,8 @@ private void OnMapCreated(MapChangedEvent e)
var session = sessions[i];
playerChunks[i] = _playerChunkPool.Get();

var viewers = GetSessionViewers(session);
viewerEntities[i] = viewers;
ref var viewers = ref viewerEntities[i];
GetSessionViewers(session, ref viewers);

for (var j = 0; j < viewers.Length; j++)
{
Expand Down Expand Up @@ -577,34 +576,20 @@ public void RegisterNewPreviousChunkTrees(
_reusedTrees.Add(chunks[i]);
}

_previousIndices.Clear();
_previousIndices.EnsureCapacity(_previousTrees.Count);

foreach (var index in _previousTrees.Keys)
{
_previousIndices.Add(index);
}

for (var i = 0; i < _previousIndices.Count; i++)
foreach (var (index, chunk) in _previousTrees)
{
var index = _previousIndices[i];
// ReSharper disable once InconsistentlySynchronizedField
if (_reusedTrees.Contains(index))
continue;

if (chunk != null)
{
ref var chunk = ref CollectionsMarshal.GetValueRefOrNullRef(_previousTrees, index);
if (chunk != null)
{
_chunkCachePool.Return(chunk.Value.metadata);
_treePool.Return(chunk.Value.tree);
}
_chunkCachePool.Return(chunk.Value.metadata);
_treePool.Return(chunk.Value.tree);
}

if (!chunks.Contains(index))
{
_previousTrees.Remove(index);
}
}

_previousTrees.EnsureCapacity(chunks.Count);
Expand Down Expand Up @@ -1332,28 +1317,44 @@ private EntityState GetFullEntityState(ICommonSession player, EntityUid entityUi
return entState;
}

private EntityUid[] GetSessionViewers(ICommonSession session)
private void GetSessionViewers(ICommonSession session, [NotNull] ref EntityUid[]? viewers)
{
if (session.Status != SessionStatus.InGame)
return Array.Empty<EntityUid>();
{
viewers = Array.Empty<EntityUid>();
return;
}

// Fast path
if (session.ViewSubscriptions.Count == 0)
{
if (session.AttachedEntity == null)
return Array.Empty<EntityUid>();
{
viewers = Array.Empty<EntityUid>();
return;
}

return new[] { session.AttachedEntity.Value };
Array.Resize(ref viewers, 1);
viewers[0] = session.AttachedEntity.Value;
return;
}

var viewers = _uidSetPool.Get();
if (session.AttachedEntity != null)
viewers.Add(session.AttachedEntity.Value);
int i = 0;
if (session.AttachedEntity is { } local)
{
DebugTools.Assert(!session.ViewSubscriptions.Contains(local));
Array.Resize(ref viewers, session.ViewSubscriptions.Count + 1);
viewers[i++] = local;
}
else
{
Array.Resize(ref viewers, session.ViewSubscriptions.Count);
}

viewers.UnionWith(session.ViewSubscriptions);
var viewersArray = viewers.ToArray();
_uidSetPool.Return(viewers);
return viewersArray;
foreach (var ent in session.ViewSubscriptions)
{
viewers[i++] = ent;
}
}

// Read Safe
Expand Down
10 changes: 7 additions & 3 deletions Robust.Shared/Console/Commands/MapCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public override void Execute(IConsoleShell shell, string argStr, string[] args)

internal sealed class ListMapsCommand : LocalizedCommands
{
[Dependency] private readonly IEntityManager _entManager = default!;
[Dependency] private readonly IMapManager _map = default!;

public override string Command => "lsmap";
Expand All @@ -144,10 +145,13 @@ public override void Execute(IConsoleShell shell, string argStr, string[] args)

foreach (var mapId in _map.GetAllMapIds().OrderBy(id => id.Value))
{
msg.AppendFormat("{0}: init: {1}, paused: {2}, ent: {3}, grids: {4}\n",
mapId, _map.IsMapInitialized(mapId),
var mapUid = _map.GetMapEntityId(mapId);

msg.AppendFormat("{0}: {1}, init: {2}, paused: {3}, nent: {4}, grids: {5}\n",
mapId, _entManager.GetComponent<MetaDataComponent>(mapUid).EntityName,
_map.IsMapInitialized(mapId),
_map.IsMapPaused(mapId),
_map.GetMapEntityId(mapId),
_entManager.GetNetEntity(_map.GetMapEntityId(mapId)),
string.Join(",", _map.GetAllGrids(mapId).Select(grid => grid.Owner)));
}

Expand Down
Loading

0 comments on commit 84f7d63

Please sign in to comment.