Files
wwdpublic/Content.Server/DeviceLinking/Systems/DeviceLinkSystem.cs
VMSolidus b4524b79da Device Link Sink Optimization (#2465)
This system is only actually touched by the game a tiny handful of times
during a round, yet it was the 4th top frametime consumer, because it
was querrying every potentially networkable entity in existence on the
maybe off-chance it was interacted with by a remote signaller. On a
typical station, this is tens of thousands of entities, with strong
linear scaling as the round time increases due to creation of new
entities.

I have tested this PR to verify that device links and signals still
work, except now with 99.99% less frametime cost.

<details><summary><h1>Media</h1></summary>
<p>

![image](https://github.com/user-attachments/assets/1434fdcb-034a-4d44-8f83-ebe2bf6cecaa)

</p>
</details>

🆑
- tweak: Made significant performance improvements to device network
systems.

Co-authored-by: Eris <eris@erisws.com>
2025-07-12 12:42:02 +10:00

176 lines
6.2 KiB
C#

using Content.Server.DeviceLinking.Components;
using Content.Server.DeviceLinking.Events;
using Content.Server.DeviceNetwork;
using Content.Server.DeviceNetwork.Components;
using Content.Server.DeviceNetwork.Systems;
using Content.Shared.DeviceLinking;
using Content.Shared.DeviceLinking.Events;
using Content.Shared.DeviceNetwork;
namespace Content.Server.DeviceLinking.Systems;
public sealed class DeviceLinkSystem : SharedDeviceLinkSystem
{
[Dependency] private readonly DeviceNetworkSystem _deviceNetworkSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<DeviceLinkSinkComponent, DeviceNetworkPacketEvent>(OnPacketReceived);
SubscribeLocalEvent<DeviceLinkSourceComponent, NewLinkEvent>(OnNewLink);
}
public override void Update(float frameTime)
{
var query = EntityQueryEnumerator<ActiveDeviceLinkSinkComponent>();
while (query.MoveNext(out var uid, out var component))
{
component.InvokeCounter--;
if (component.InvokeCounter > 0)
continue;
RemCompDeferred<ActiveDeviceLinkSinkComponent>(uid);
}
}
#region Sending & Receiving
public override void InvokePort(EntityUid uid, string port, NetworkPayload? data = null, DeviceLinkSourceComponent? sourceComponent = null)
{
if (!Resolve(uid, ref sourceComponent) || !sourceComponent.Outputs.TryGetValue(port, out var sinks))
return;
foreach (var sinkUid in sinks)
{
if (!sourceComponent.LinkedPorts.TryGetValue(sinkUid, out var links))
continue;
if (!TryComp<DeviceLinkSinkComponent>(sinkUid, out var sinkComponent))
continue;
foreach (var (source, sink) in links)
{
if (source == port)
InvokeDirect((uid, sourceComponent), (sinkUid, sinkComponent), sink, data);
}
}
}
/// <summary>
/// Raises an event on or sends a network packet directly to a sink from a source.
/// </summary>
private void InvokeDirect(Entity<DeviceLinkSourceComponent> source, Entity<DeviceLinkSinkComponent?> sink, string sinkPort, NetworkPayload? data)
{
if (!Resolve(sink, ref sink.Comp))
return;
if (sink.Comp.InvokeLimit > 0)
{
EnsureComp<ActiveDeviceLinkSinkComponent>(sink, out var activeLink);
if (activeLink.InvokeCounter > sink.Comp.InvokeLimit)
{
activeLink.InvokeCounter = 0;
var args = new DeviceLinkOverloadedEvent();
RaiseLocalEvent(sink, ref args);
RemoveAllFromSink(sink, sink.Comp);
RemCompDeferred<ActiveDeviceLinkSinkComponent>(sink);
return;
}
activeLink.InvokeCounter++;
Dirty(sink.Owner, activeLink);
}
//Just skip using device networking if the source or the sink doesn't support it
if (!HasComp<DeviceNetworkComponent>(source) || !TryComp<DeviceNetworkComponent>(sink, out var sinkNetwork))
{
var eventArgs = new SignalReceivedEvent(sinkPort, source);
RaiseLocalEvent(sink, ref eventArgs);
return;
}
var payload = new NetworkPayload()
{
[InvokedPort] = sinkPort
};
if (data != null)
{
//Prevent overriding the invoked port
data.Remove(InvokedPort);
foreach (var (key, value) in data)
{
payload.Add(key, value);
}
}
// force using wireless network so things like atmos devices are able to send signals
var network = (int) DeviceNetworkComponent.DeviceNetIdDefaults.Wireless;
_deviceNetworkSystem.QueuePacket(source, sinkNetwork.Address, payload, sinkNetwork.ReceiveFrequency, network);
}
/// <summary>
/// Helper function that invokes a port with a high/low binary logic signal.
/// </summary>
public void SendSignal(EntityUid uid, string port, bool signal, DeviceLinkSourceComponent? comp = null)
{
if (!Resolve(uid, ref comp))
return;
var data = new NetworkPayload
{
[DeviceNetworkConstants.LogicState] = signal ? SignalState.High : SignalState.Low
};
InvokePort(uid, port, data, comp);
comp.LastSignals[port] = signal;
}
/// <summary>
/// Clears the last signals state for linking.
/// This is not to be confused with sending a low signal, this is the complete absence of anything.
/// Use if the device is in an invalid state and has no reasonable output signal.
/// </summary>
public void ClearSignal(Entity<DeviceLinkSourceComponent?> ent, string port)
{
if (!Resolve(ent, ref ent.Comp))
return;
ent.Comp.LastSignals.Remove(port);
}
/// <summary>
/// Checks if the payload has a port defined and if the port is present on the sink.
/// Raises a <see cref="SignalReceivedEvent"/> containing the payload when the check passes
/// </summary>
private void OnPacketReceived(EntityUid uid, DeviceLinkSinkComponent component, DeviceNetworkPacketEvent args)
{
if (!args.Data.TryGetValue(InvokedPort, out string? port) || !(component.Ports?.Contains(port) ?? false))
return;
var eventArgs = new SignalReceivedEvent(port, args.Sender, args.Data);
RaiseLocalEvent(uid, ref eventArgs);
}
/// <summary>
/// When linking from a port that currently has a signal being sent, invoke the new link with that signal.
/// </summary>
private void OnNewLink(Entity<DeviceLinkSourceComponent> ent, ref NewLinkEvent args)
{
if (args.Source != ent.Owner)
return;
// only do anything if a signal is being sent from a port
if (!ent.Comp.LastSignals.TryGetValue(args.SourcePort, out var signal))
return;
var payload = new NetworkPayload()
{
[DeviceNetworkConstants.LogicState] = signal ? SignalState.High : SignalState.Low
};
InvokeDirect(ent, args.Sink, args.SinkPort, payload);
}
#endregion
}