Skip to content

Commit 9b4c106

Browse files
committed
Use per thread buffer allocation instead of per frame
1 parent d040cdd commit 9b4c106

File tree

8 files changed

+77
-132
lines changed

8 files changed

+77
-132
lines changed

include/BM3D_Base.h

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#define BM3D_BASE_H_
2727

2828

29+
#include <unordered_map>
30+
#include <thread>
2931
#include "BM3D.h"
3032

3133

@@ -52,6 +54,8 @@ class BM3D_Data_Base
5254
_Mypara para;
5355
std::vector<BM3D_FilterData> f;
5456

57+
std::unordered_map<std::thread::id, FLType *> buffer0, buffer1, buffer2;
58+
5559
public:
5660
explicit BM3D_Data_Base(bool _wiener,
5761
const VSAPI *_vsapi = nullptr, std::string _FunctionName = "Base", std::string _NameSpace = "bm3d")
@@ -60,45 +64,26 @@ class BM3D_Data_Base
6064
{}
6165

6266
BM3D_Data_Base(const _Myt &right) = delete;
63-
64-
BM3D_Data_Base(_Myt &&right)
65-
: _Mybase(std::move(right)),
66-
rdef(right.rdef), rnode(right.rnode), rvi(right.rvi),
67-
para_default(right.para_default), para(right.para),
68-
f(std::move(right.f))
69-
{
70-
right.rdef = false;
71-
right.rnode = nullptr;
72-
right.rvi = nullptr;
73-
}
74-
67+
BM3D_Data_Base(_Myt &&right) = delete;
7568
_Myt &operator=(const _Myt &right) = delete;
76-
77-
_Myt &operator=(_Myt &&right)
78-
{
79-
_Mybase::operator=(std::move(right));
80-
81-
if (rdef && rnode) vsapi->freeNode(rnode);
82-
83-
rdef = right.rdef;
84-
rnode = right.rnode;
85-
rvi = right.rvi;
86-
87-
para_default = right.para_default;
88-
para = right.para;
89-
90-
f = std::move(right.f);
91-
92-
right.rdef = false;
93-
right.rnode = nullptr;
94-
right.rvi = nullptr;
95-
96-
return *this;
97-
}
69+
_Myt &operator=(_Myt &&right) = delete;
9870

9971
virtual ~BM3D_Data_Base() override
10072
{
10173
if (rdef && rnode) vsapi->freeNode(rnode);
74+
75+
for (auto &e : buffer0)
76+
{
77+
AlignedFree(e.second);
78+
}
79+
for (auto &e : buffer1)
80+
{
81+
AlignedFree(e.second);
82+
}
83+
for (auto &e : buffer2)
84+
{
85+
AlignedFree(e.second);
86+
}
10287
}
10388

10489
virtual int arguments_process(const VSMap *in, VSMap *out) override;
@@ -135,7 +120,7 @@ class BM3D_Process_Base
135120
typedef BlockGroup<FLType, FLType> block_group;
136121

137122
private:
138-
const _Mydata &d;
123+
_Mydata &d;
139124

140125
protected:
141126
const VSFrameRef *ref = nullptr;
@@ -167,7 +152,7 @@ class BM3D_Process_Base
167152
virtual void process_coreS() override;
168153

