@@ -406,6 +406,8 @@ class Mapping
406406
407407 Mapping (utl::Logger* logger, const Subject& subject);
408408
409+ ClassMatch* reserveMatches (int n);
410+
409411 ClassMatch* allocMatches (int n);
410412
411413 Node* node (Net net) { return &nodes_[Graph::netId (net)]; }
@@ -466,13 +468,22 @@ bool Mapping::isMappable(Net net)
466468 return isMappable (inst);
467469}
468470
469- ClassMatch* Mapping::allocMatches (const int n)
471+ ClassMatch* Mapping::reserveMatches (const int n)
470472{
473+ assert (n <= kArenaChunk );
471474 if (arena_slot_ + n > kArenaChunk || match_arena_.empty ()) {
472475 match_arena_.push_back (std::make_unique<ClassMatch[]>(kArenaChunk ));
473476 arena_slot_ = 0 ;
474477 }
475478 ClassMatch* ret = &match_arena_.back ()[arena_slot_];
479+ // We are reserving but not allocating: do not increment arena_slot_
480+ return ret;
481+ }
482+
483+ ClassMatch* Mapping::allocMatches (const int n)
484+ {
485+ assert (n <= kArenaChunk );
486+ ClassMatch* ret = reserveMatches (n);
476487 arena_slot_ += n;
477488 return ret;
478489}
@@ -673,7 +684,11 @@ void Mapping::prepareMatches(const int npriority_cuts,
673684
674685 int matchSlot = 0 ;
675686 int psSlot = 0 ;
676- ClassMatch* matchBuf = allocMatches (nmatches_max);
687+
688+ // Conservatively reserve a buffer for the maximal number of matches.
689+ // Later we will call `allocMatches` to allocate the used portion of
690+ // the buffer. This way we don't overallocate and waste slots.
691+ ClassMatch* matchBuf = reserveMatches (nmatches_max);
677692
678693 for (int i = -1 ; i < (int ) cache[n1->fid ].ps .size (); i++) {
679694 for (int j = -1 ; j < (int ) cache[n2->fid ].ps .size (); j++) {
@@ -783,6 +798,9 @@ void Mapping::prepareMatches(const int npriority_cuts,
783798 lcache->ps = std::span<PriorityCut>(
784799 &pcuts[static_cast <size_t >(cur->fid ) * npriority_cuts], psSlot);
785800 lcache->mark = net;
801+ ClassMatch* allocatedBuf = allocMatches (matchSlot);
802+ (void ) allocatedBuf;
803+ assert (allocatedBuf == matchBuf);
786804 cur->matches = matchBuf;
787805 cur->nmatches = matchSlot;
788806 });
0 commit comments