Cache wxDisplayImpl object for faster access

Avoid a heap allocation on every wxDisplay creation by caching the
wxDisplayImpl objects once they're created.

Currently we never invalidate the cache, but we should add a way to do
it in the future.

This speeds up wxDisplay::GetGeometry() benchmark by a factor of 4.
This commit is contained in:
Vadim Zeitlin
2018-09-30 00:53:59 +02:00
parent d6793893c0
commit 990c8bfd73
3 changed files with 36 additions and 12 deletions

View File

@@ -118,12 +118,7 @@ wxDisplay::wxDisplay(unsigned n)
wxASSERT_MSG( n == 0 || n < GetCount(),
wxT("An invalid index was passed to wxDisplay") );
m_impl = Factory().CreateDisplay(n);
}
wxDisplay::~wxDisplay()
{
delete m_impl;
m_impl = Factory().GetDisplay(n);
}
// ----------------------------------------------------------------------------
@@ -230,6 +225,15 @@ bool wxDisplay::ChangeMode(const wxVideoMode& mode)
// wxDisplayFactory implementation
// ============================================================================
wxDisplayFactory::~wxDisplayFactory()
{
for ( size_t n = 0; n < m_impls.size(); ++n )
{
// It can be null, that's ok.
delete m_impls[n];
}
}
int wxDisplayFactory::GetFromWindow(const wxWindow *window)
{
// consider that the window belongs to the display containing its centre