From c66d5b151f298e819decada390ac4e69e124697b Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Thu, 28 Dec 2017 19:11:04 +0100 Subject: Avoid using temporaries. --- .../eval/tensor/sparse/direct_sparse_tensor_builder.h | 17 ++++++----------- .../eval/tensor/sparse/sparse_tensor_address_ref.h | 13 +++++++------ 2 files changed, 13 insertions(+), 17 deletions(-) (limited to 'eval') diff --git a/eval/src/vespa/eval/tensor/sparse/direct_sparse_tensor_builder.h b/eval/src/vespa/eval/tensor/sparse/direct_sparse_tensor_builder.h index c977131fcd3..9ec98b2c11d 100644 --- a/eval/src/vespa/eval/tensor/sparse/direct_sparse_tensor_builder.h +++ b/eval/src/vespa/eval/tensor/sparse/direct_sparse_tensor_builder.h @@ -40,8 +40,7 @@ public: void copyCells(const Cells &cells_in, const eval::ValueType &cells_in_type) { - SparseTensorAddressPadder addressPadder(_type, - cells_in_type); + SparseTensorAddressPadder addressPadder(_type, cells_in_type); for (const auto &cell : cells_in) { addressPadder.padAddress(cell.first); SparseTensorAddressRef oldRef = addressPadder.getAddressRef(); @@ -64,8 +63,7 @@ public: { } - DirectTensorBuilder(const eval::ValueType &type_in, - const Cells &cells_in) + DirectTensorBuilder(const eval::ValueType &type_in, const Cells &cells_in) : _stash(TensorImplType::STASH_CHUNK_SIZE), _type(type_in), _cells() @@ -94,14 +92,12 @@ public: } template - void insertCell(SparseTensorAddressRef address, double value, - Function &&func) + void insertCell(SparseTensorAddressRef address, double value, Function &&func) { - SparseTensorAddressRef oldRef(address); - auto res = _cells.insert(std::make_pair(oldRef, value)); + auto res = _cells.insert(std::make_pair(address, value)); if (res.second) { // Replace key with own copy - res.first->first = SparseTensorAddressRef(oldRef, _stash); + res.first->first = SparseTensorAddressRef(address, _stash); } else { res.first->second = func(res.first->second, value); } @@ -113,8 +109,7 @@ public: } template - void insertCell(SparseTensorAddressBuilder &address, double value, - Function &&func) + void insertCell(SparseTensorAddressBuilder &address, double value, Function &&func) { insertCell(address.getAddressRef(), value, func); } diff --git a/eval/src/vespa/eval/tensor/sparse/sparse_tensor_address_ref.h b/eval/src/vespa/eval/tensor/sparse/sparse_tensor_address_ref.h index b179b431b94..8533aa29829 100644 --- a/eval/src/vespa/eval/tensor/sparse/sparse_tensor_address_ref.h +++ b/eval/src/vespa/eval/tensor/sparse/sparse_tensor_address_ref.h @@ -16,6 +16,11 @@ class SparseTensorAddressRef const void *_start; uint32_t _size; uint32_t _hash; + static void * copy(const SparseTensorAddressRef rhs, Stash &stash) { + void *res = stash.alloc(rhs._size); + memcpy(res, rhs._start, rhs._size); + return res; + } public: SparseTensorAddressRef() : _start(nullptr), _size(0u), _hash(0u) @@ -29,14 +34,10 @@ public: } SparseTensorAddressRef(const SparseTensorAddressRef rhs, Stash &stash) - : _start(nullptr), + : _start(copy(rhs, stash)), _size(rhs._size), _hash(rhs._hash) - { - void *res = stash.alloc(rhs._size); - memcpy(res, rhs._start, rhs._size); - _start = res; - } + {} uint32_t hash() const { return _hash; } -- cgit v1.2.3