From b17b6e7c4f0631658aa22ce62b6f190220ef2e18 Mon Sep 17 00:00:00 2001 From: baldurk Date: Tue, 14 Apr 2020 15:06:22 +0100 Subject: [PATCH] Optimise rdcspv::Editor destruction * If we erase each nop individually it's O(n^2) in all the shifting needed. Since we can't iterate backwards we take advantage of the swap into external SPIR-V and instead just push-back every non-nop opcode. --- .../driver/shaders/spirv/spirv_editor.cpp | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/renderdoc/driver/shaders/spirv/spirv_editor.cpp b/renderdoc/driver/shaders/spirv/spirv_editor.cpp index d932842a7..39193c9cd 100644 --- a/renderdoc/driver/shaders/spirv/spirv_editor.cpp +++ b/renderdoc/driver/shaders/spirv/spirv_editor.cpp @@ -143,13 +143,24 @@ void Editor::CreateEmpty(uint32_t major, uint32_t minor) Editor::~Editor() { - for(size_t i = FirstRealWord; i < m_SPIRV.size();) - { - // don't need to update anything as we're destructing! - while(i < m_SPIRV.size() && m_SPIRV[i] == OpNopWord) - m_SPIRV.erase(i); + m_ExternalSPIRV.clear(); + m_ExternalSPIRV.reserve(m_SPIRV.size()); - uint32_t len = m_SPIRV[i] >> WordCountShift; + // copy into m_ExternalSPIRV, but skipping nops + auto it = m_SPIRV.begin(); + + for(size_t i = 0; i < FirstRealWord; ++i, ++it) + m_ExternalSPIRV.push_back(*it); + + while(it != m_SPIRV.end()) + { + if(*it == OpNopWord) + { + ++it; + continue; + } + + uint32_t len = *it >> WordCountShift; if(len == 0) { @@ -157,10 +168,9 @@ Editor::~Editor() break; } - i += len; + m_ExternalSPIRV.append(it, len); + it += len; } - - m_ExternalSPIRV.swap(m_SPIRV); } Id Editor::MakeId()