summaryrefslogtreecommitdiffstats
path: root/eval
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2020-03-18 22:09:00 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2020-03-18 22:09:00 +0000
commit94579ec02f1b9cf90a2ef40dcb9d66ce8dce85e2 (patch)
tree73f127aa97e76b351d693fcb5816690e465f1f9c /eval
parent3a785aa5363cb143a04988b39975f33431a54bf4 (diff)
Use vespalib::hash_set instead of std::set to reduce number of allocation and epeed it up. Also use faster 2^N AND based hash tables.
Diffstat (limited to 'eval')
-rw-r--r--eval/src/vespa/eval/tensor/sparse/CMakeLists.txt1
-rw-r--r--eval/src/vespa/eval/tensor/sparse/direct_sparse_tensor_builder.cpp62
-rw-r--r--eval/src/vespa/eval/tensor/sparse/direct_sparse_tensor_builder.h54
-rw-r--r--eval/src/vespa/eval/tensor/sparse/sparse_tensor.cpp3
-rw-r--r--eval/src/vespa/eval/tensor/sparse/sparse_tensor.h2
5 files changed, 74 insertions, 48 deletions
diff --git a/eval/src/vespa/eval/tensor/sparse/CMakeLists.txt b/eval/src/vespa/eval/tensor/sparse/CMakeLists.txt
index 8e1d18a87b7..a25d2abb477 100644
--- a/eval/src/vespa/eval/tensor/sparse/CMakeLists.txt
+++ b/eval/src/vespa/eval/tensor/sparse/CMakeLists.txt
@@ -1,6 +1,7 @@
# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
vespa_add_library(eval_tensor_sparse OBJECT
SOURCES
+ direct_sparse_tensor_builder.cpp
sparse_tensor.cpp
sparse_tensor_add.cpp
sparse_tensor_address_builder.cpp
diff --git a/eval/src/vespa/eval/tensor/sparse/direct_sparse_tensor_builder.cpp b/eval/src/vespa/eval/tensor/sparse/direct_sparse_tensor_builder.cpp
new file mode 100644
index 00000000000..4a28b54d201
--- /dev/null
+++ b/eval/src/vespa/eval/tensor/sparse/direct_sparse_tensor_builder.cpp
@@ -0,0 +1,62 @@
+// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include "direct_sparse_tensor_builder.h"
+
+namespace vespalib::tensor {
+
+void
+DirectSparseTensorBuilder::copyCells(const Cells &cells_in)
+{
+ for (const auto &cell : cells_in) {
+ SparseTensorAddressRef oldRef = cell.first;
+ SparseTensorAddressRef newRef(oldRef, _stash);
+ _cells[newRef] = cell.second;
+ }
+}
+
+DirectSparseTensorBuilder::DirectSparseTensorBuilder()
+ : _stash(SparseTensor::STASH_CHUNK_SIZE),
+ _type(eval::ValueType::double_type()),
+ _cells()
+{
+}
+
+DirectSparseTensorBuilder::DirectSparseTensorBuilder(const eval::ValueType &type_in)
+ : _stash(SparseTensor::STASH_CHUNK_SIZE),
+ _type(type_in),
+ _cells()
+{
+}
+
+DirectSparseTensorBuilder::DirectSparseTensorBuilder(const eval::ValueType &type_in, const Cells &cells_in)
+ : _stash(SparseTensor::STASH_CHUNK_SIZE),
+ _type(type_in),
+ _cells()
+{
+ copyCells(cells_in);
+}
+
+DirectSparseTensorBuilder::~DirectSparseTensorBuilder() = default;
+
+Tensor::UP
+DirectSparseTensorBuilder::build() {
+ return std::make_unique<SparseTensor>(std::move(_type), std::move(_cells), std::move(_stash));
+}
+
+void
+DirectSparseTensorBuilder::insertCell(SparseTensorAddressRef address, double value) {
+ // This address should not already exist and a new cell should be inserted.
+ insertCell(address, value, [](double, double) -> double { HDR_ABORT("should not be reached"); });
+}
+
+void
+DirectSparseTensorBuilder::insertCell(SparseTensorAddressBuilder &address, double value) {
+ // This address should not already exist and a new cell should be inserted.
+ insertCell(address.getAddressRef(), value, [](double, double) -> double { HDR_ABORT("should not be reached"); });
+}
+
+void DirectSparseTensorBuilder::reserve(uint32_t estimatedCells) {
+ _cells.resize(estimatedCells*2);
+}
+
+} \ No newline at end of file
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 d54c8810a81..f9182a199be 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
@@ -25,43 +25,13 @@ private:
Cells _cells;
public:
- void
- copyCells(const Cells &cells_in)
- {
- for (const auto &cell : cells_in) {
- SparseTensorAddressRef oldRef = cell.first;
- SparseTensorAddressRef newRef(oldRef, _stash);
- _cells[newRef] = cell.second;
- }
- }
-
- DirectSparseTensorBuilder()
- : _stash(SparseTensor::STASH_CHUNK_SIZE),
- _type(eval::ValueType::double_type()),
- _cells()
- {
- }
-
- DirectSparseTensorBuilder(const eval::ValueType &type_in)
- : _stash(SparseTensor::STASH_CHUNK_SIZE),
- _type(type_in),
- _cells()
- {
- }
-
- DirectSparseTensorBuilder(const eval::ValueType &type_in, const Cells &cells_in)
- : _stash(SparseTensor::STASH_CHUNK_SIZE),
- _type(type_in),
- _cells()
- {
- copyCells(cells_in);
- }
+ void copyCells(const Cells &cells_in);
+ DirectSparseTensorBuilder();
+ DirectSparseTensorBuilder(const eval::ValueType &type_in);
+ DirectSparseTensorBuilder(const eval::ValueType &type_in, const Cells &cells_in);
+ ~DirectSparseTensorBuilder();
- ~DirectSparseTensorBuilder() {};
-
- Tensor::UP build() {
- return std::make_unique<SparseTensor>(std::move(_type), std::move(_cells), std::move(_stash));
- }
+ Tensor::UP build();
template <class Function>
void insertCell(SparseTensorAddressRef address, double value, Function &&func)
@@ -75,10 +45,7 @@ public:
}
}
- void insertCell(SparseTensorAddressRef address, double value) {
- // This address should not already exist and a new cell should be inserted.
- insertCell(address, value, [](double, double) -> double { HDR_ABORT("should not be reached"); });
- }
+ void insertCell(SparseTensorAddressRef address, double value);
template <class Function>
void insertCell(SparseTensorAddressBuilder &address, double value, Function &&func)
@@ -86,14 +53,11 @@ public:
insertCell(address.getAddressRef(), value, func);
}
- void insertCell(SparseTensorAddressBuilder &address, double value) {
- // This address should not already exist and a new cell should be inserted.
- insertCell(address.getAddressRef(), value, [](double, double) -> double { HDR_ABORT("should not be reached"); });
- }
+ void insertCell(SparseTensorAddressBuilder &address, double value);
eval::ValueType &fast_type() { return _type; }
Cells &cells() { return _cells; }
- void reserve(uint32_t estimatedCells) { _cells.resize(estimatedCells*2); }
+ void reserve(uint32_t estimatedCells);
};
}
diff --git a/eval/src/vespa/eval/tensor/sparse/sparse_tensor.cpp b/eval/src/vespa/eval/tensor/sparse/sparse_tensor.cpp
index 1fc93e8234f..d183c33f5cd 100644
--- a/eval/src/vespa/eval/tensor/sparse/sparse_tensor.cpp
+++ b/eval/src/vespa/eval/tensor/sparse/sparse_tensor.cpp
@@ -245,5 +245,4 @@ SparseTensor::remove(const CellValues &cellAddresses) const
}
-VESPALIB_HASH_MAP_INSTANTIATE_H_E_M(vespalib::tensor::SparseTensorAddressRef, double, vespalib::hash<vespalib::tensor::SparseTensorAddressRef>,
- std::equal_to<vespalib::tensor::SparseTensorAddressRef>, vespalib::hashtable_base::and_modulator);
+VESPALIB_HASH_MAP_INSTANTIATE(vespalib::tensor::SparseTensorAddressRef, double);
diff --git a/eval/src/vespa/eval/tensor/sparse/sparse_tensor.h b/eval/src/vespa/eval/tensor/sparse/sparse_tensor.h
index 880cd32c605..e5ea639b460 100644
--- a/eval/src/vespa/eval/tensor/sparse/sparse_tensor.h
+++ b/eval/src/vespa/eval/tensor/sparse/sparse_tensor.h
@@ -22,7 +22,7 @@ class SparseTensor : public Tensor
{
public:
using Cells = hash_map<SparseTensorAddressRef, double, hash<SparseTensorAddressRef>,
- std::equal_to<SparseTensorAddressRef>, hashtable_base::and_modulator>;
+ std::equal_to<>, hashtable_base::and_modulator>;
static constexpr size_t STASH_CHUNK_SIZE = 16384u;