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.
This commit is contained in:
parent
439ad4a7c8
commit
830526a793
2 changed files with 21 additions and 11 deletions
|
@ -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)
|
||||
|
|
|
@ -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&);
|
||||
|
|
Loading…
Add table
Reference in a new issue