Skip to content

Commit

Permalink
Small Optimizations (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasteles authored Nov 18, 2024
1 parent d6e22ce commit f16e5d5
Show file tree
Hide file tree
Showing 43 changed files with 234 additions and 187 deletions.
4 changes: 2 additions & 2 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"dotnet-reportgenerator-globaltool": {
"version": "5.3.11",
"version": "5.4.1",
"commands": [
"reportgenerator"
],
Expand All @@ -17,7 +17,7 @@
"rollForward": false
},
"docfx": {
"version": "2.77.0",
"version": "2.78.0",
"commands": [
"docfx"
],
Expand Down
1 change: 1 addition & 0 deletions .nuke/build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"enum": [
"BrowseReport",
"Build",
"BuildAll",
"BuildDocs",
"BuildNative",
"BuildSamples",
Expand Down
26 changes: 15 additions & 11 deletions _build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@ class MainBuild : NukeBuild

[Solution] readonly Solution Solution;
[Parameter] readonly string TestResultFile = "test_result.xml";
AbsolutePath CoverageFiles => RootDirectory / "**" / "coverage.cobertura.xml";
AbsolutePath TestReportDirectory => RootDirectory / "TestReport";
AbsolutePath DocsPath => RootDirectory / "docfx";
AbsolutePath DocsSitePath => DocsPath / "_site";

static AbsolutePath CoverageFiles => RootDirectory / "**" / "coverage.cobertura.xml";
static AbsolutePath TestReportDirectory => RootDirectory / "TestReport";
static AbsolutePath DocsPath => RootDirectory / "docfx";
static AbsolutePath DocsSitePath => DocsPath / "_site";

static readonly string[] cleanPaths = ["src", "tests", "samples"];

Target Clean => _ => _
.Description("Clean project directories")
.Executes(() => new[] { "src", "tests" }
.Select(path => RootDirectory / path)
.Executes(() => cleanPaths.Select(path => RootDirectory / path)
.SelectMany(dir => dir
.GlobDirectories("**/bin", "**/obj", "**/TestResults"))
.Append(TestReportDirectory)
.ForEach(x => x.CreateOrCleanDirectory()));
.ForEach(x => x.DeleteDirectory()));

Target Restore => _ => _
.Description("Run dotnet restore in every project")
.DependsOn(Clean)
.Executes(() => DotNetRestore(s => s
.SetProjectFile(Solution)));
.Executes(() => DotNetRestore(s => s.SetProjectFile(Solution)));

Target Build => _ => _
.Description("Builds SDK")
Expand All @@ -49,17 +50,19 @@ class MainBuild : NukeBuild

Target BuildSamples => _ => _
.Description("Builds SDK and Samples")
.DependsOn(Restore)
.Executes(() =>
DotNetBuild(s => s
.SetProjectFile(RootDirectory / "Samples" / "Backdash.Samples.sln")
.SetConfiguration(Configuration)
.EnableNoLogo()
.EnableNoRestore()
.SetProperty("UseSharedCompilation", false)
.SetProcessArgumentConfigurator(args => args.Add("/nodeReuse:false")))
);

Target BuildAll => _ => _
.Description("Build All Projects")
.Triggers(Build, BuildSamples);

Target Lint => _ => _
.Description("Check for codebase formatting and analyzers")
.DependsOn(Build)
Expand Down Expand Up @@ -183,6 +186,7 @@ class MainBuild : NukeBuild
.SetProcessArgumentConfigurator(args => args.Add("--use-current-runtime"))
));


public static int Main() => Execute<MainBuild>();

protected override void OnBuildInitialized() =>
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/Backdash.Benchmarks.Ping/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
using BackgroundJobManager jobs = new(logger);

const int bufferSize = Max.CompressedBytes * Max.NumberOfPlayers;
var sendBuffer1 = Mem.CreatePinnedMemory(bufferSize);
var sendBuffer2 = Mem.CreatePinnedMemory(bufferSize);
var sendBuffer1 = Mem.AllocatePinnedMemory(bufferSize);
var sendBuffer2 = Mem.AllocatePinnedMemory(bufferSize);

using var peer1 = CreateClient(9000, sendBuffer1);
using var peer2 = CreateClient(9001, sendBuffer2);
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/Backdash.Benchmarks/Cases/UdpClientBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class UdpClientBenchmark
[GlobalSetup]
public void Setup()
{
pingerPinnedBuffer = Mem.CreatePinnedMemory(Max.UdpPacketSize);
pongerPinnedBuffer = Mem.CreatePinnedMemory(Max.UdpPacketSize);
pingerPinnedBuffer = Mem.AllocatePinnedMemory(Max.UdpPacketSize);
pongerPinnedBuffer = Mem.AllocatePinnedMemory(Max.UdpPacketSize);
}

[Benchmark]
Expand Down
5 changes: 5 additions & 0 deletions samples/ConsoleGame/GameState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public enum GameInput
Left = 1 << 2,
Right = 1 << 3,
}

[Serializable]
public record struct GameState
{
public Vector2 Position1;
Expand All @@ -19,6 +21,8 @@ public record struct GameState
public int Score2;
public Vector2 Target;
}

[Serializable]
public class NonGameState
{
public required PlayerHandle? LocalPlayer;
Expand All @@ -32,6 +36,7 @@ public class NonGameState
public DateTime LostConnectionTime;
public TimeSpan DisconnectTimeout;
}

