Saveload: Simplify list handling in SlRefList

Use reserve on load if suitable
This commit is contained in:
Jonathan G Rennison
2023-12-26 18:18:00 +00:00
parent 60378323ea
commit 2e9cb16254

View File

@@ -1528,8 +1528,7 @@ static void SlRefList(void *list, SLRefType conv)
case SLA_SAVE: { case SLA_SAVE: {
SlWriteUint32((uint32)l->size()); SlWriteUint32((uint32)l->size());
typename PtrList::iterator iter; for (auto iter = l->begin(); iter != l->end(); ++iter) {
for (iter = l->begin(); iter != l->end(); ++iter) {
void *ptr = *iter; void *ptr = *iter;
SlWriteUint32((uint32)ReferenceToInt(ptr, conv)); SlWriteUint32((uint32)ReferenceToInt(ptr, conv));
} }
@@ -1538,6 +1537,9 @@ static void SlRefList(void *list, SLRefType conv)
case SLA_LOAD_CHECK: case SLA_LOAD_CHECK:
case SLA_LOAD: { case SLA_LOAD: {
size_t length = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : SlReadUint32(); size_t length = IsSavegameVersionBefore(SLV_69) ? SlReadUint16() : SlReadUint32();
if constexpr (!std::is_same_v<PtrList, std::list<void *>>) {
l->reserve(length);
}
/* Load each reference and push to the end of the list */ /* Load each reference and push to the end of the list */
for (size_t i = 0; i < length; i++) { for (size_t i = 0; i < length; i++) {
@@ -1547,13 +1549,8 @@ static void SlRefList(void *list, SLRefType conv)
break; break;
} }
case SLA_PTRS: { case SLA_PTRS: {
PtrList temp = *l; for (auto iter = l->begin(); iter != l->end(); ++iter) {
*iter = IntToReference((size_t)*iter, conv);
l->clear();
typename PtrList::iterator iter;
for (iter = temp.begin(); iter != temp.end(); ++iter) {
void *ptr = IntToReference((size_t)*iter, conv);
l->push_back(ptr);
} }
break; break;
} }