(svn r17812) -Codechange: move the feeder_share cache from CargoList to VehicleCargoList; saves 512 bytes per station and 1-2% on CargoList::MoveTo.

This commit is contained in:
rubidium 2009-10-19 01:12:51 +00:00
parent 40a32bb976
commit 2cca30d2e3
2 changed files with 79 additions and 34 deletions

View File

@ -96,7 +96,6 @@ template <class Tinst>
void CargoList<Tinst>::RemoveFromCache(const CargoPacket *cp) void CargoList<Tinst>::RemoveFromCache(const CargoPacket *cp)
{ {
this->count -= cp->count; this->count -= cp->count;
this->feeder_share -= cp->feeder_share;
this->cargo_days_in_transit -= cp->days_in_transit * cp->count; this->cargo_days_in_transit -= cp->days_in_transit * cp->count;
} }
@ -104,21 +103,9 @@ template <class Tinst>
void CargoList<Tinst>::AddToCache(const CargoPacket *cp) void CargoList<Tinst>::AddToCache(const CargoPacket *cp)
{ {
this->count += cp->count; this->count += cp->count;
this->feeder_share += cp->feeder_share;
this->cargo_days_in_transit += cp->days_in_transit * cp->count; this->cargo_days_in_transit += cp->days_in_transit * cp->count;
} }
void VehicleCargoList::AgeCargo()
{
for (List::const_iterator it = this->packets.begin(); it != this->packets.end(); it++) {
/* If we're at the maximum, then we can't increase no more. */
if ((*it)->days_in_transit == 0xFF) continue;
(*it)->days_in_transit++;
this->cargo_days_in_transit += (*it)->count;
}
}
template <class Tinst> template <class Tinst>
void CargoList<Tinst>::Append(CargoPacket *cp) void CargoList<Tinst>::Append(CargoPacket *cp)
{ {
@ -130,7 +117,7 @@ void CargoList<Tinst>::Append(CargoPacket *cp)
icp->count += cp->count; icp->count += cp->count;
icp->feeder_share += cp->feeder_share; icp->feeder_share += cp->feeder_share;
this->AddToCache(cp); static_cast<Tinst *>(this)->AddToCache(cp);
delete cp; delete cp;
return; return;
} }
@ -138,7 +125,7 @@ void CargoList<Tinst>::Append(CargoPacket *cp)
/* The packet could not be merged with another one */ /* The packet could not be merged with another one */
this->packets.push_back(cp); this->packets.push_back(cp);
this->AddToCache(cp); static_cast<Tinst *>(this)->AddToCache(cp);
} }
@ -150,7 +137,7 @@ void CargoList<Tinst>::Truncate(uint max_remaining)
if (max_remaining == 0) { if (max_remaining == 0) {
/* Nothing should remain, just remove the packets. */ /* Nothing should remain, just remove the packets. */
packets.erase(it++); packets.erase(it++);
this->RemoveFromCache(cp); static_cast<Tinst *>(this)->RemoveFromCache(cp);
delete cp; delete cp;
continue; continue;
} }
@ -189,7 +176,7 @@ bool CargoList<Tinst>::MoveTo(Tother_inst *dest, uint max_move, MoveToAction mta
/* Can move the complete packet */ /* Can move the complete packet */
max_move -= cp->count; max_move -= cp->count;
this->packets.erase(it++); this->packets.erase(it++);
this->RemoveFromCache(cp); static_cast<Tinst *>(this)->RemoveFromCache(cp);
switch(mta) { switch(mta) {
case MTA_FINAL_DELIVERY: case MTA_FINAL_DELIVERY:
payment->PayFinalDelivery(cp, cp->count); payment->PayFinalDelivery(cp, cp->count);
@ -215,20 +202,24 @@ bool CargoList<Tinst>::MoveTo(Tother_inst *dest, uint max_move, MoveToAction mta
if (mta == MTA_FINAL_DELIVERY) { if (mta == MTA_FINAL_DELIVERY) {
/* Final delivery doesn't need package splitting. */ /* Final delivery doesn't need package splitting. */
payment->PayFinalDelivery(cp, max_move); payment->PayFinalDelivery(cp, max_move);
this->count -= max_move;
this->cargo_days_in_transit -= max_move * cp->days_in_transit; /* Remove the delivered data from the cache */
uint left = cp->count - max_move;
cp->count = max_move;
static_cast<Tinst *>(this)->RemoveFromCache(cp);
/* Final delivery payment pays the feeder share, so we have to /* Final delivery payment pays the feeder share, so we have to
* reset that so it is not 'shown' twice for partial unloads. */ * reset that so it is not 'shown' twice for partial unloads. */
this->feeder_share -= cp->feeder_share;
cp->feeder_share = 0; cp->feeder_share = 0;
cp->count = left;
} else { } else {
/* But... the rest needs package splitting. */ /* But... the rest needs package splitting. */
Money fs = cp->feeder_share * max_move / static_cast<uint>(cp->count); Money fs = cp->feeder_share * max_move / static_cast<uint>(cp->count);
cp->feeder_share -= fs; cp->feeder_share -= fs;
cp->count -= max_move;
CargoPacket *cp_new = new CargoPacket(max_move, cp->days_in_transit, cp->source, cp->source_xy, (mta == MTA_CARGO_LOAD) ? data : cp->loaded_at_xy, fs, cp->source_type, cp->source_id); CargoPacket *cp_new = new CargoPacket(max_move, cp->days_in_transit, cp->source, cp->source_xy, (mta == MTA_CARGO_LOAD) ? data : cp->loaded_at_xy, fs, cp->source_type, cp->source_id);
this->RemoveFromCache(cp_new); // this reflects the changes in cp. static_cast<Tinst *>(this)->RemoveFromCache(cp_new); // this reflects the changes in cp.
if (mta == MTA_TRANSFER) { if (mta == MTA_TRANSFER) {
/* Add the feeder share before inserting in dest. */ /* Add the feeder share before inserting in dest. */
@ -237,7 +228,6 @@ bool CargoList<Tinst>::MoveTo(Tother_inst *dest, uint max_move, MoveToAction mta
dest->Append(cp_new); dest->Append(cp_new);
} }
cp->count -= max_move;
max_move = 0; max_move = 0;
} }
@ -249,14 +239,43 @@ template <class Tinst>
void CargoList<Tinst>::InvalidateCache() void CargoList<Tinst>::InvalidateCache()
{ {
this->count = 0; this->count = 0;
this->feeder_share = 0;
this->cargo_days_in_transit = 0; this->cargo_days_in_transit = 0;
for (List::const_iterator it = this->packets.begin(); it != this->packets.end(); it++) { for (List::const_iterator it = this->packets.begin(); it != this->packets.end(); it++) {
this->AddToCache(*it); static_cast<Tinst *>(this)->AddToCache(*it);
} }
} }
void VehicleCargoList::RemoveFromCache(const CargoPacket *cp)
{
this->feeder_share -= cp->feeder_share;
this->Parent::RemoveFromCache(cp);
}
void VehicleCargoList::AddToCache(const CargoPacket *cp)
{
this->feeder_share += cp->feeder_share;
this->Parent::AddToCache(cp);
}
void VehicleCargoList::AgeCargo()
{
for (List::const_iterator it = this->packets.begin(); it != this->packets.end(); it++) {
/* If we're at the maximum, then we can't increase no more. */
if ((*it)->days_in_transit == 0xFF) continue;
(*it)->days_in_transit++;
this->cargo_days_in_transit += (*it)->count;
}
}
void VehicleCargoList::InvalidateCache()
{
this->feeder_share = 0;
this->Parent::InvalidateCache();
}
/* /*
* We have to instantiate everything we want to be usable. * We have to instantiate everything we want to be usable.
*/ */

View File

@ -197,7 +197,6 @@ public:
}; };
protected: protected:
Money feeder_share; ///< Cache for the feeder share
uint count; ///< Cache for the number of cargo entities uint count; ///< Cache for the number of cargo entities
uint cargo_days_in_transit; ///< Cache for the sum of number of days in transit of each entity; comparable to man-hours uint cargo_days_in_transit; ///< Cache for the sum of number of days in transit of each entity; comparable to man-hours
@ -250,15 +249,6 @@ public:
return this->count; return this->count;
} }
/**
* Returns total sum of the feeder share for all packets
* @return the before mentioned number
*/
FORCEINLINE Money FeederShare() const
{
return this->feeder_share;
}
/** /**
* Returns source of the first cargo packet in this list * Returns source of the first cargo packet in this list
* @return the before mentioned source * @return the before mentioned source
@ -326,15 +316,49 @@ public:
* CargoList that is used for vehicles. * CargoList that is used for vehicles.
*/ */
class VehicleCargoList : public CargoList<VehicleCargoList> { class VehicleCargoList : public CargoList<VehicleCargoList> {
protected:
/** The (direct) parent of this class */
typedef CargoList<VehicleCargoList> Parent;
Money feeder_share; ///< Cache for the feeder share
/**
* Update the cache to reflect adding of this packet.
* Increases count, feeder share and days_in_transit
* @param cp a new packet to be inserted
*/
void AddToCache(const CargoPacket *cp);
/**
* Update the cached values to reflect the removal of this packet.
* Decreases count, feeder share and days_in_transit
* @param cp Packet to be removed from cache
*/
void RemoveFromCache(const CargoPacket *cp);
public: public:
/** The super class ought to know what it's doing */
friend class CargoList<VehicleCargoList>;
/** The vehicles have a cargo list (and we want that saved). */ /** The vehicles have a cargo list (and we want that saved). */
friend const struct SaveLoad *GetVehicleDescription(VehicleType vt); friend const struct SaveLoad *GetVehicleDescription(VehicleType vt);
/**
* Returns total sum of the feeder share for all packets
* @return the before mentioned number
*/
FORCEINLINE Money FeederShare() const
{
return this->feeder_share;
}
/** /**
* Ages the all cargo in this list * Ages the all cargo in this list
*/ */
void AgeCargo(); void AgeCargo();
/** Invalidates the cached data and rebuild it */
void InvalidateCache();
/** /**
* Are two the two CargoPackets mergeable in the context of * Are two the two CargoPackets mergeable in the context of
* a list of CargoPackets for a Vehicle? * a list of CargoPackets for a Vehicle?
@ -357,6 +381,8 @@ public:
*/ */
class StationCargoList : public CargoList<StationCargoList> { class StationCargoList : public CargoList<StationCargoList> {
public: public:
/** The super class ought to know what it's doing */
friend class CargoList<StationCargoList>;
/** The stations, via GoodsEntry, have a CargoList. */ /** The stations, via GoodsEntry, have a CargoList. */
friend const struct SaveLoad *GetGoodsDesc(); friend const struct SaveLoad *GetGoodsDesc();