public enum PlayerStatus
{
Connecting = 0,
Expand Down
6 changes: 3 additions & 3 deletions samples/ConsoleGame/start_2players.cmd
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
dotnet build -c Release
start dotnet run --no-build -c Release -- 9000 2 local 127.0.0.1:9001
start dotnet run --no-build -c Release -- 9001 2 127.0.0.1:9000 local
dotnet build -c Debug
start dotnet run --no-build -- 9000 2 local 127.0.0.1:9001
start dotnet run --no-build -- 9001 2 127.0.0.1:9000 local
8 changes: 4 additions & 4 deletions samples/ConsoleGame/start_2players_1spec.cmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dotnet build -c Release
start dotnet run --no-build -c Release -- 9000 2 local 127.0.0.1:9001 127.0.0.1:9100
start dotnet run --no-build -c Release -- 9001 2 127.0.0.1:9000 local
start dotnet run --no-build -c Release -- 9100 2 spectate 127.0.0.1:9000
dotnet build -c Debug
start dotnet run --no-build -- 9000 2 local 127.0.0.1:9001 127.0.0.1:9100
start dotnet run --no-build -- 9001 2 127.0.0.1:9000 local
start dotnet run --no-build -- 9100 2 spectate 127.0.0.1:9000
10 changes: 5 additions & 5 deletions samples/ConsoleGame/start_2players_2spec.cmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dotnet build -c Release
start dotnet run --no-build -c Release -- 9000 2 local 127.0.0.1:9001 127.0.0.1:9100
start dotnet run --no-build -c Release -- 9001 2 127.0.0.1:9000 local 127.0.0.1:9101
start dotnet run --no-build -c Release -- 9100 2 spectate 127.0.0.1:9000
start dotnet run --no-build -c Release -- 9101 2 spectate 127.0.0.1:9001
dotnet build -c Debug
start dotnet run --no-build -- 9000 2 local 127.0.0.1:9001 127.0.0.1:9100
start dotnet run --no-build -- 9001 2 127.0.0.1:9000 local 127.0.0.1:9101
start dotnet run --no-build -- 9100 2 spectate 127.0.0.1:9000
start dotnet run --no-build -- 9101 2 spectate 127.0.0.1:9001
3 changes: 1 addition & 2 deletions samples/SpaceWar.Lobby/Services/LobbyUdpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public LobbyUdpClient(int localPort, Uri serverUrl, int serverPort)
public async Task HandShake(User user, CancellationToken ct = default)
{
if (!user.Token.TryFormat(buffer, out var bytesWritten) || bytesWritten is 0) return;
await socket.SendToAsync(buffer.AsMemory()[..bytesWritten], serverEndpoint, ct)
.ConfigureAwait(false);
await socket.SendToAsync(buffer.AsMemory()[..bytesWritten], serverEndpoint, ct);
}

public async Task Ping(User user, Peer[] peers, CancellationToken ct = default)
Expand Down
2 changes: 1 addition & 1 deletion samples/SpaceWar.Lobby/scripts/linux/start_server.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
pushd "$(dirname "$0")/../../../LobbyServer" || exit
dotnet run --no-build -c Release
dotnet run --no-build
popd || exit
2 changes: 1 addition & 1 deletion samples/SpaceWar.Lobby/scripts/windows/start_server.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ dotnet build -c Release %~dp0\..\..\..\..
@pushd %~dp0\..\..\..\LobbyServer

@set LOBBY_SERVER_URL=http://localhost:9999
start dotnet run --no-build -c Release
start dotnet run --no-build
@popd
12 changes: 6 additions & 6 deletions samples/SpaceWar.Shared/.config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@
"isRoot": true,
"tools": {
"dotnet-mgcb": {
"version": "3.8.1.303",
"version": "3.8.2.1105",
"commands": [
"mgcb"
]
},
"dotnet-mgcb-editor": {
"version": "3.8.1.303",
"version": "3.8.2.1105",
"commands": [
"mgcb-editor"
]
},
"dotnet-mgcb-editor-linux": {
"version": "3.8.1.303",
"version": "3.8.2.1105",
"commands": [
"mgcb-editor-linux"
]
},
"dotnet-mgcb-editor-windows": {
"version": "3.8.1.303",
"version": "3.8.2.1105",
"commands": [
"mgcb-editor-windows"
]
},
"dotnet-mgcb-editor-mac": {
"version": "3.8.1.303",
"version": "3.8.2.1105",
"commands": [
"mgcb-editor-mac"
]
}
}
}
}
37 changes: 20 additions & 17 deletions samples/SpaceWar.Shared/SpaceWar.Shared.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>SpaceWar</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303"/>
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Backdash\Backdash.csproj" />
</ItemGroup>
<Target Name="RestoreDotnetTools" BeforeTargets="Restore">
<Message Text="Restoring dotnet tools" Importance="High"/>
<Exec Command="dotnet tool restore"/>
</Target>
<PropertyGroup>
<RootNamespace>SpaceWar</RootNamespace>
<TargetFramework>net8.0</TargetFramework>
<RollForward>Major</RollForward>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishReadyToRun>false</PublishReadyToRun>
<TieredCompilation>false</TieredCompilation>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.2.1105"/>
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.2.1105"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Backdash\Backdash.csproj"/>
</ItemGroup>
<Target Name="RestoreDotnetTools" BeforeTargets="Restore">
<Message Text="Restoring dotnet tools" Importance="High"/>
<Exec Command="dotnet tool restore"/>
</Target>
</Project>
2 changes: 1 addition & 1 deletion samples/SpaceWar/Game1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Game1 : Game
FrameDelay = 2,
Log = new()
{
EnabledLevel = LogLevel.Information,
EnabledLevel = LogLevel.Warning,
},
Protocol = new()
{
Expand Down
Loading

0 comments on commit f16e5d5

Please sign in to comment.