Skip to content

Commit 8bf1795

Browse files
committed
Dynamic FlatFIT Optimization
Made the query stack a member variable to promote reuse and avoid frequently allocating/deallocating small arrays.
1 parent 1bd325f commit 8bf1795

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

cpp/src/DynamicFlatFIT.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define __DYNAMIC_FLATFIT_H__
33

44
#include<vector>
5-
#include<stack>
65
#include<iterator>
76
#include<algorithm>
87
#include<cassert>
@@ -71,22 +70,22 @@ namespace dynamic_flatfit {
7170

7271

7372
outT query() {
73+
// invariant: _tracing_indicies is empty coming in
7474
if (_size == 0)
7575
return _binOp.lower(_identE);
7676

7777
// for non-empty cases
78-
std::stack<int> tracing_indices;
7978
for (int cur=_front;cur!=_back;cur=_buffer[cur]._next)
80-
tracing_indices.push(cur);
79+
_tracing_indices.push_back(cur);
8180

8281
aggT theSum = _identE;
83-
while (!tracing_indices.empty()) {
84-
int index = tracing_indices.top();
82+
while (!_tracing_indices.empty()) {
83+
int index = _tracing_indices.back();
8584
theSum = _binOp.combine(_buffer[index]._val, theSum);
8685
_buffer[index] = AggT(theSum, _back);
87-
tracing_indices.pop();
86+
_tracing_indices.pop_back();
8887
}
89-
88+
// invariant: _tracing_indicies is empty going out
9089
return _binOp.lower(_binOp.combine(theSum, _buffer[_back]._val));
9190
}
9291

@@ -100,6 +99,7 @@ namespace dynamic_flatfit {
10099
private:
101100
int _size;
102101
std::vector<AggT> _buffer;
102+
std::vector<int> _tracing_indices;
103103
bool _ever_evicted;
104104
int32_t _front, _back;
105105

cpp/src/TimestampedDynamicFlatFIT.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define __TIMESTAMPED_DYNAMIC_FLATFIT_H__
33

44
#include<vector>
5-
#include<stack>
65
#include<iterator>
76
#include<algorithm>
87
#include<cassert>
@@ -75,22 +74,22 @@ namespace timestamped_dynamic_flatfit {
7574

7675

7776
outT query() {
77+
// invariant: _tracing_indicies is empty coming in
7878
if (_size == 0)
7979
return _binOp.lower(_identE);
8080

8181
// for non-empty cases
82-
std::stack<int> tracing_indices;
8382
for (int cur=_front;cur!=_back;cur=_buffer[cur]._next)
84-
tracing_indices.push(cur);
83+
_tracing_indices.push_back(cur);
8584

8685
aggT theSum = _identE;
87-
while (!tracing_indices.empty()) {
88-
int index = tracing_indices.top();
86+
while (!_tracing_indices.empty()) {
87+
int index = _tracing_indices.back();
8988
theSum = _binOp.combine(_buffer[index]._val, theSum);
9089
_buffer[index] = AggT(theSum, _back, _buffer[index]._timestamp);
91-
tracing_indices.pop();
90+
_tracing_indices.pop_back();
9291
}
93-
92+
// invariant: _tracing_indicies is empty going out
9493
return _binOp.lower(_binOp.combine(theSum, _buffer[_back]._val));
9594
}
9695
timeT oldest() { return _buffer[_front]._timestamp; }
@@ -107,6 +106,7 @@ namespace timestamped_dynamic_flatfit {
107106
private:
108107
int _size;
109108
std::vector<AggT> _buffer;
109+
std::vector<int> _tracing_indices;
110110
bool _ever_evicted;
111111
int32_t _front, _back;
112112

0 commit comments

Comments
 (0)