From f1efda5c9f0b8e83918d77e6b4c78b3b8e884589 Mon Sep 17 00:00:00 2001 From: Luna Date: Tue, 17 Sep 2024 21:28:58 +0200 Subject: [PATCH] avoid use of D std for vector copies --- source/numem/mem/internal/btree.d | 3 +++ source/numem/mem/package.d | 2 +- source/numem/mem/vector.d | 18 +++++++++++++----- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/source/numem/mem/internal/btree.d b/source/numem/mem/internal/btree.d index d2240b1..ea9b044 100644 --- a/source/numem/mem/internal/btree.d +++ b/source/numem/mem/internal/btree.d @@ -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; @@ -536,6 +537,7 @@ private: rebalanceAfterDeletion(parent); nogc_delete(right); + right = null; } // internal use, delete a kv and shift remaining kv @@ -633,6 +635,7 @@ private: void deallocateNode(Node* node) { nogc_delete(node); + node = null; } static struct Node { diff --git a/source/numem/mem/package.d b/source/numem/mem/package.d index 25629b5..dfae947 100644 --- a/source/numem/mem/package.d +++ b/source/numem/mem/package.d @@ -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_) { diff --git a/source/numem/mem/vector.d b/source/numem/mem/vector.d index 66a61a5..e1b1aa1 100644 --- a/source/numem/mem/vector.d +++ b/source/numem/mem/vector.d @@ -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. @@ -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")) { @@ -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_); } } @@ -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_); } } }