Skip to content

Commit

Permalink
Toast: lazily create the grid
Browse files Browse the repository at this point in the history
The toast grid as it is done at the moment uses a lot of memory and
takes a relatively long time to create.

This changes the code so that we only create the grid when a toast
survey needs to be rendered instead of at creation time, saving memory
and startup time in the usual case when we don't use it.
  • Loading branch information
guillaumechereau committed May 16, 2018
1 parent 54a8140 commit 516e65c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/core/StelToast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,14 @@ void ToastTile::draw(StelPainter* sPainter, const SphericalCap& viewportShape, i

/////// ToastSurvey methods ////////////
ToastSurvey::ToastSurvey(const QString& path, int amaxLevel)
: grid(amaxLevel), path(path), maxLevel(amaxLevel), toastCache(200)
: path(path), maxLevel(amaxLevel), toastCache(200)
{
rootTile = new ToastTile(this, 0, 0, 0);
}

ToastSurvey::~ToastSurvey()
{
delete rootTile;
delete grid;
rootTile = Q_NULLPTR;
}

Expand All @@ -251,6 +251,10 @@ void ToastSurvey::draw(StelPainter* sPainter)
const double maxAngle = anglePerPixel * getTilesSize();
int maxVisibleLevel = (int)(log2(360. / maxAngle));

// Lazily creation of the grid and root tile.
if (!grid) grid = new ToastGrid(maxLevel);
if (!rootTile) rootTile = new ToastTile(this, 0, 0, 0);

// We also get the viewport shape to discard invisibly tiles.
const SphericalCap& viewportRegion = sPainter->getProjector()->getBoundingCap();
rootTile->draw(sPainter, viewportRegion, maxVisibleLevel);
Expand Down
6 changes: 3 additions & 3 deletions src/core/StelToast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class ToastSurvey : public QObject
virtual ~ToastSurvey();
QString getTilePath(int level, int x, int y) const;
void draw(StelPainter* sPainter);
const ToastGrid* getGrid() const {return &grid;}
const ToastGrid* getGrid() const {Q_ASSERT(grid); return grid;}
int getMaxLevel() const {return maxLevel;}
int getTilesSize() const {return 256;}

Expand All @@ -146,9 +146,9 @@ class ToastSurvey : public QObject
void putIntoCache(ToastTile* tile);

private:
ToastGrid grid;
ToastGrid* grid = Q_NULLPTR;
QString path;
ToastTile* rootTile;
ToastTile* rootTile = Q_NULLPTR;
int maxLevel;

typedef QCache<ToastTile::Coord, ToastTile> ToastCache;
Expand Down

0 comments on commit 516e65c

Please sign in to comment.