From 3adaa57a2efac2672373cb99ee084c9ebe5accf5 Mon Sep 17 00:00:00 2001 From: rubidium Date: Mon, 19 Oct 2009 01:12:51 +0000 Subject: [PATCH] (svn r17812) -Codechange: move the feeder_share cache from CargoList to VehicleCargoList; saves 512 bytes per station and 1-2% on CargoList::MoveTo. --- src/cargopacket.cpp | 67 +++++++++++++++++++++++++++++---------------- src/cargopacket.h | 46 ++++++++++++++++++++++++------- 2 files changed, 79 insertions(+), 34 deletions(-) diff --git a/src/cargopacket.cpp b/src/cargopacket.cpp index 6c93fc1c9e..08c504bc9f 100644 --- a/src/cargopacket.cpp +++ b/src/cargopacket.cpp @@ -96,7 +96,6 @@ template void CargoList::RemoveFromCache(const CargoPacket *cp) { this->count -= cp->count; - this->feeder_share -= cp->feeder_share; this->cargo_days_in_transit -= cp->days_in_transit * cp->count; } @@ -104,21 +103,9 @@ template void CargoList::AddToCache(const CargoPacket *cp) { this->count += cp->count; - this->feeder_share += cp->feeder_share; 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 void CargoList::Append(CargoPacket *cp) { @@ -130,7 +117,7 @@ void CargoList::Append(CargoPacket *cp) icp->count += cp->count; icp->feeder_share += cp->feeder_share; - this->AddToCache(cp); + static_cast(this)->AddToCache(cp); delete cp; return; } @@ -138,7 +125,7 @@ void CargoList::Append(CargoPacket *cp) /* The packet could not be merged with another one */ this->packets.push_back(cp); - this->AddToCache(cp); + static_cast(this)->AddToCache(cp); } @@ -150,7 +137,7 @@ void CargoList::Truncate(uint max_remaining) if (max_remaining == 0) { /* Nothing should remain, just remove the packets. */ packets.erase(it++); - this->RemoveFromCache(cp); + static_cast(this)->RemoveFromCache(cp); delete cp; continue; } @@ -189,7 +176,7 @@ bool CargoList::MoveTo(Tother_inst *dest, uint max_move, MoveToAction mta /* Can move the complete packet */ max_move -= cp->count; this->packets.erase(it++); - this->RemoveFromCache(cp); + static_cast(this)->RemoveFromCache(cp); switch(mta) { case MTA_FINAL_DELIVERY: payment->PayFinalDelivery(cp, cp->count); @@ -215,20 +202,24 @@ bool CargoList::MoveTo(Tother_inst *dest, uint max_move, MoveToAction mta if (mta == MTA_FINAL_DELIVERY) { /* Final delivery doesn't need package splitting. */ 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(this)->RemoveFromCache(cp); /* Final delivery payment pays the feeder share, so we have to * reset that so it is not 'shown' twice for partial unloads. */ - this->feeder_share -= cp->feeder_share; cp->feeder_share = 0; + cp->count = left; } else { /* But... the rest needs package splitting. */ Money fs = cp->feeder_share * max_move / static_cast(cp->count); 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); - this->RemoveFromCache(cp_new); // this reflects the changes in cp. + static_cast(this)->RemoveFromCache(cp_new); // this reflects the changes in cp. if (mta == MTA_TRANSFER) { /* Add the feeder share before inserting in dest. */ @@ -237,7 +228,6 @@ bool CargoList::MoveTo(Tother_inst *dest, uint max_move, MoveToAction mta dest->Append(cp_new); } - cp->count -= max_move; max_move = 0; } @@ -249,14 +239,43 @@ template void CargoList::InvalidateCache() { this->count = 0; - this->feeder_share = 0; this->cargo_days_in_transit = 0; for (List::const_iterator it = this->packets.begin(); it != this->packets.end(); it++) { - this->AddToCache(*it); + static_cast(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. */ diff --git a/src/cargopacket.h b/src/cargopacket.h index 2d5c824e99..90081855e0 100644 --- a/src/cargopacket.h +++ b/src/cargopacket.h @@ -197,7 +197,6 @@ public: }; protected: - Money feeder_share; ///< Cache for the feeder share 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 @@ -250,15 +249,6 @@ public: 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 * @return the before mentioned source @@ -326,15 +316,49 @@ public: * CargoList that is used for vehicles. */ class VehicleCargoList : public CargoList { +protected: + /** The (direct) parent of this class */ + typedef CargoList 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: + /** The super class ought to know what it's doing */ + friend class CargoList; /** The vehicles have a cargo list (and we want that saved). */ 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 */ void AgeCargo(); + /** Invalidates the cached data and rebuild it */ + void InvalidateCache(); + /** * Are two the two CargoPackets mergeable in the context of * a list of CargoPackets for a Vehicle? @@ -357,6 +381,8 @@ public: */ class StationCargoList : public CargoList { public: + /** The super class ought to know what it's doing */ + friend class CargoList; /** The stations, via GoodsEntry, have a CargoList. */ friend const struct SaveLoad *GetGoodsDesc();