Warmup system to speed up startup.

This commit is contained in:
Pieter-Jan Briers
2022-12-05 00:44:50 +01:00
parent 22fe99ac99
commit db1e85e69d
6 changed files with 79 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
using System.Threading.Tasks;
using Robust.Shared;
using Robust.Shared.Resources;
using SixLabors.ImageSharp;
namespace Robust.Client;
/// <summary>
/// Logic for "warming up" things like slow static constructors concurrently.
/// </summary>
internal static class ClientWarmup
{
public static void RunWarmup()
{
Task.Run(WarmupCore);
}
private static void WarmupCore()
{
// Get ImageSharp loaded.
_ = Configuration.Default;
SharedWarmup.WarmupCore();
// Preload the JSON loading code.
RsiLoading.Warmup();
}
}
@@ -45,6 +45,8 @@ namespace Robust.Client
private static void ParsedMain(CommandLineArgs args, bool contentStart, IMainArgs? loaderArgs, GameControllerOptions options)
{
ClientWarmup.RunWarmup();
var deps = IoCManager.InitThread();
var mode = args.Headless ? DisplayMode.Headless : DisplayMode.Clyde;
+2
View File
@@ -48,6 +48,8 @@ namespace Robust.Server
private static void ParsedMain(CommandLineArgs args, bool contentStart, ServerOptions options)
{
ServerWarmup.RunWarmup();
Thread.CurrentThread.Name = "Main Thread";
var deps = IoCManager.InitThread();
ServerIoC.RegisterIoC(deps);
+20
View File
@@ -0,0 +1,20 @@
using System.Threading.Tasks;
using Robust.Shared;
namespace Robust.Server;
/// <summary>
/// Logic for "warming up" things like slow static constructors concurrently.
/// </summary>
internal static class ServerWarmup
{
public static void RunWarmup()
{
Task.Run(WarmupCore);
}
private static void WarmupCore()
{
SharedWarmup.WarmupCore();
}
}
+6
View File
@@ -96,6 +96,12 @@ internal static class RsiLoading
return new RsiMetadata(size, states);
}
public static void Warmup()
{
// Just a random RSI I pulled from SS14.
const string warmupJson = @"{""version"":1,""license"":""CC-BY-SA-3.0"",""copyright"":""Space Wizards Federation"",""size"":{""x"":32,""y"":32},""states"":[{""name"":""mono""}]}";
JsonSerializer.Deserialize<RsiJsonMetadata>(warmupJson, SerializerOptions);
}
internal sealed class RsiMetadata
{
+21
View File
@@ -0,0 +1,21 @@
using System.Runtime.CompilerServices;
using Robust.Shared.GameObjects;
using Robust.Shared.Maths;
namespace Robust.Shared;
/// <summary>
/// Logic for "warming up" things like slow static constructors concurrently.
/// </summary>
internal static class SharedWarmup
{
[MethodImpl(MethodImplOptions.NoOptimization)]
public static void WarmupCore()
{
// Color's cctor unironically takes ~12ms.
RuntimeHelpers.RunClassConstructor(typeof(Color).TypeHandle);
// This ends up initializing prometheus-net which takes quite a while.
RuntimeHelpers.RunClassConstructor(typeof(EntitySystemManager).TypeHandle);
}
}