S&box Wiki

Revision Difference

Hotload#546517

<cat>Code.Misc</cat> <title>Hotload Performance</title> Here's some tips in case hotloading code changes to your addon is getting too slow. Our hotloading system needs to go through every object in memory, replacing any from the old DLL with one from the new DLL. This can get slow if you've got huge data structures with millions of objects. Some things to keep in mind: * Arrays of primitive types (`byte`, `int`, etc) survive the hotload without getting processed * Arrays of custom struct types that only contain value types can be copied across very quickly too * Arrays of any other custom types will have each item processed one-by-one, which can be much slower # Find out what's slow You can find out which objects are the slowest to replace by setting these console variables: ``` sv_hotload_log 2 cl_hotload_log 2 ``` These will log a bunch of timing info to the console after each hotload, for both server and client DLLs. The output will look like this: ``` Server Hotload processed 3,272,107 instances in 3.65s ---------------------------------------------------------------------------------------- | MinimalExample.MinimalPlayer.Example | 1000000 | 1321.7ms | | [StaticField] Sandbox.InteropSystem::Address | 1000000 | 1321.7ms | ---------------------------------------------------------------------------------------- | MinimalExample.MinimalPlayer.Example[] | 2048000 | 859.6ms | | [StaticField] Sandbox.InteropSystem::Address | 1024000 | 793.7ms | | [StaticField] MinimalExample.MinimalPlayer::_task | 1024000 | 66.0ms | ---------------------------------------------------------------------------------------- | Sandbox.Prop | 644 | 120.2ms | | [StaticField] Sandbox.InteropSystem::Address | 644 | 120.2ms | ---------------------------------------------------------------------------------------- ... ``` There will be a table for each type encountered, with the second column counting how many instances were seen, and the last column the time taken to process all the instances. The types are listed in descending order of time taken. You can ignore the rows starting with `[StaticField]` for now, they describe how the instances were found. From the example above, we can see processing `MinimalExample.MinimalPlayer.Example` instances is taking the bulk of the hotload time. # Using SkipHotload When you've found what types are slow to process, you can tell the hotloading system to just skip over fields and properties containing instances of those types. When a field or property is skipped, it will be left uninitialized after the hotload. That means `null` for reference types, or `default(T)` for value types. Continuing with the example above, we've worked out a property `BigList` is holding all the instances that hotload is taking ages to process. We can skip that property with the `[SkipHotload]` attribute: ``` [SkipHotload] public List<List<Example>> BigList { get; set; } ``` Now hotloading takes much less time: ``` Server Hotload processed 231,741 instances in 1.12s --------------------------------------------------------------------------------------------- | Sandbox.Prop | 644 | 108.0ms | | [StaticField] Sandbox.InteropSystem::Address | 644 | 108.0ms | --------------------------------------------------------------------------------------------- ... ``` However that property is now `null` after hotload, so skipping is only useful for data structures that you can generate again (and more quickly than it would take hotload to process). For example, your type might contain some raw data in a `byte` array that doesn't need to be skipped, and also some larger uncompressed representation of the same data for live use that you can regenerate after hotload is done. # Hotload callback interfaces For cases where you want to repopulate skipped fields, the containing type can implement `IHotloadBorn` like this: For cases where you want to repopulate skipped fields, the containing type can implement `IHotloadManaged`. This has two methods that will be called by the hotloading system: `IHotloadManaged.Destroyed` is called on the old instance, and `IHotloadManaged.Created` is called on the new one that replaces it. In `Destroyed` you can return a dictionary of values to bring over to the new instance, which get passed to `Created`: ``` void IHotloadBorn.HotloadBorn( object oldObject ) Dictionary<string, object> IHotloadManaged.Destroyed() { return new() { { "Example", BigList.Select(x => x.Count).ToArray() } }; } void IHotloadManaged.Created(Dictionary<string, object> state) { FillBigList(); var example = (int[])state["Example"]; // ... repopulate big list ... } ``` You'll want to only store stuff in the dictionary that is quick for hotload to process, like arrays of primitive types.