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

Commit

Permalink
Jobify serialization (#1454)
Browse files Browse the repository at this point in the history
* Serialize component updates in jobs

* Fix early job schedule due to combined dependencies
Filter on changed components to avoid scheduling jobs for unchanged chunks
Don't reuse queue, dispose and re-create

* Remove UnityComponentSenderGenerator and the ComponentSendSystem
Hack MessagesToSend to store pre-serialized component updates

* Add more profile markers

* Dispose Native collections in right places
Fixes unit tests

* For performance tests by changing profiling marks
Add DisableAutoCreate to ReplicationSystems

* Fix code smells

* Changelog

* Fix netstats for component updates
Rename AddComponentUpdate to AddComponentEvent

* Better profile markers
  • Loading branch information
zeroZshadow authored Aug 25, 2020
1 parent f219927 commit ae4c5fc
Show file tree
Hide file tree
Showing 19 changed files with 209 additions and 292 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

## Unreleased

### Added

- Added `MeansImplicitUse` attribute to `RequireAttribute` to reduce warnings in Rider IDE. [#1462](https://github.com/spatialos/gdk-for-unity/pull/1462)

### Changed

- Upgrade to Worker SDK v14.8.0. [#1458](https://github.com/spatialos/gdk-for-unity/pull/1458)
- Migrated launch configurations to latest game templates. [#1457](https://github.com/spatialos/gdk-for-unity/pull/1457)
- Added `MeansImplicitUse` attribute to `RequireAttribute` to reduce warnings in Rider IDE. [#1462](https://github.com/spatialos/gdk-for-unity/pull/1462)
- Multithreaded component serialization through `SystemBase` jobs. [#1454](https://github.com/spatialos/gdk-for-unity/pull/1454)

## `0.3.10` - 2020-08-18

Expand Down
10 changes: 5 additions & 5 deletions workers/unity/Assets/Playground/Config/EntityTemplates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static EntityTemplate CreateCubeEntityTemplate(Vector3 location)
var template = new EntityTemplate();
template.AddComponent(new Position.Snapshot(location.ToCoordinates()), WorkerUtils.UnityGameLogic);
template.AddComponent(new Metadata.Snapshot("Cube"), WorkerUtils.UnityGameLogic);
template.AddComponent(new Persistence.Snapshot(), WorkerUtils.UnityGameLogic);
template.AddComponent(new Persistence.Snapshot());
template.AddComponent(new CubeColor.Snapshot(), WorkerUtils.UnityGameLogic);
template.AddComponent(new CubeTargetVelocity.Snapshot(new Vector3f(-2.0f, 0, 0)),
WorkerUtils.UnityGameLogic);
Expand Down Expand Up @@ -99,7 +99,7 @@ public static EntityTemplate CreateSpinnerEntityTemplate(Coordinates coords)
template.AddComponent(new Position.Snapshot(coords), WorkerUtils.UnityGameLogic);
template.AddComponent(new Metadata.Snapshot("Spinner"), WorkerUtils.UnityGameLogic);
template.AddComponent(transform, WorkerUtils.UnityGameLogic);
template.AddComponent(new Persistence.Snapshot(), WorkerUtils.UnityGameLogic);
template.AddComponent(new Persistence.Snapshot());
template.AddComponent(new Collisions.Snapshot(), WorkerUtils.UnityGameLogic);
template.AddComponent(new SpinnerColor.Snapshot(Color.BLUE), WorkerUtils.UnityGameLogic);
template.AddComponent(new SpinnerRotation.Snapshot(), WorkerUtils.UnityGameLogic);
Expand All @@ -121,9 +121,9 @@ public static EntityTemplate CreateSpinnerEntityTemplate(Coordinates coords)
public static EntityTemplate CreatePlayerSpawnerEntityTemplate(Coordinates playerSpawnerLocation)
{
var template = new EntityTemplate();
template.AddComponent(new Position.Snapshot(playerSpawnerLocation), WorkerUtils.UnityGameLogic);
template.AddComponent(new Metadata.Snapshot("PlayerCreator"), WorkerUtils.UnityGameLogic);
template.AddComponent(new Persistence.Snapshot(), WorkerUtils.UnityGameLogic);
template.AddComponent(new Position.Snapshot(playerSpawnerLocation));
template.AddComponent(new Metadata.Snapshot("PlayerCreator"));
template.AddComponent(new Persistence.Snapshot());
template.AddComponent(new PlayerCreator.Snapshot(), WorkerUtils.UnityGameLogic);

return template;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ public void SinglePositionUpdate()
{
var markers = new[]
{
"GatherAllChunks",
"ExecuteReplication",
"Position.SendUpdates",
"SpatialOSSendSystem.CompletingAllJobs",
"SpatialOSSendSystem.QueueSerializedMessages",
"WorkerSystem.SendMessages",
"Position.ComponentSerializer.Serialize"
"PositionSerializationJob"
};

var currentState = World.Step(world =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public CoreCodegenJob(CodegenJobOptions options, IFileSystem fileSystem, Details
Logger.Trace("Adding job targets for components.");
AddGenerators(componentsToGenerate,
c => ($"{c.Name}.cs", UnityComponentDataGenerator.Generate),
c => ($"{c.Name}UpdateSender.cs", UnityComponentSenderGenerator.Generate),
c => ($"{c.Name}ReplicationSystem.cs", UnityComponentReplicationSystemGenerator.Generate),
c => ($"{c.Name}EcsViewManager.cs", UnityEcsViewManagerGenerator.Generate),
c => ($"{c.Name}ComponentDiffStorage.cs", ComponentDiffStorageGenerator.Generate),
c => ($"{c.Name}ComponentDiffDeserializer.cs", ComponentDiffDeserializerGenerator.Generate),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,7 @@ public uint GetComponentId()
{
m.ProfileScope("serializeMarker", s =>
{
s.Line(@"
var storage = messages.GetComponentDiffStorage(ComponentId);
var updates = ((IDiffUpdateStorage<Update>) storage).GetUpdates();
for (int i = 0; i < updates.Count; ++i)
{
ref readonly var update = ref updates[i];
var schemaUpdate = SchemaComponentUpdate.Create();
var componentUpdate = new ComponentUpdate(ComponentId, schemaUpdate);
Serialization.SerializeUpdate(update.Update, schemaUpdate);
serializedMessages.AddComponentUpdate(componentUpdate, update.EntityId.Id);
}
");
s.Line("var storage = messages.GetComponentDiffStorage(ComponentId);");

foreach (var ev in eventDetailsList)
{
Expand All @@ -138,7 +125,7 @@ public uint GetComponentId()
var componentUpdate = new ComponentUpdate(ComponentId, schemaUpdate);
var obj = schemaUpdate.GetEvents().AddObject({ev.EventIndex});
{ev.FqnPayloadType}.Serialization.Serialize(ev.Event.Payload, obj);
serializedMessages.AddComponentUpdate(componentUpdate, ev.EntityId.Id);
serializedMessages.AddComponentEvent(componentUpdate, ev.EntityId.Id);
}}
"
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static CodeWriter Generate(UnityComponentDetails componentDetails)
public Type Snapshot {{ get; }} = typeof({rootNamespace}.Snapshot);
public Type Update {{ get; }} = typeof({rootNamespace}.Update);
public Type ReplicationHandler {{ get; }} = typeof({rootNamespace}.ComponentReplicator);
public Type ReplicationSystem {{ get; }} = typeof({rootNamespace}.ReplicationSystem);
public Type Serializer {{ get; }} = typeof({rootNamespace}.ComponentSerializer);
public Type DiffDeserializer {{ get; }} = typeof({rootNamespace}.DiffComponentDeserializer);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using Improbable.Gdk.CodeGeneration.CodeWriter;
using Improbable.Gdk.CodeGeneration.Model.Details;
using NLog;

namespace Improbable.Gdk.CodeGenerator
{
public static class UnityComponentReplicationSystemGenerator
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

public static CodeWriter Generate(UnityComponentDetails componentDetails)
{
var componentNamespace = $"global::{componentDetails.Namespace}.{componentDetails.Name}";

Logger.Trace($"Generating {componentDetails.Namespace}.{componentDetails.Name}.ReplicationSystem internal class.");

return CodeWriter.Populate(cgw =>
{
cgw.UsingDirectives(
"Improbable.Gdk.Core",
"Improbable.Worker.CInterop",
"Unity.Collections",
"Unity.Entities",
"Unity.Profiling"
);

cgw.Namespace(componentDetails.Namespace, ns =>
{
ns.Type($"public partial class {componentDetails.Name}", partial =>
{
partial.Annotate("DisableAutoCreation, UpdateInGroup(typeof(SpatialOSSendGroup)), UpdateBefore(typeof(SpatialOSSendGroup.InternalSpatialOSSendGroup))")
.Type("internal class ReplicationSystem : SystemBase", system =>
{
system.Line($@"
private NativeQueue<SerializedMessagesToSend.UpdateToSend> dirtyComponents;
private SpatialOSSendSystem spatialOsSendSystem;
private ProfilerMarker foreachMarker = new ProfilerMarker(""{componentDetails.Name}SerializationJob"");
protected override void OnCreate()
{{
spatialOsSendSystem = World.GetExistingSystem<SpatialOSSendSystem>();
}}
");
system.Method("protected override void OnUpdate()", m =>
{
m.Line(new[]
{
"dirtyComponents = new NativeQueue<SerializedMessagesToSend.UpdateToSend>(Allocator.TempJob);",
"var dirtyComponentsWriter = dirtyComponents.AsParallelWriter();",
"var marker = foreachMarker;",
});

m.Line($@"
Dependency = Entities.WithName(""{componentDetails.Name}Replication"")");
if (!componentDetails.IsBlittable)
{
m.Line(@"
.WithoutBurst()");
}

m.Line(@"
.WithAll<HasAuthority>()
.WithChangeFilter<Component>()
.ForEach((ref Component component, in SpatialEntityId entity) =>
{
marker.Begin();
if (!component.IsDataDirty())
{
marker.End();
return;
}
// Serialize component
var schemaUpdate = SchemaComponentUpdate.Create();
Serialization.SerializeUpdate(component, schemaUpdate);
component.MarkDataClean();
// Schedule update
var componentUpdate = new ComponentUpdate(ComponentId, schemaUpdate);
var update = new SerializedMessagesToSend.UpdateToSend(componentUpdate, entity.EntityId.Id);
dirtyComponentsWriter.Enqueue(update);
marker.End();
})
.ScheduleParallel(Dependency);
spatialOsSendSystem.AddReplicationJobProducer(Dependency, dirtyComponents);
dirtyComponents = default;
");
});
});
});
});
});
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IComponentMetaclass
Type Snapshot { get; }
Type Update { get; }

Type ReplicationHandler { get; }
Type ReplicationSystem { get; }
Type Serializer { get; }
Type DiffDeserializer { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"references": [
"Unity.Entities",
"Unity.Entities.Hybrid",
"Unity.Mathematics"
"Unity.Mathematics",
"Unity.Collections"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand All @@ -12,5 +13,6 @@
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": []
"versionDefines": [],
"noEngineReferences": false
}
Loading

0 comments on commit ae4c5fc

Please sign in to comment.