Skip to content

Commit

Permalink
avoid use of D std for vector copies
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaTheFoxgirl committed Sep 17, 2024
1 parent b018a8d commit 4be7776
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
3 changes: 3 additions & 0 deletions source/numem/mem/internal/btree.d
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ private:
oldRoot.numKeys = -1;
oldRoot.children[0] = null; // so that it is not destroyed
nogc_delete(oldRoot); // <- here
oldRoot = null;
}
}
return;
Expand Down Expand Up @@ -536,6 +537,7 @@ private:
rebalanceAfterDeletion(parent);

nogc_delete(right);
right = null;
}

// internal use, delete a kv and shift remaining kv
Expand Down Expand Up @@ -633,6 +635,7 @@ private:

void deallocateNode(Node* node) {
nogc_delete(node);
node = null;
}

static struct Node {
Expand Down
6 changes: 3 additions & 3 deletions source/numem/mem/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ void nogc_delete(T)(ref T obj_) {


// With a normal runtime we can use destroy
static if (is(T == struct) || is(T == union)) {
static if (is(PointerTarget!T == struct) || is(PointerTarget!T == union)) {
auto objptr_ = &obj_;

if (objptr_) {
Expand Down Expand Up @@ -284,8 +284,8 @@ void nogc_delete(T)(ref T obj_) {

// Free memory
static if(isPointer!T) {
if (cast(void*)obj_) {
free(cast(void*)obj_);
if (cast(void*)*obj_) {
free(cast(void*)*obj_);
obj_ = null;
}
}
Expand Down
18 changes: 13 additions & 5 deletions source/numem/mem/vector.d
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,16 @@ private:
for (size_t n = before; n < capacity_; ++n)
{
T tmp = T.init;
memcpy(memory + n, &tmp, T.sizeof);
_memcpy(memory + n, &tmp, 1);
}
}
}

pragma(inline, true)
void _memcpy(T* dst, T* src, size_t length) {
memcpy(dst, src, T.sizeof*length);
}

public:

/// Gets the type of character stored in the string.
Expand Down Expand Up @@ -103,7 +108,7 @@ public:
@trusted
this(T[] data) {
this.resize_(data.length);
this.memory[0..data.length] = data[0..$];
this._memcpy(this.memory, data.ptr, data.length);
}

static if (!isCopyable!T && __traits(hasMember, T, "moveTo")) {
Expand Down Expand Up @@ -133,7 +138,7 @@ public:
this(ref vector!T rhs) {
if (rhs.memory) {
this.resize_(rhs.size_);
this.memory[0..size_] = rhs.memory[0..rhs.size_];
this._memcpy(this.memory, rhs.memory, rhs.size_);
}
}

Expand All @@ -143,8 +148,11 @@ public:
@trusted
this(ref return scope inout(vector!T) rhs) inout {
if (rhs.memory) {
(cast(vector!T)this).resize_(rhs.size_);
(cast(vector!T)this).memory[0..size_] = (cast(vector!T)rhs).memory[0..rhs.size_];
auto self = (cast(vector!T)this);
auto other = (cast(vector!T)rhs);

self.resize_(rhs.size_);
other._memcpy(self.memory, other.memory, other.size_);
}
}
}
Expand Down

0 comments on commit 4be7776

Please sign in to comment.