169154
public:
170-
BM3D_Process_Base(const _Mydata &_d, int _n, VSFrameContext *_frameCtx, VSCore *_core, const VSAPI *_vsapi)
155+
BM3D_Process_Base(_Mydata &_d, int _n, VSFrameContext *_frameCtx, VSCore *_core, const VSAPI *_vsapi)
171156
: _Mybase(_d, _n, _frameCtx, _core, _vsapi), d(_d)
172157
{
173158
if (d.rdef)

include/BM3D_Basic.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,9 @@ class BM3D_Basic_Data
4545
{}
4646

4747
BM3D_Basic_Data(const _Myt &right) = delete;
48-
49-
BM3D_Basic_Data(_Myt &&right)
50-
: _Mybase(std::move(right))
51-
{}
52-
48+
BM3D_Basic_Data(_Myt &&right) = delete;
5349
_Myt &operator=(const _Myt &right) = delete;
54-
55-
_Myt &operator=(_Myt &&right)
56-
{
57-
_Mybase::operator=(std::move(right));
58-
59-
return *this;
60-
}
50+
_Myt &operator=(_Myt &&right) = delete;
6151

6252
virtual ~BM3D_Basic_Data() override {}
6353

@@ -80,7 +70,7 @@ class BM3D_Basic_Process
8070
const _Mydata &d;
8171

8272
public:
83-
BM3D_Basic_Process(const _Mydata &_d, int _n, VSFrameContext *_frameCtx, VSCore *_core, const VSAPI *_vsapi)
73+
BM3D_Basic_Process(_Mydata &_d, int _n, VSFrameContext *_frameCtx, VSCore *_core, const VSAPI *_vsapi)
8474
: _Mybase(_d, _n, _frameCtx, _core, _vsapi), d(_d)
8575
{}
8676

include/BM3D_Final.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,9 @@ class BM3D_Final_Data
4545
{}
4646

4747
BM3D_Final_Data(const _Myt &right) = delete;
48-
49-
BM3D_Final_Data(_Myt &&right)
50-
: _Mybase(std::move(right))
51-
{}
52-
48+
BM3D_Final_Data(_Myt &&right) = delete;
5349
_Myt &operator=(const _Myt &right) = delete;
54-
55-
_Myt &operator=(_Myt &&right)
56-
{
57-
_Mybase::operator=(std::move(right));
58-
59-
return *this;
60-
}
50+
_Myt &operator=(_Myt &&right) = delete;
6151

6252
virtual ~BM3D_Final_Data() override {}
6353

@@ -80,7 +70,7 @@ class BM3D_Final_Process
8070
const _Mydata &d;
8171

8272
public:
83-
BM3D_Final_Process(const _Mydata &_d, int _n, VSFrameContext *_frameCtx, VSCore *_core, const VSAPI *_vsapi)
73+
BM3D_Final_Process(_Mydata &_d, int _n, VSFrameContext *_frameCtx, VSCore *_core, const VSAPI *_vsapi)
8474
: _Mybase(_d, _n, _frameCtx, _core, _vsapi), d(_d)
8575
{}
8676

include/VBM3D_Base.h

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -78,41 +78,9 @@ class VBM3D_Data_Base
7878
{}
7979

8080
VBM3D_Data_Base(const _Myt &right) = delete;
81-
82-
VBM3D_Data_Base(_Myt &&right)
83-
: _Mybase(std::move(right)),
84-
rdef(right.rdef), rnode(right.rnode), rvi(right.rvi),
85-
para_default(right.para_default), para(right.para),
86-
f(std::move(right.f))
87-
{
88-
right.rdef = false;
89-
right.rnode = nullptr;
90-
right.rvi = nullptr;
91-
}
92-
81+
VBM3D_Data_Base(_Myt &&right) = delete;
9382
_Myt &operator=(const _Myt &right) = delete;
94-
95-
_Myt &operator=(_Myt &&right)
96-
{
97-
_Mybase::operator=(std::move(right));
98-
99-
if (rdef && rnode) vsapi->freeNode(rnode);
100-
101-
rdef = right.rdef;
102-
rnode = right.rnode;
103-
rvi = right.rvi;
104-
105-
para_default = right.para_default;
106-
para = right.para;
107-
108-
f = std::move(right.f);
109-
110-
right.rdef = false;
111-
right.rnode = nullptr;
112-
right.rvi = nullptr;
113-
114-
return *this;
115-
}
83+
_Myt &operator=(_Myt &&right) = delete;
11684

11785
virtual ~VBM3D_Data_Base() override
11886
{

include/VBM3D_Basic.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,9 @@ class VBM3D_Basic_Data
4545
{}
4646

4747
VBM3D_Basic_Data(const _Myt &right) = delete;
48-
49-
VBM3D_Basic_Data(_Myt &&right)
50-
: _Mybase(std::move(right))
51-
{}
52-
48+
VBM3D_Basic_Data(_Myt &&right) = delete;
5349
_Myt &operator=(const _Myt &right) = delete;
54-
55-
_Myt &operator=(_Myt &&right)
56-
{
57-
_Mybase::operator=(std::move(right));
58-
59-
return *this;
60-
}
50+
_Myt &operator=(_Myt &&right) = delete;
6151

6252
virtual ~VBM3D_Basic_Data() override {}
6353

include/VBM3D_Final.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,9 @@ class VBM3D_Final_Data
4545
{}
4646

4747
VBM3D_Final_Data(const _Myt &right) = delete;
48-
49-
VBM3D_Final_Data(_Myt &&right)
50-
: _Mybase(std::move(right))
51-
{}
52-
48+
VBM3D_Final_Data(_Myt &&right) = delete;
5349
_Myt &operator=(const _Myt &right) = delete;
54-
55-
_Myt &operator=(_Myt &&right)
56-
{
57-
_Mybase::operator=(std::move(right));
58-
59-
return *this;
60-
}
50+
_Myt &operator=(_Myt &&right) = delete;
6151

6252
virtual ~VBM3D_Final_Data() override {}
6353

source/BM3D_Base.cpp

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,19 @@ void BM3D_Data_Base::init_filter_data()
293293

294294
void BM3D_Process_Base::Kernel(FLType *dst, const FLType *src, const FLType *ref) const
295295
{
296+
std::thread::id threadId = std::this_thread::get_id();
296297
FLType *ResNum = dst, *ResDen = nullptr;
297298

298-
AlignedMalloc(ResDen, dst_pcount[0]);
299+
if (!d.buffer0.count(threadId))
300+
{
301+
AlignedMalloc(ResDen, dst_pcount[0]);
302+
d.buffer0.emplace(threadId, ResDen);
303+
}
304+
else
305+
{
306+
ResDen = d.buffer0.at(threadId);
307+
}
308+
299309
memset(ResNum, 0, sizeof(FLType) * dst_pcount[0]);
300310
memset(ResDen, 0, sizeof(FLType) * dst_pcount[0]);
301311

@@ -339,36 +349,62 @@ void BM3D_Process_Base::Kernel(FLType *dst, const FLType *src, const FLType *ref
339349
{
340350
dst[i] = ResNum[i] / ResDen[i];
341351
});
342-
343-
AlignedFree(ResDen);
344352
}
345353

346354

347355
void BM3D_Process_Base::Kernel(FLType *dstY, FLType *dstU, FLType *dstV,
348356
const FLType *srcY, const FLType *srcU, const FLType *srcV,
349357
const FLType *refY, const FLType *refU, const FLType *refV) const
350358
{
359+
std::thread::id threadId = std::this_thread::get_id();
351360
FLType *ResNumY = dstY, *ResDenY = nullptr;
352361
FLType *ResNumU = dstU, *ResDenU = nullptr;
353362
FLType *ResNumV = dstV, *ResDenV = nullptr;
354363

355364
if (d.process[0])
356365
{
357-
AlignedMalloc(ResDenY, dst_pcount[0]);
366+
if (!d.buffer0.count(threadId))
367+
{
368+
AlignedMalloc(ResDenY, dst_pcount[0]);
369+
d.buffer0.emplace(threadId, ResDenY);
370+
}
371+
else
372+
{
373+
ResDenY = d.buffer0.at(threadId);
374+
}
375+
358376
memset(ResNumY, 0, sizeof(FLType) * dst_pcount[0]);
359377
memset(ResDenY, 0, sizeof(FLType) * dst_pcount[0]);
360378
}
361379

362380
if (d.process[1])
363381
{
364-
AlignedMalloc(ResDenU, dst_pcount[1]);
382+
if (!d.buffer1.count(threadId))
383+
{
384+
AlignedMalloc(ResDenU, dst_pcount[1]);
385+
d.buffer1.emplace(threadId, ResDenU);
386+
}
387+
else
388+
{
389+
ResDenU = d.buffer1.at(threadId);
390+
}
391+
365392
memset(ResNumU, 0, sizeof(FLType) * dst_pcount[1]);
366393
memset(ResDenU, 0, sizeof(FLType) * dst_pcount[1]);
367394
}
368395

369396
if (d.process[2])
370397
{
371-
AlignedMalloc(ResDenV, dst_pcount[2]);
398+
if (!d.buffer2.count(threadId))
399+
{
400+
AlignedMalloc(ResDenV, dst_pcount[2]);
401+
d.buffer2.emplace(threadId, ResDenV);
402+
}
403+
else
404+
{
405+
ResDenV = d.buffer2.at(threadId);
406+
}
407+
372408
memset(ResNumV, 0, sizeof(FLType) * dst_pcount[2]);
373409
memset(ResDenV, 0, sizeof(FLType) * dst_pcount[2]);
374410
}
@@ -425,10 +461,6 @@ void BM3D_Process_Base::Kernel(FLType *dstY, FLType *dstU, FLType *dstV,
425461
{
426462
dstV[i] = ResNumV[i] / ResDenV[i];
427463
});
428-
429-
if (d.process[0]) AlignedFree(ResDenY);
430-
if (d.process[1]) AlignedFree(ResDenU);
431-
if (d.process[2]) AlignedFree(ResDenV);
432464
}
433465

434466

source/VSPlugin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static void VS_CC BM3D_Basic_Init(VSMap *in, VSMap *out, void **instanceData, VS
154154

155155
static const VSFrameRef *VS_CC BM3D_Basic_GetFrame(int n, int activationReason, void **instanceData, void **frameData, VSFrameContext *frameCtx, VSCore *core, const VSAPI *vsapi)
156156
{
157-
const BM3D_Basic_Data *d = reinterpret_cast<BM3D_Basic_Data *>(*instanceData);
157+
BM3D_Basic_Data *d = reinterpret_cast<BM3D_Basic_Data *>(*instanceData);
158158

159159
if (activationReason == arInitial)
160160
{
@@ -207,7 +207,7 @@ static void VS_CC BM3D_Final_Init(VSMap *in, VSMap *out, void **instanceData, VS
207207

208208
static const VSFrameRef *VS_CC BM3D_Final_GetFrame(int n, int activationReason, void **instanceData, void **frameData, VSFrameContext *frameCtx, VSCore *core, const VSAPI *vsapi)
209209
{
210-
const BM3D_Final_Data *d = reinterpret_cast<BM3D_Final_Data *>(*instanceData);
210+
BM3D_Final_Data *d = reinterpret_cast<BM3D_Final_Data *>(*instanceData);
211211

212212
if (activationReason == arInitial)
213213
{

0 commit comments

Comments
 (0)