Skip to content

Commit

Permalink
no copy in AddAndReadVector
Browse files Browse the repository at this point in the history
  • Loading branch information
lixinqi committed Jun 10, 2022
1 parent 4775c72 commit 6fafb4b
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions oneflow/core/common/add_and_read_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ class AddAndReadVector {
CHECK_GE(index, 0);
CHECK_LT(index, size_);
int gran = GetGranularity(index + 1);
return granularity2vector_[gran].data()[index];
int offset = (1 << gran) - 1;
return granularity2vector_[gran].data()[index - offset];
}

// lock free.
T& at(size_t index) {
CHECK_GE(index, 0);
CHECK_LT(index, size_);
int gran = GetGranularity(index + 1);
return granularity2vector_[gran].data()[index];
int offset = (1 << gran) - 1;
return granularity2vector_[gran].data()[index - offset];
}

void push_back(const T& elem) {
Expand All @@ -61,7 +63,6 @@ class AddAndReadVector {
CHECK_LT(next_granularity, N);
CHECK_EQ(next_granularity, GetGranularity(size_ + 1));
granularity2vector_[next_granularity].reserve(1 << next_granularity);
granularity2vector_[next_granularity] = granularity2vector_[granularity];
granularity = next_granularity;
} else if (granularity2vector_[granularity].size() > (1 << granularity)) {
LOG(FATAL) << "fatal bug in AddAndReadVector::push_back";
Expand Down

0 comments on commit 6fafb4b

Please sign in to comment.