aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Gebben <jeremyg@lunarg.com>2022-12-19 11:08:01 -0700
committerGitHub <noreply@github.com>2022-12-19 13:08:01 -0500
commit025ea891faaffa832116e5411bfcdb70d256b494 (patch)
tree390da45083cb35a2153504db48a7d694428b1c16
parent1c287b03d3dc51c28b9c679e146c269969ce8972 (diff)
downloadspirv-tools-025ea891faaffa832116e5411bfcdb70d256b494.tar.gz
Optimize allocation of spvtools::opt::Instruction::operands_ (#5024)
Reserve space for the entire operand list rather than adding them one a time.
-rw-r--r--source/opt/instruction.cpp10
1 files changed, 10 insertions, 0 deletions
diff --git a/source/opt/instruction.cpp b/source/opt/instruction.cpp
index ab3ca0fd..ece6baf9 100644
--- a/source/opt/instruction.cpp
+++ b/source/opt/instruction.cpp
@@ -73,6 +73,7 @@ Instruction::Instruction(IRContext* c, const spv_parsed_instruction_t& inst,
unique_id_(c->TakeNextUniqueId()),
dbg_line_insts_(std::move(dbg_line)),
dbg_scope_(kNoDebugScope, kNoInlinedAt) {
+ operands_.reserve(inst.num_operands);
for (uint32_t i = 0; i < inst.num_operands; ++i) {
const auto& current_payload = inst.operands[i];
operands_.emplace_back(
@@ -92,6 +93,7 @@ Instruction::Instruction(IRContext* c, const spv_parsed_instruction_t& inst,
has_result_id_(inst.result_id != 0),
unique_id_(c->TakeNextUniqueId()),
dbg_scope_(dbg_scope) {
+ operands_.reserve(inst.num_operands);
for (uint32_t i = 0; i < inst.num_operands; ++i) {
const auto& current_payload = inst.operands[i];
operands_.emplace_back(
@@ -110,6 +112,14 @@ Instruction::Instruction(IRContext* c, spv::Op op, uint32_t ty_id,
unique_id_(c->TakeNextUniqueId()),
operands_(),
dbg_scope_(kNoDebugScope, kNoInlinedAt) {
+ size_t operands_size = in_operands.size();
+ if (has_type_id_) {
+ operands_size++;
+ }
+ if (has_result_id_) {
+ operands_size++;
+ }
+ operands_.reserve(operands_size);
if (has_type_id_) {
operands_.emplace_back(spv_operand_type_t::SPV_OPERAND_TYPE_TYPE_ID,
std::initializer_list<uint32_t>{ty_id});