1
0
mirror of https://github.com/soarqin/DSP_Mods.git synced 2025-12-09 00:13:36 +08:00

Fix bugs and add description to README

This commit is contained in:
2023-09-01 21:01:07 +08:00
parent 125ff43e09
commit 4402e48d15
3 changed files with 34 additions and 12 deletions

View File

@@ -61,7 +61,7 @@ public class PoolOptPatch : BaseUnityPlugin
ref cargoTraffic.beltPool, ref cargoTraffic.beltCursor, ref cargoTraffic.beltCapacity,
ref cargoTraffic.beltRecycle, ref cargoTraffic.beltRecycleCursor))
Logger.LogDebug($"Optimized `{nameof(cargoTraffic.beltPool)}` on Planet {planet.planetId}");
if (OptimizePool((in CargoPath n) => n.id, 16,
if (OptimizePool((in CargoPath n) => n?.id ?? 0, 16,
ref cargoTraffic.pathPool, ref cargoTraffic.pathCursor, ref cargoTraffic.pathCapacity,
ref cargoTraffic.pathRecycle, ref cargoTraffic.pathRecycleCursor))
Logger.LogDebug($"Optimized `{nameof(cargoTraffic.pathPool)}` on Planet {planet.planetId}");
@@ -85,7 +85,7 @@ public class PoolOptPatch : BaseUnityPlugin
var factoryStorage = planet.factoryStorage;
if (factoryStorage != null)
{
if (OptimizePool((in StorageComponent n) => n.id, 64,
if (OptimizePool((in StorageComponent n) => n?.id ?? n.id, 64,
ref factoryStorage.storagePool, ref factoryStorage.storageCursor, ref factoryStorage.storageCapacity,
ref factoryStorage.storageRecycle, ref factoryStorage.storageRecycleCursor))
Logger.LogDebug($"Optimized `{nameof(factoryStorage.storagePool)}` on Planet {planet.planetId}");
@@ -129,11 +129,11 @@ public class PoolOptPatch : BaseUnityPlugin
var transport = planet.transport;
if (transport != null)
{
if (OptimizePool((in StationComponent n) => n.id, 16,
if (OptimizePool((in StationComponent n) => n?.id ?? 0, 16,
ref transport.stationPool, ref transport.stationCursor, ref transport.stationCapacity,
ref transport.stationRecycle, ref transport.stationRecycleCursor))
Logger.LogDebug($"Optimized `{nameof(transport.stationPool)}` on Planet {planet.planetId}");
if (OptimizePool((in DispenserComponent n) => n.id, 8,
if (OptimizePool((in DispenserComponent n) => n?.id ?? 0, 8,
ref transport.dispenserPool, ref transport.dispenserCursor, ref transport.dispenserCapacity,
ref transport.dispenserRecycle, ref transport.dispenserRecycleCursor))
Logger.LogDebug($"Optimized `{nameof(transport.dispenserPool)}` on Planet {planet.planetId}");
@@ -162,7 +162,7 @@ public class PoolOptPatch : BaseUnityPlugin
ref powerSystem.excPool, ref powerSystem.excCursor, ref powerSystem.excCapacity,
ref powerSystem.excRecycle, ref powerSystem.excRecycleCursor))
Logger.LogDebug($"Optimized `{nameof(powerSystem.excPool)}` on Planet {planet.planetId}");
if (OptimizePool((in PowerNetwork n) => n.id, 8,
if (OptimizePool((in PowerNetwork n) => n?.id ?? 0, 8,
ref powerSystem.netPool, ref powerSystem.netCursor, ref powerSystem.netCapacity,
ref powerSystem.netRecycle, ref powerSystem.netRecycleCursor))
Logger.LogDebug($"Optimized `{nameof(powerSystem.netPool)}` on Planet {planet.planetId}");
@@ -210,15 +210,15 @@ public class PoolOptPatch : BaseUnityPlugin
foreach (var layer in dysonSphere.layersSorted)
{
if (layer == null) continue;
if (OptimizePool((in DysonNode n) => n.id, 64,
if (OptimizePool((in DysonNode n) => n?.id ?? 0, 64,
ref layer.nodePool, ref layer.nodeCursor, ref layer.nodeCapacity,
ref layer.nodeRecycle, ref layer.nodeRecycleCursor))
Logger.LogDebug($"Optimized `{nameof(layer.nodePool)}` on Star {dysonSphere.starData.id} layer {layer.id}");
if (OptimizePool((in DysonFrame n) => n.id, 64,
if (OptimizePool((in DysonFrame n) => n?.id ?? 0, 64,
ref layer.framePool, ref layer.frameCursor, ref layer.frameCapacity,
ref layer.frameRecycle, ref layer.frameRecycleCursor))
Logger.LogDebug($"Optimized `{nameof(layer.framePool)}` on Star {dysonSphere.starData.id} layer {layer.id}");
if (OptimizePool((in DysonShell n) => n.id, 64,
if (OptimizePool((in DysonShell n) => n?.id ?? 0, 64,
ref layer.shellPool, ref layer.shellCursor, ref layer.shellCapacity,
ref layer.shellRecycle, ref layer.shellRecycleCursor))
Logger.LogDebug($"Optimized `{nameof(layer.shellPool)}` on Star {dysonSphere.starData.id} layer {layer.id}");
@@ -244,14 +244,14 @@ public class PoolOptPatch : BaseUnityPlugin
private static bool OptimizePool<T>(GetId<T> getter, int initCapacity, ref T[] pool, ref int cursor, ref int capacity, ref int[] recycle, ref int recycleCursor)
{
if (cursor <= 1) return false;
var pos = cursor;
var pos = cursor - 1;
while (pos > 0)
{
if (getter(pool[pos]) == pos) break;
pos--;
}
if (pos == cursor) return false;
if (pos + 1 == cursor) return false;
if (pos == 0)
{
cursor = 1;
@@ -263,11 +263,12 @@ public class PoolOptPatch : BaseUnityPlugin
return true;
}
Logger.LogDebug($"Old size = {cursor}. Old recycle size = {recycleCursor}");
cursor = pos + 1;
Array.Sort(recycle, 0, recycleCursor);
var idx = Array.BinarySearch(recycle, 0, recycleCursor, pos + 1);
recycleCursor = idx < 0 ? ~idx : idx;
Logger.LogDebug($"Shrinked pool size to {pos} and recycle size to {recycleCursor}");
Logger.LogDebug($"Shrinked pool size to {cursor} and recycle size to {recycleCursor}");
return true;
}
@@ -295,7 +296,7 @@ public class PoolOptPatch : BaseUnityPlugin
return true;
}
cursor = pos + 1;
cursor = pos;
Array.Sort(recycle, 0, recycleCursor);
var idx = Array.BinarySearch(recycle, 0, recycleCursor, pos + 1);
recycleCursor = idx < 0 ? ~idx : idx;

View File

@@ -2,3 +2,19 @@
#### Optimize memory pools on loading gamesaves
#### 加载游戏存档时优化内存池的使用
# Mechanism
* The game uses a lot of memory pools in array to store data, sizes and capacities of these pools are increased on demand but never decreased, even most data inside are not used due to dismantle objects. Unused slot IDs are save in recycle array for reuse.
* Some game functions loop through all allocated slots in the pool, so the size of the pool will affect the performance.
* This mod will optimize the memory pools when loading gamesaves, strip the unused slots from tail and reduce the size of the pool and recycle array, for better performance.
# TODO
* Remove the unused slots from middle of the pool, which leads to ID change for stored objects, needs more investigation.
# 原理
* 游戏使用了大量的内存池数组来存储数据这些内存池的大小和容量是只会随需求膨胀不会收缩的即使由于拆除物体导致内部大多数数据不再使用。而未使用的槽位ID会被保存在回收数组中以供重复使用。
* 一些游戏功能会循环遍历内存池中的所有分配过的槽位,因此内存池的大小会影响性能。
* 本MOD会在加载游戏存档时优化内存池去除尾部未使用的槽位减少内存池数组和回收数组的大小以获得更好的性能。
# TODO
* 去除内存池中间的未使用槽位这会导致物件的存储ID发生变化需要进一步调研。

View File

@@ -49,3 +49,8 @@ Some tweaks for mecha drones and build functions(Successor to FastDrones MOD)
Performance optimizations for Matrix Labs
优化研究站性能
# [PoolOpt](PoolOpt)
Optimize memory pools on loading gamesaves
加载游戏存档时优化内存池的使用