Skip to content

Commit

Permalink
chore: replace ARRAY_ON_STACK with utils::ArrayOnStack
Browse files Browse the repository at this point in the history
  • Loading branch information
zz-jason committed Dec 20, 2023
1 parent 3078fa5 commit dda0764
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 90 deletions.
27 changes: 12 additions & 15 deletions source/storage/btree/BTreeLL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include "concurrency-recovery/CRMG.hpp"
#include "storage/btree/core/BTreeExclusiveIterator.hpp"
#include "storage/btree/core/BTreeSharedIterator.hpp"
#include "utils/Misc.hpp"

#include <gflags/gflags.h>
#include <glog/logging.h>

#include <alloca.h>
#include <signal.h>

using namespace std;
Expand All @@ -17,8 +17,6 @@ namespace leanstore {
namespace storage {
namespace btree {

#define ARRAY_ON_STACK(varName, T, N) T* varName = (T*)alloca((N) * sizeof(T));

OP_RESULT BTreeLL::Lookup(Slice key, ValCallback valCallback) {
DCHECK(cr::Worker::my().IsTxStarted());
while (true) {
Expand Down Expand Up @@ -180,8 +178,8 @@ OP_RESULT BTreeLL::prefixLookup(Slice key, PrefixLookupCallback callback) {
guardedLeaf.JumpIfModifiedByOthers();
JUMPMU_RETURN OP_RESULT::OK;
} else if (cur < guardedLeaf->mNumSeps) {
u16 fullKeySize = guardedLeaf->getFullKeyLen(cur);
ARRAY_ON_STACK(fullKeyBuf, u8, fullKeySize);
auto fullKeySize = guardedLeaf->getFullKeyLen(cur);
auto fullKeyBuf = utils::ArrayOnStack<u8>(fullKeySize);
guardedLeaf->copyFullKey(cur, fullKeyBuf);
guardedLeaf.JumpIfModifiedByOthers();

Expand Down Expand Up @@ -221,9 +219,8 @@ OP_RESULT BTreeLL::prefixLookupForPrev(Slice key,
JUMPMU_RETURN OP_RESULT::OK;
} else if (cur > 0) {
cur -= 1;

u16 fullKeySize = guardedLeaf->getFullKeyLen(cur);
ARRAY_ON_STACK(fullKeyBuf, u8, fullKeySize);
auto fullKeySize = guardedLeaf->getFullKeyLen(cur);
auto fullKeyBuf = utils::ArrayOnStack<u8>(fullKeySize);
guardedLeaf->copyFullKey(cur, fullKeyBuf);
guardedLeaf.JumpIfModifiedByOthers();

Expand Down Expand Up @@ -461,17 +458,17 @@ OP_RESULT BTreeLL::rangeRemove(Slice startKey, Slice endKey, bool page_wise) {
}

// page start key
ARRAY_ON_STACK(firstKey, u8, guardedLeaf->getFullKeyLen(0));
auto firstKeySize = guardedLeaf->getFullKeyLen(0);
auto firstKey = utils::ArrayOnStack<u8>(firstKeySize);
guardedLeaf->copyFullKey(0, firstKey);
Slice pageStartKey(firstKey, guardedLeaf->getFullKeyLen(0));
Slice pageStartKey(firstKey, firstKeySize);

// page end key
ARRAY_ON_STACK(
lastKey, u8,
guardedLeaf->getFullKeyLen(guardedLeaf->mNumSeps - 1));
auto lastKeySize =
guardedLeaf->getFullKeyLen(guardedLeaf->mNumSeps - 1);
auto lastKey = utils::ArrayOnStack<u8>(lastKeySize);
guardedLeaf->copyFullKey(guardedLeaf->mNumSeps - 1, lastKey);
Slice pageEndKey(
lastKey, guardedLeaf->getFullKeyLen(guardedLeaf->mNumSeps - 1));
Slice pageEndKey(lastKey, lastKeySize);

if (pageStartKey >= startKey && pageEndKey <= endKey) {
// Purge the whole page
Expand Down
41 changes: 20 additions & 21 deletions source/storage/btree/FatTupleDifferentAttributes.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "BTreeVI.hpp"
#include "concurrency-recovery/CRMG.hpp"
#include "utils/Misc.hpp"

#include "gflags/gflags.h"

Expand Down Expand Up @@ -28,8 +29,6 @@ template <> class hash<leanstore::UpdateSameSizeInPlaceDescriptor::Slot> {

namespace leanstore::storage::btree {

#define ARRAY_ON_STACK(varName, T, N) T* varName = (T*)alloca((N) * sizeof(T));

void FatTupleDifferentAttributes::undoLastUpdate() {
ENSURE(deltas_count >= 1);
auto& delta = getDelta(deltas_count - 1);
Expand Down Expand Up @@ -123,8 +122,8 @@ void FatTupleDifferentAttributes::garbageCollection() {
return;
}

u32 buffer_size = total_space + sizeof(FatTupleDifferentAttributes);
ARRAY_ON_STACK(buffer, u8, buffer_size);
auto bufferSize = total_space + sizeof(FatTupleDifferentAttributes);
auto buffer = utils::ArrayOnStack<u8>(bufferSize);
auto& new_fat_tuple = *new (buffer) FatTupleDifferentAttributes(total_space);
new_fat_tuple.mWorkerId = mWorkerId;
new_fat_tuple.tx_ts = tx_ts;
Expand Down Expand Up @@ -194,7 +193,7 @@ void FatTupleDifferentAttributes::garbageCollection() {
}
}

std::memcpy(this, buffer, buffer_size);
std::memcpy(this, buffer, bufferSize);
assert(total_space >= used_space);

DEBUG_BLOCK() {
Expand Down Expand Up @@ -336,17 +335,17 @@ std::tuple<OP_RESULT, u16> FatTupleDifferentAttributes::reconstructTuple(
valCallback(Slice(getValueConstant(), value_length));
return {OP_RESULT::OK, 1};
} else if (deltas_count > 0) {
ARRAY_ON_STACK(materialized_value, u8, value_length);
std::memcpy(materialized_value, getValueConstant(), value_length);
auto materializedValue = utils::ArrayOnStack<u8>(value_length);
std::memcpy(materializedValue, getValueConstant(), value_length);
// we have to apply the diffs
u16 chain_length = 2;
for (s16 d_i = deltas_count - 1; d_i >= 0; d_i--) {
auto& delta = getDeltaConstant(d_i);
BTreeLL::applyDiff(
delta.getConstantDescriptor(), materialized_value,
delta.getConstantDescriptor(), materializedValue,
delta.payload + delta.getConstantDescriptor().size()); // Apply diff
if (cr::Worker::my().cc.isVisibleForMe(delta.mWorkerId, delta.tx_ts)) {
valCallback(Slice(materialized_value, value_length));
valCallback(Slice(materializedValue, value_length));
return {OP_RESULT::OK, chain_length};
} else {
chain_length++;
Expand All @@ -361,16 +360,16 @@ std::tuple<OP_RESULT, u16> FatTupleDifferentAttributes::reconstructTuple(
}
}

void FatTupleDifferentAttributes::resize(const u32 new_length) {
ARRAY_ON_STACK(tmp_page, u8,
new_length + sizeof(FatTupleDifferentAttributes));
auto& new_fat_tuple = *new (tmp_page) FatTupleDifferentAttributes(new_length);
new_fat_tuple.mWorkerId = mWorkerId;
new_fat_tuple.tx_ts = tx_ts;
new_fat_tuple.command_id = command_id;
new_fat_tuple.used_space += value_length;
new_fat_tuple.value_length = value_length;
std::memcpy(new_fat_tuple.payload, payload, value_length); // Copy value
void FatTupleDifferentAttributes::resize(const u32 newLength) {
auto tmpPageSize = newLength + sizeof(FatTupleDifferentAttributes);
auto tmpPage = utils::ArrayOnStack<u8>(tmpPageSize);
auto& newFatTuple = *new (tmpPage) FatTupleDifferentAttributes(newLength);
newFatTuple.mWorkerId = mWorkerId;
newFatTuple.tx_ts = tx_ts;
newFatTuple.command_id = command_id;
newFatTuple.used_space += value_length;
newFatTuple.value_length = value_length;
std::memcpy(newFatTuple.payload, payload, value_length); // Copy value
auto append_ll = [](FatTupleDifferentAttributes& fat_tuple, u8* delta,
u16 delta_length) {
assert(fat_tuple.total_space >=
Expand All @@ -382,10 +381,10 @@ void FatTupleDifferentAttributes::resize(const u32 new_length) {
std::memcpy(fat_tuple.payload + fat_tuple.mDataOffset, delta, delta_length);
};
for (u64 d_i = 0; d_i < deltas_count; d_i++) {
append_ll(new_fat_tuple, reinterpret_cast<u8*>(&getDelta(d_i)),
append_ll(newFatTuple, reinterpret_cast<u8*>(&getDelta(d_i)),
getDelta(d_i).totalLength());
}
std::memcpy(this, tmp_page, new_length + sizeof(FatTupleDifferentAttributes));
std::memcpy(this, tmpPage, tmpPageSize);
assert(total_space >= used_space);
}

Expand Down
52 changes: 25 additions & 27 deletions source/storage/btree/core/BTreeGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@
#include "profiling/counters/WorkerCounters.hpp"
#include "storage/buffer-manager/BufferManager.hpp"
#include "storage/buffer-manager/GuardedBufferFrame.hpp"
#include "utils/Misc.hpp"
#include "utils/RandomGenerator.hpp"

#include <glog/logging.h>

#include <alloca.h>

using namespace leanstore::storage;

namespace leanstore::storage::btree {

#define ARRAY_ON_STACK(varName, T, N) T* varName = (T*)alloca((N) * sizeof(T));

void BTreeGeneric::Init(TREEID btreeId, Config config) {
this->mTreeId = btreeId;
this->config = config;
Expand Down Expand Up @@ -82,8 +79,7 @@ void BTreeGeneric::trySplit(BufferFrame& toSplit, s16 favoredSplitPos) {
BTreeNode::SeparatorInfo{guardedChild->getFullKeyLen(favoredSplitPos),
static_cast<u16>(favoredSplitPos), false};
}
// u8 sepKey[sepInfo.length];
ARRAY_ON_STACK(sepKey, u8, sepInfo.length);
auto sepKey = utils::ArrayOnStack<u8>(sepInfo.length);
if (isMetaNode(guardedParent)) {
// split the root node
auto xGuardedParent = ExclusiveGuardedBufferFrame(std::move(guardedParent));
Expand Down Expand Up @@ -415,44 +411,45 @@ s16 BTreeGeneric::mergeLeftIntoRight(
xGuardedRight->mLowerFence.length)) <
EFFECTIVE_PAGE_SIZE * 1.0);
assert(till_slot_id > 0);
// -------------------------------------------------------------------------------------

u16 copy_from_count = xGuardedLeft->mNumSeps - till_slot_id;
// -------------------------------------------------------------------------------------
u16 new_left_uf_length = xGuardedLeft->getFullKeyLen(till_slot_id - 1);
ENSURE(new_left_uf_length > 0);
ARRAY_ON_STACK(new_left_uf_key, u8, new_left_uf_length);
xGuardedLeft->copyFullKey(till_slot_id - 1, new_left_uf_key);
// -------------------------------------------------------------------------------------
if (!xGuardedParent->prepareInsert(new_left_uf_length, 0))

u16 newLeftUpperFenceSize = xGuardedLeft->getFullKeyLen(till_slot_id - 1);
ENSURE(newLeftUpperFenceSize > 0);
auto newLeftUpperFence = utils::ArrayOnStack<u8>(newLeftUpperFenceSize);
xGuardedLeft->copyFullKey(till_slot_id - 1, newLeftUpperFence);

if (!xGuardedParent->prepareInsert(newLeftUpperFenceSize, 0)) {
return 0; // false
// -------------------------------------------------------------------------------------
}

{
BTreeNode tmp(true);
tmp.setFences(Slice(new_left_uf_key, new_left_uf_length),
tmp.setFences(Slice(newLeftUpperFence, newLeftUpperFenceSize),
xGuardedRight->GetUpperFence());
// -------------------------------------------------------------------------------------

xGuardedLeft->copyKeyValueRange(&tmp, 0, till_slot_id, copy_from_count);
xGuardedRight->copyKeyValueRange(&tmp, copy_from_count, 0,
xGuardedRight->mNumSeps);
memcpy(xGuardedRight.GetPagePayloadPtr(), &tmp, sizeof(BTreeNode));
xGuardedRight->makeHint();
// -------------------------------------------------------------------------------------

// Nothing to do for the right node's separator
assert(xGuardedRight->compareKeyWithBoundaries(
Slice(new_left_uf_key, new_left_uf_length)) == 1);
Slice(newLeftUpperFence, newLeftUpperFenceSize)) == 1);
}
{
BTreeNode tmp(true);
tmp.setFences(xGuardedLeft->GetLowerFence(),
Slice(new_left_uf_key, new_left_uf_length));
Slice(newLeftUpperFence, newLeftUpperFenceSize));
// -------------------------------------------------------------------------------------
xGuardedLeft->copyKeyValueRange(&tmp, 0, 0,
xGuardedLeft->mNumSeps - copy_from_count);
memcpy(xGuardedLeft.GetPagePayloadPtr(), &tmp, sizeof(BTreeNode));
xGuardedLeft->makeHint();
// -------------------------------------------------------------------------------------
assert(xGuardedLeft->compareKeyWithBoundaries(
Slice(new_left_uf_key, new_left_uf_length)) == 0);
Slice(newLeftUpperFence, newLeftUpperFenceSize)) == 0);
// -------------------------------------------------------------------------------------
xGuardedParent->removeSlot(lhsSlotId);
ENSURE(xGuardedParent->prepareInsert(xGuardedLeft->mUpperFence.length,
Expand Down Expand Up @@ -481,11 +478,12 @@ BTreeGeneric::XMergeReturnCode BTreeGeneric::XMerge(
s16 pos = parentHandler.mPosInParent;
u8 pages_count = 1;
s16 max_right;
ARRAY_ON_STACK(guardedNodes, GuardedBufferFrame<BTreeNode>, MAX_MERGE_PAGES);
ARRAY_ON_STACK(fully_merged, bool, MAX_MERGE_PAGES);
auto guardedNodes =
utils::ArrayOnStack<GuardedBufferFrame<BTreeNode>>(MAX_MERGE_PAGES);
auto fullyMerged = utils::ArrayOnStack<bool>(MAX_MERGE_PAGES);

guardedNodes[0] = std::move(guardedChild);
fully_merged[0] = false;
fullyMerged[0] = false;
double total_fill_factor = guardedNodes[0]->fillFactorAfterCompaction();

// Handle upper swip instead of avoiding guardedParent->mNumSeps -1 swip
Expand All @@ -503,7 +501,7 @@ BTreeGeneric::XMergeReturnCode BTreeGeneric::XMerge(

guardedNodes[max_right - pos] = GuardedBufferFrame<BTreeNode>(
guardedParent, guardedParent->getChild(max_right));
fully_merged[max_right - pos] = false;
fullyMerged[max_right - pos] = false;
total_fill_factor +=
guardedNodes[max_right - pos]->fillFactorAfterCompaction();
pages_count++;
Expand All @@ -526,7 +524,7 @@ BTreeGeneric::XMergeReturnCode BTreeGeneric::XMerge(
s16 left_hand, right_hand, ret;
while (true) {
for (right_hand = max_right; right_hand > pos; right_hand--) {
if (fully_merged[right_hand - pos]) {
if (fullyMerged[right_hand - pos]) {
continue;
} else {
break;
Expand All @@ -549,7 +547,7 @@ BTreeGeneric::XMergeReturnCode BTreeGeneric::XMerge(
xGuardedRight, left_hand == pos);
// we unlock only the left page, the right one should not be touched again
if (ret == 1) {
fully_merged[left_hand - pos] = true;
fullyMerged[left_hand - pos] = true;
WorkerCounters::myCounters().xmerge_full_counter[mTreeId]++;
ret_code = XMergeReturnCode::FULL_MERGE;
} else if (ret == 2) {
Expand Down
Loading

0 comments on commit dda0764

Please sign in to comment.