Simplify & optimise BurningManAccountingStore.purgeLastTenBlocks

Remove the last 10 blocks one-by-one from the end of the internal linked
list of blocks, instead of rebuilding a truncated list from scratch.
(This all takes place within a write-lock anyway, so it's atomic.)
This commit is contained in:
Steven Barclay 2024-02-24 11:26:16 +08:00
parent 02ee5bc6ea
commit 0214566619
No known key found for this signature in database
GPG Key ID: 9FED6BF1176D500B

View File

@ -74,7 +74,9 @@ public class BurningManAccountingStore implements PersistableEnvelope {
Lock writeLock = readWriteLock.writeLock();
writeLock.lock();
try {
purgeLast10Blocks();
for (int i = 0; i < 10 && !blocks.isEmpty(); i++) {
blocks.removeLast();
}
} finally {
writeLock.unlock();
}
@ -147,17 +149,6 @@ public class BurningManAccountingStore implements PersistableEnvelope {
}
}
private void purgeLast10Blocks() {
if (blocks.size() <= 10) {
blocks.clear();
return;
}
List<AccountingBlock> purged = new ArrayList<>(blocks.subList(0, blocks.size() - 10));
blocks.clear();
blocks.addAll(purged);
}
public Message toProtoMessage() {
List<AccountingBlock> blocksCopy;
Lock readLock = readWriteLock.readLock();