Skip to content

Commit b9aceed

Browse files
authored
reduce heap allocations and deallocations in proof_trace_callback_writer (#1198)
Avoid setup and tear-down costs for current_call_event_ and current_rewrite_event_ by providing members to replace and reuse rather than using optional::reset() and emplace().
1 parent c55f039 commit b9aceed

File tree

1 file changed

+37
-39
lines changed

1 file changed

+37
-39
lines changed

include/runtime/proof_trace_writer.h

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,16 @@ class proof_trace_callback_writer : public proof_trace_writer {
249249
char const *location;
250250
std::vector<kore_term_construction> arguments;
251251
std::optional<kore_term_construction> result;
252-
253-
call_event_construction(
254-
char const *hook_name, char const *symbol_name, char const *location)
255-
: hook_name(hook_name)
256-
, symbol_name(symbol_name)
257-
, location(location) { }
258-
259-
call_event_construction(char const *symbol_name, char const *location)
260-
: hook_name(nullptr)
261-
, symbol_name(symbol_name)
262-
, location(location) { }
252+
//
253+
// To avoid a heap allocation and deallocation for arguments on each event we reuse it.
254+
//
255+
void reinitialize(char const *h_name, char const *s_name, char const *loc) {
256+
hook_name = h_name;
257+
symbol_name = s_name;
258+
location = loc;
259+
arguments.clear();
260+
result.reset();
261+
}
263262
};
264263

265264
struct rewrite_event_construction {
@@ -270,20 +269,23 @@ class proof_trace_callback_writer : public proof_trace_writer {
270269
uint64_t arity;
271270
size_t pos;
272271
subst_t substitution;
273-
274-
rewrite_event_construction(uint64_t ordinal, uint64_t arity)
275-
: ordinal(ordinal)
276-
, arity(arity)
277-
, pos(0) {
272+
//
273+
// To avoid a heap allocation and deallocation for substitution on each event we reuse it.
274+
//
275+
void reinitialize(uint64_t ord, uint64_t arty) {
276+
ordinal = ord;
277+
arity = arty;
278+
pos = 0;
278279
substitution.resize(arity);
279280
}
280281
};
281282

282283
private:
283-
std::optional<call_event_construction> current_call_event_;
284-
285-
std::optional<rewrite_event_construction> current_rewrite_event_{
286-
std::nullopt};
284+
//
285+
// These structs get reused.
286+
//
287+
call_event_construction current_call_event_;
288+
rewrite_event_construction current_rewrite_event_;
287289

288290
bool rewrite_callback_pending_;
289291

@@ -315,25 +317,23 @@ class proof_trace_callback_writer : public proof_trace_writer {
315317
void hook_event_pre(
316318
char const *name, char const *pattern,
317319
char const *location_stack) override {
318-
current_call_event_.reset();
319-
current_call_event_.emplace(name, pattern, location_stack);
320+
current_call_event_.reinitialize(name, pattern, location_stack);
320321
}
321322

322323
void hook_event_post(
323324
void *hook_result, uint64_t block_header, uint64_t bits) override {
324-
current_call_event_->result.emplace(hook_result, block_header, bits);
325-
hook_event_callback(current_call_event_.value());
325+
current_call_event_.result.emplace(hook_result, block_header, bits);
326+
hook_event_callback(current_call_event_);
326327
}
327328

328329
void argument(void *arg, uint64_t block_header, uint64_t bits) override {
329-
current_call_event_->arguments.emplace_back(arg, block_header, bits);
330+
current_call_event_.arguments.emplace_back(arg, block_header, bits);
330331
}
331332

332333
void rewrite_event_pre(uint64_t ordinal, uint64_t arity) override {
333-
current_rewrite_event_.reset();
334-
current_rewrite_event_.emplace(ordinal, arity);
334+
current_rewrite_event_.reinitialize(ordinal, arity);
335335
if (arity == 0) {
336-
rewrite_event_callback(current_rewrite_event_.value());
336+
rewrite_event_callback(current_rewrite_event_);
337337
} else {
338338
rewrite_callback_pending_ = true;
339339
}
@@ -342,18 +342,18 @@ class proof_trace_callback_writer : public proof_trace_writer {
342342
void variable(
343343
char const *name, void *var, uint64_t block_header,
344344
uint64_t bits) override {
345-
auto &p = current_rewrite_event_->substitution[current_rewrite_event_->pos];
345+
auto &p = current_rewrite_event_.substitution[current_rewrite_event_.pos];
346346
p.first = name;
347347
p.second.subject = var;
348348
p.second.block_header = block_header;
349349
p.second.bits = bits;
350-
size_t new_pos = ++current_rewrite_event_->pos;
351-
if (new_pos == current_rewrite_event_->arity) {
350+
size_t new_pos = ++current_rewrite_event_.pos;
351+
if (new_pos == current_rewrite_event_.arity) {
352352
if (rewrite_callback_pending_) {
353-
rewrite_event_callback(current_rewrite_event_.value());
353+
rewrite_event_callback(current_rewrite_event_);
354354
rewrite_callback_pending_ = false;
355355
} else {
356-
side_condition_event_callback(current_rewrite_event_.value());
356+
side_condition_event_callback(current_rewrite_event_);
357357
}
358358
}
359359
}
@@ -366,19 +366,17 @@ class proof_trace_callback_writer : public proof_trace_writer {
366366

367367
void
368368
function_event_pre(char const *name, char const *location_stack) override {
369-
current_call_event_.reset();
370-
current_call_event_.emplace(name, location_stack);
369+
current_call_event_.reinitialize(nullptr, name, location_stack);
371370
}
372371

373372
void function_event_post() override {
374-
function_event_callback(current_call_event_.value());
373+
function_event_callback(current_call_event_);
375374
}
376375

377376
void side_condition_event_pre(uint64_t ordinal, uint64_t arity) override {
378-
current_rewrite_event_.reset();
379-
current_rewrite_event_.emplace(ordinal, arity);
377+
current_rewrite_event_.reinitialize(ordinal, arity);
380378
if (arity == 0) {
381-
side_condition_event_callback(current_rewrite_event_.value());
379+
side_condition_event_callback(current_rewrite_event_);
382380
}
383381
}
384382

0 commit comments

Comments
 (0)