summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2007-06-23 20:38:28 +0000
committerrubidium <rubidium@openttd.org>2007-06-23 20:38:28 +0000
commitbe35df3f0363ffffe1cfed8d81084c40212e9571 (patch)
tree3b466991f19a45dadbee2beba3fe16a05c77573b /src
parente1958a1d59c541518b80c4613021a17670f84333 (diff)
downloadopenttd-be35df3f0363ffffe1cfed8d81084c40212e9571.tar.xz
(svn r10297) -Codechange: increase the performance of CargoPacket::AllocateRaw using a very simple "caching" system.
Diffstat (limited to 'src')
-rw-r--r--src/cargopacket.cpp8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/cargopacket.cpp b/src/cargopacket.cpp
index 897812b11..0de5b279d 100644
--- a/src/cargopacket.cpp
+++ b/src/cargopacket.cpp
@@ -8,6 +8,9 @@
#include "cargopacket.h"
#include "saveload.h"
+/** Cache for speeding up lookups in AllocateRaw */
+static uint _first_free_cargo_packet_index;
+
/**
* Called if a new block is added to the station-pool
*/
@@ -31,6 +34,7 @@ DEFINE_OLD_POOL(CargoPacket, CargoPacket, CargoPacketPoolNewBlock, CargoPacketPo
void InitializeCargoPackets()
{
+ _first_free_cargo_packet_index = 0;
/* Clean the cargo packet pool and create 1 block in it */
CleanPool(&_CargoPacket_pool);
AddBlockToPool(&_CargoPacket_pool);
@@ -55,6 +59,7 @@ CargoPacket::CargoPacket(StationID source, uint16 count)
CargoPacket::~CargoPacket()
{
+ if (this->index < _first_free_cargo_packet_index) _first_free_cargo_packet_index = this->index;
this->count = 0;
}
@@ -92,12 +97,13 @@ void CargoPacket::operator delete(void *p, CargoPacket::ID cp_idx)
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
* TODO - This is just a temporary stage, this will be removed. */
- for (cp = GetCargoPacket(0); cp != NULL; cp = (cp->index + 1U < GetCargoPacketPoolSize()) ? GetCargoPacket(cp->index + 1U) : NULL) {
+ for (cp = GetCargoPacket(_first_free_cargo_packet_index); cp != NULL; cp = (cp->index + 1U < GetCargoPacketPoolSize()) ? GetCargoPacket(cp->index + 1U) : NULL) {
if (!cp->IsValid()) {
CargoPacket::ID index = cp->index;
memset(cp, 0, sizeof(CargoPacket));
cp->index = index;
+ _first_free_cargo_packet_index = cp->index;
return cp;
}
}