From 0e20693a8d1c38ea322bfbfb42ef75810cbf99c9 Mon Sep 17 00:00:00 2001 From: Vincent Rabaud Date: Sat, 10 Oct 2026 13:31:52 +0200 Subject: [PATCH] reduce stack buffer size in MatExprInvoker from 128 KB to 32 KB MatExprInvoker::operator() previously allocated two cv::AutoBuffer instances (scratchBuf and tstoreBuf) simultaneously on the stack, consuming over 32 KB per buffer and pushing the stack frame over 36 KB. Furthermore, capElems = 16 * 1024 could allocate up to 128 KB (16K * 8 bytes for CV_64F) before even accounting for multiple scratch slots, exceeding a typical 32 KB L1 data cache. --- modules/core/src/arithm_expr.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/core/src/arithm_expr.cpp b/modules/core/src/arithm_expr.cpp index 679d4b855a..5601ed9fba 100644 --- a/modules/core/src/arithm_expr.cpp +++ b/modules/core/src/arithm_expr.cpp @@ -945,10 +945,10 @@ void TExpr::compile() } for (int b = 0; b < nbuffers; b++) bufEszPrefix[b + 1] += bufEszPrefix[b]; // -> prefix sums - // L1 fragment cap: # elements one ~16KB scratch fragment holds (exec fragments the strip so the + // L1 fragment cap: # elements one ~4KB scratch fragment holds (exec fragments the strip so the // intermediates stay hot). Pure function of the temp byte layout, hence computed here once. const int totalEsz = bufEszPrefix[nbuffers]; - capElems = totalEsz > 0 ? std::max(64, (16 * 1024) / totalEsz) : INT_MAX; + capElems = totalEsz > 0 ? std::max(64, (4 * 1024) / totalEsz) : INT_MAX; } @@ -1294,15 +1294,15 @@ void TExpr::exec(const Mat* const* inputs, Mat* outputs) // Temps present: walk the strip in L1-sized fragments so the intermediates stay hot. // Byte layout (bufEszPrefix) and the fragment cap (capElems) both come from compile(); - // scratch is totalEsz*wf0 (<= ~16KB). + // scratch is totalEsz*wf0 (<= ~4KB). const int totalEsz = bufEszPrefix[nbuffers]; const int wf0 = std::min((int)total, capElems); const size_t region = alignSize((size_t)wf0, 8); // Scratch for the temp buffers. AutoBuffer no longer value-inits its tail, so a fresh per-call - // buffer is free (we only WRITE to it); the inline 16KB covers the L1-capped size, heap backs + // buffer is free (we only WRITE to it); the inline 4KB covers the L1-capped size, heap backs // the rare larger case. uint64_t is used for memory alignment on the worst case. - AutoBuffer scratchBuf( + AutoBuffer scratchBuf( divUp((size_t)totalEsz * region, (unsigned)sizeof(uint64_t))); uchar* scratch = (uchar*)scratchBuf.data(); for (int x0 = 0; x0 < (int)total; x0 += wf0) @@ -1480,8 +1480,8 @@ void TExpr::exec(const Mat* const* inputs, Mat* outputs) const size_t region = alignSize((size_t)bw * bh, 8); // uint64_t elements (see the note on scratchBuf above): keeps the block-local temp store // 8-byte aligned, which the 64-bit-lane kernels require on 32-bit ABIs. - AutoBuffer tstoreBuf( - divUp((size_t)eszPrefix[bc.nbuffers] * region, (unsigned)sizeof(uint64_t))); // inline (<= ~16KB) + AutoBuffer tstoreBuf( + divUp((size_t)eszPrefix[bc.nbuffers] * region, (unsigned)sizeof(uint64_t))); // inline (<= ~4KB) uchar* tstore = (uchar*)tstoreBuf.data(); AutoBuffer args(nsl);