]> git.mxchange.org Git - flightgear.git/commitdiff
Traffic performance: add airport memory cache
authorThorstenB <brehmt@gmail.com>
Sat, 24 Nov 2012 11:42:31 +0000 (12:42 +0100)
committerThorstenB <brehmt@gmail.com>
Sat, 24 Nov 2012 11:42:31 +0000 (12:42 +0100)
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
src/Airports/simple.hxx

index 9fff74dc98d9df56a933958b77f0ab9bed65418e..ff12662df95aa19f5bf35b96c96815b6c8d3e1f4 100644 (file)
@@ -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<FGAirport*>(r.ptr());
+  FGAirport* r = static_cast<FGAirport*> (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<FGAirport*>(r.ptr());
+  return r;
 }
 
 char** FGAirport::searchNamesAndIdents(const std::string& aFilter)
index de7f0b8cab153568631e8e45b5532c060170ee29..86045277ef668710300c17fd77af11b46fab39f0 100644 (file)
@@ -31,6 +31,7 @@
 
 #include <string>
 #include <vector>
+#include <map>
 
 #include <Navaids/positioned.hxx>
 
@@ -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<WayptRef> WayptVec;
   
   typedef std::vector<CommStation*> CommStationList;
+  typedef std::map<std::string, FGAirport*> AirportCache;
 }
 
 
@@ -220,6 +223,8 @@ public:
     
     flightgear::CommStationList commStations() const;
 private:
+    static flightgear::AirportCache airportCache;
+
     // disable these
     FGAirport operator=(FGAirport &other);
     FGAirport(const FGAirport&);