Skip to content

Commit

Permalink
Optimize fiber_sbox2's performance by using array.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengshuxin committed Dec 30, 2024
1 parent fe42748 commit 60e8160
Showing 1 changed file with 54 additions and 15 deletions.
69 changes: 54 additions & 15 deletions lib_fiber/cpp/include/fiber/fiber_sem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,49 @@ template<typename T>
class fiber_sbox2 : public box2<T> {
public:
explicit fiber_sbox2(bool async = true)
: sem_(0, async ? fiber_sem_t_async : fiber_sem_t_sync) {}
: sem_(0, async ? fiber_sem_t_async : fiber_sem_t_sync)
, capacity_(1000)
, off_curr_(0)
, off_next_(0)
{
sbox_ = (T*) malloc(sizeof(T) * capacity_);
}

explicit fiber_sbox2(int buf)
: sem_(0, buf) {}
: sem_(0, buf)
, capacity_(1000)
, off_curr_(0)
, off_next_(0)
{
sbox_ = (T*) malloc(sizeof(T) * capacity_);
}

~fiber_sbox2() {}

// @override
bool push(T t, bool dummy = false) {
(void) dummy;
#if __cplusplus >= 201103L || defined(USE_CPP11) // Support c++11 ?
sbox_.emplace_back(std::move(t));

if (off_next_ == capacity_) {
if (off_curr_ >= 1000) {
#if 1
size_t n = 0;
for (size_t i = off_curr_; i < off_next_; i++) {
sbox_[n++] = sbox_[i];
}
#else
sbox_.push_back(t);
memmove(array_, array_ + off_curr_,
(off_next_ - off_curr_) * sizeof(T*));
#endif

off_next_ -= off_curr_;
off_curr_ = 0;
} else {
capacity_ += 10000;
sbox_ = (T*) realloc(sbox_, sizeof(T) * capacity_);
}
}
sbox_[off_next_++] = t;
sem_.post();
return true;
}
Expand All @@ -171,12 +199,16 @@ class fiber_sbox2 : public box2<T> {
return false;
}

#if __cplusplus >= 201103L || defined(USE_CPP11) // Support c++11 ?
t = std::move(sbox_.front());
#if __cplusplus >= 201103L || defined(USE_CPP11)
t = std::move(sbox_[off_curr_++]);
#else
t = sbox_.front();
t = sbox_[off_curr_++];
#endif
sbox_.pop_front();
if (off_curr_ == off_next_) {
if (off_curr_ > 0) {
off_curr_ = off_next_ = 0;
}
}
return true;
}

Expand All @@ -188,13 +220,17 @@ class fiber_sbox2 : public box2<T> {
return n;
}

T t = sbox_.front();
#if __cplusplus >= 201103L || defined(USE_CPP11) // Support c++11 ?
out.emplace_back(std::move(t));
#if __cplusplus >= 201103L || defined(USE_CPP11)
out.emplace_back(std::move(sbox_[off_curr_++]));
#else
out.push_back(t);
out.push_back(sbox_[off_curr_++]);
#endif
n++;
if (off_curr_ == off_next_) {
if (off_curr_ > 0) {
off_curr_ = off_next_ = 0;
}
}
if (max > 0 && n >= max) {
return n;
}
Expand All @@ -204,7 +240,7 @@ class fiber_sbox2 : public box2<T> {

// @override
size_t size() const {
return sem_.num();
return off_next_ - off_curr_;
}

// @override
Expand All @@ -214,7 +250,10 @@ class fiber_sbox2 : public box2<T> {

private:
fiber_sem sem_;
std::list<T> sbox_;
T* sbox_;
size_t capacity_;
size_t off_curr_;
size_t off_next_;

fiber_sbox2(const fiber_sbox2&);
const fiber_sbox2& operator=(const fiber_sbox2&);
Expand Down

0 comments on commit 60e8160

Please sign in to comment.