From 830526a7937217e4a47ec694133ce1cf17d57ad5 Mon Sep 17 00:00:00 2001 From: ThorstenB Date: Sat, 24 Nov 2012 12:42:31 +0100 Subject: [PATCH] Traffic performance: add airport memory cache to avoid repeating identical NavCache/SQL queries. Also ensures we're not using multiple FGAirport instances for an airport - each triggering identical queries to pull in airport data. Airports by ICAO ID are requested very frequently at run-time, so caching significantly boosts performance. --- src/Airports/simple.cxx | 27 ++++++++++++++++----------- src/Airports/simple.hxx | 5 +++++ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/Airports/simple.cxx b/src/Airports/simple.cxx index 9fff74dc9..ff12662df 100644 --- a/src/Airports/simple.cxx +++ b/src/Airports/simple.cxx @@ -62,6 +62,8 @@ using namespace flightgear; * FGAirport ***************************************************************************/ +AirportCache FGAirport::airportCache; + FGAirport::FGAirport(PositionedID aGuid, const string &id, const SGGeod& location, const string &name, bool has_metar, Type aType) : FGPositioned(aGuid, aType, id, location), @@ -305,23 +307,26 @@ bool FGAirport::HardSurfaceFilter::passAirport(FGAirport* aApt) const FGAirport* FGAirport::findByIdent(const std::string& aIdent) { + AirportCache::iterator it = airportCache.find(aIdent); + if (it != airportCache.end()) + return it->second; + PortsFilter filter; - FGPositionedRef r = FGPositioned::findFirstWithIdent(aIdent, &filter); - if (!r) { - return NULL; // we don't warn here, let the caller do that - } - return static_cast(r.ptr()); + FGAirport* r = static_cast (FGPositioned::findFirstWithIdent(aIdent, &filter).get()); + + // add airport to the cache (even when it's NULL, so we don't need to search in vain again) + airportCache[aIdent] = r; + + // we don't warn here when r==NULL, let the caller do that + return r; } FGAirport* FGAirport::getByIdent(const std::string& aIdent) { - FGPositionedRef r; - PortsFilter filter; - r = FGPositioned::findFirstWithIdent(aIdent, &filter); - if (!r) { + FGAirport* r = findByIdent(aIdent); + if (!r) throw sg_range_exception("No such airport with ident: " + aIdent); - } - return static_cast(r.ptr()); + return r; } char** FGAirport::searchNamesAndIdents(const std::string& aFilter) diff --git a/src/Airports/simple.hxx b/src/Airports/simple.hxx index de7f0b8ca..86045277e 100644 --- a/src/Airports/simple.hxx +++ b/src/Airports/simple.hxx @@ -31,6 +31,7 @@ #include #include +#include #include @@ -40,6 +41,7 @@ class FGRunway; class FGTaxiway; class FGPavement; class SGPropertyNode; +class FGAirport; namespace flightgear { class SID; @@ -52,6 +54,7 @@ namespace flightgear { typedef std::vector WayptVec; typedef std::vector CommStationList; + typedef std::map AirportCache; } @@ -220,6 +223,8 @@ public: flightgear::CommStationList commStations() const; private: + static flightgear::AirportCache airportCache; + // disable these FGAirport operator=(FGAirport &other); FGAirport(const FGAirport&); -- 2.39.5