With many draw calls, doing heap allocations is an overhead as it translate in OS API call.
Since you talk about performance, I assume you ran a profiler. What kind of bottleneck did the profiler report?
Yes, run profiler on other situation involving heap allocations, not this.
I prefer no heap allocations anywhere after loading resources.
transientContext exists only once per thread and as you can see in the code you post, it only get allocated if it wasn't allocated already.
// This per-thread variable tracks if and how a transient
// context is currently being used on the current thread
sf::ThreadLocalPtr<TransientContext> transientContext(NULL);
Then why I get alloc/dealloc all the time in the same frame, and the same main thread?
Continously calling new and delete on the transientContext.
This is a snapshot in the same frame, the same thread:
Not Flagged > 6188 0 Main Thread Main Thread Game.exe!MemoryAllocHook Normal
Game.exe!MemoryAllocHook(int nAllocType, void * pvData, unsigned int nSize, int nBlockUse, long lRequest, const unsigned char * szFileName, int nLine) Line 54
[External Code]
[Frames below may be incorrect and/or missing, no symbols loaded for ucrtbased.dll]
Game.exe!sf::priv::GlContext::acquireTransientContext() Line 316
Game.exe!sf::GlResource::TransientContextLock::TransientContextLock() Line 63
Game.exe!sf::Texture::bind(const sf::Texture * texture, sf::Texture::CoordinateType coordinateType) Line 619
Game.exe!sf::RenderTarget::applyTexture(const sf::Texture * texture) Line 492
Game.exe!sf::RenderTarget::clear(const sf::Color & color) Line 106
Game.exe!PD_Screen::Clear() Line 53
Game.exe!GameManager::Draw() Line 501
Game.exe!GameManager::Run() Line 337
Game.exe!Run() Line 44
Game.exe!main() Line 55
[External Code]
Not Flagged > 6188 0 Main Thread Main Thread Game.exe!MemoryAllocHook Normal
Game.exe!MemoryAllocHook(int nAllocType, void * pvData, unsigned int nSize, int nBlockUse, long lRequest, const unsigned char * szFileName, int nLine) Line 54
[External Code]
[Frames below may be incorrect and/or missing, no symbols loaded for ucrtbased.dll]
Game.exe!sf::priv::GlContext::releaseTransientContext() Line 339
Game.exe!sf::GlResource::TransientContextLock::~TransientContextLock() Line 70
Game.exe!sf::Texture::bind(const sf::Texture * texture, sf::Texture::CoordinateType coordinateType) Line 669
Game.exe!sf::RenderTarget::applyTexture(const sf::Texture * texture) Line 492
Game.exe!sf::RenderTarget::clear(const sf::Color & color) Line 106
Game.exe!PD_Screen::Clear() Line 53
Game.exe!GameManager::Draw() Line 501
Game.exe!GameManager::Run() Line 337
Game.exe!Run() Line 44
Game.exe!main() Line 55
[External Code]
Not Flagged > 6188 0 Main Thread Main Thread Game.exe!MemoryAllocHook Normal
Game.exe!MemoryAllocHook(int nAllocType, void * pvData, unsigned int nSize, int nBlockUse, long lRequest, const unsigned char * szFileName, int nLine) Line 54
[External Code]
[Frames below may be incorrect and/or missing, no symbols loaded for ucrtbased.dll]
Game.exe!sf::priv::GlContext::acquireTransientContext() Line 316
Game.exe!sf::GlResource::TransientContextLock::TransientContextLock() Line 63
Game.exe!sf::Texture::bind(const sf::Texture * texture, sf::Texture::CoordinateType coordinateType) Line 619
Game.exe!sf::RenderTarget::applyTexture(const sf::Texture * texture) Line 492
Game.exe!sf::RenderTarget::draw(const sf::Vertex * vertices, unsigned int vertexCount, sf::PrimitiveType type, const sf::RenderStates & states) Line 259
Game.exe!sf::Sprite::draw(sf::RenderTarget & target, sf::RenderStates states) Line 145
Game.exe!sf::RenderTarget::draw(const sf::Drawable & drawable, const sf::RenderStates & states) Line 195
Game.exe!PD_Screen::Draw(const ISprite & xSprite, RenderState::BLEND_MODE eBlendMode, const IShader * pxShader) Line 61
Game.exe!Widget::Draw(IRenderTarget & xRenderTarget, RenderState::BLEND_MODE eBlendMode) Line 137
Game.exe!Gui::Draw(IRenderTarget & xRenderTarget) Line 64
Game.exe!MenuHandler::Draw(IRenderTarget & xRenderTarget) Line 46
Game.exe!GameManager::Draw() Line 508
Game.exe!GameManager::Run() Line 337
Game.exe!Run() Line 44
Game.exe!main() Line 55
[External Code]
Not Flagged > 6188 0 Main Thread Main Thread Game.exe!MemoryAllocHook Normal
Game.exe!MemoryAllocHook(int nAllocType, void * pvData, unsigned int nSize, int nBlockUse, long lRequest, const unsigned char * szFileName, int nLine) Line 54
[External Code]
[Frames below may be incorrect and/or missing, no symbols loaded for ucrtbased.dll]
Game.exe!sf::priv::GlContext::releaseTransientContext() Line 339
Game.exe!sf::GlResource::TransientContextLock::~TransientContextLock() Line 70
Game.exe!sf::Texture::bind(const sf::Texture * texture, sf::Texture::CoordinateType coordinateType) Line 669
Game.exe!sf::RenderTarget::applyTexture(const sf::Texture * texture) Line 492
Game.exe!sf::RenderTarget::draw(const sf::Vertex * vertices, unsigned int vertexCount, sf::PrimitiveType type, const sf::RenderStates & states) Line 259
Game.exe!sf::Sprite::draw(sf::RenderTarget & target, sf::RenderStates states) Line 145
Game.exe!sf::RenderTarget::draw(const sf::Drawable & drawable, const sf::RenderStates & states) Line 195
Game.exe!PD_Screen::Draw(const ISprite & xSprite, RenderState::BLEND_MODE eBlendMode, const IShader * pxShader) Line 61
Game.exe!Widget::Draw(IRenderTarget & xRenderTarget, RenderState::BLEND_MODE eBlendMode) Line 137
Game.exe!Gui::Draw(IRenderTarget & xRenderTarget) Line 64
Game.exe!MenuHandler::Draw(IRenderTarget & xRenderTarget) Line 46
Game.exe!GameManager::Draw() Line 508
Game.exe!GameManager::Run() Line 337
Game.exe!Run() Line 44
Game.exe!main() Line 55
[External Code]
MemoryAllocHook detects heap allocations/deallocations.
It looks like the transientContext is newed, then deleted in releaseTransientContext, then newed again, then released etc...
So as you can see, it gets allocated all the time, as it is getting deleted all the time.
If this is the normal behaviour, is it possible to make it in a way to avoid new and delete?