Station flow stat save/load performance improvement

Avoid use of SlObject in inner loop
This commit is contained in:
Jonathan G Rennison
2018-05-28 02:22:50 +01:00
parent 6187517429
commit e6e93d40e8

View File

@@ -18,6 +18,7 @@
#include "../newgrf_station.h"
#include "saveload.h"
#include "saveload_buffer.h"
#include "table/strings.h"
#include "../safeguards.h"
@@ -265,6 +266,7 @@ struct FlowSaveLoad {
bool restricted;
};
#if 0
static const SaveLoad _flow_desc[] = {
SLE_VAR(FlowSaveLoad, source, SLE_UINT16),
SLE_VAR(FlowSaveLoad, via, SLE_UINT16),
@@ -272,6 +274,7 @@ static const SaveLoad _flow_desc[] = {
SLE_CONDVAR(FlowSaveLoad, restricted, SLE_BOOL, 187, SL_MAX_VERSION),
SLE_END()
};
#endif
/**
* Wrapper function to get the GoodsEntry's internal structure while
@@ -492,6 +495,8 @@ static void RealSave_STNN(BaseStation *bst)
bool waypoint = (bst->facilities & FACIL_WAYPOINT) != 0;
SlObject(bst, waypoint ? _waypoint_desc : _station_desc);
MemoryDumper *dumper = MemoryDumper::GetCurrent();
if (!waypoint) {
Station *st = Station::From(bst);
for (CargoID i = 0; i < NUM_CARGO; i++) {
@@ -512,7 +517,13 @@ static void RealSave_STNN(BaseStation *bst)
flow.restricted = inner_it->first > outer_it->second.GetUnrestricted();
sum_shares = inner_it->first;
assert(flow.share > 0);
SlObject(&flow, _flow_desc);
// SlObject(&flow, _flow_desc); /* this is highly performance-sensitive, manually unroll */
dumper->CheckBytes(2 + 2 + 4 + 1);
dumper->RawWriteUint16(flow.source);
dumper->RawWriteUint16(flow.via);
dumper->RawWriteUint32(flow.share);
dumper->RawWriteByte(flow.restricted != 0);
}
}
for (StationCargoPacketMap::ConstMapIterator it(st->goods[i].cargo.Packets()->begin()); it != st->goods[i].cargo.Packets()->end(); ++it) {
@@ -540,6 +551,8 @@ static void Load_STNN()
{
_num_flows = 0;
ReadBuffer *buffer = ReadBuffer::GetCurrent();
int index;
while ((index = SlIterateArray()) != -1) {
bool waypoint = (SlReadByte() & FACIL_WAYPOINT) != 0;
@@ -564,7 +577,13 @@ static void Load_STNN()
FlowStat *fs = NULL;
StationID prev_source = INVALID_STATION;
for (uint32 j = 0; j < _num_flows; ++j) {
SlObject(&flow, _flow_desc);
// SlObject(&flow, _flow_desc); /* this is highly performance-sensitive, manually unroll */
buffer->CheckBytes(2 + 2 + 4);
flow.source = buffer->RawReadUint16();
flow.via = buffer->RawReadUint16();
flow.share = buffer->RawReadUint32();
if (!IsSavegameVersionBefore(187)) flow.restricted = (buffer->ReadByte() != 0);
if (fs == NULL || prev_source != flow.source) {
fs = &(st->goods[i].flows.insert(std::make_pair(flow.source, FlowStat(flow.via, flow.share, flow.restricted))).first->second);
} else {