Skip to content

Commit

Permalink
Improve knn search performance (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
tzaeschke authored Mar 8, 2023
1 parent 19a5da2 commit 731a21e
Show file tree
Hide file tree
Showing 25 changed files with 1,085 additions and 101 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Changed `bpt_vectot` to use `std::destroy` i.o. default dstr. [#132](https://github.com/tzaeschke/phtree-cpp/pull/132)
- Moved B+trees into own namespace. [#131](https://github.com/tzaeschke/phtree-cpp/pull/131)
- Moved some stuff in `common` into `nemaspace detail`. [#129](https://github.com/tzaeschke/phtree-cpp/issues/129)
- Improved kNN search implementation. This also deprecates the post-increment iterator.
[#118](https://github.com/tzaeschke/phtree-cpp/issues/118)

### Fixed
- Replaced deprecated `<assert.h>` imports with `<cassert>`. [#134](https://github.com/tzaeschke/phtree-cpp/pull/134)
Expand Down
15 changes: 15 additions & 0 deletions benchmark/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,21 @@ cc_binary(
],
)

cc_binary(
name = "knn_mm_d_benchmark",
testonly = True,
srcs = [
"knn_mm_d_benchmark.cc",
],
linkstatic = True,
deps = [
":benchmark",
"//:phtree",
"@gbenchmark//:benchmark",
"@spdlog",
],
)

cc_binary(
name = "query_benchmark",
testonly = True,
Expand Down
20 changes: 18 additions & 2 deletions benchmark/bpt_insert_benchmark.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2023 Tilmann Zäschke
* Copyright 2022-2023 Tilmann Zäschke
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,6 +30,7 @@ const int GLOBAL_MAX = 10000;
enum Scenario {
MAP,
MULTIMAP,
MULTIMAP2,
HASH_MAP,
STD_MAP,
STD_MULTIMAP,
Expand Down Expand Up @@ -118,7 +119,22 @@ void IndexBenchmark<DIM, TYPE>::SetupWorld(benchmark::State& state) {
template <size_t DIM, Scenario TYPE>
void IndexBenchmark<DIM, TYPE>::Insert(benchmark::State& state, Index& tree) {
switch (TYPE) {
default: {
case MAP: {
for (size_t i = 0; i < num_entities_; ++i) {
tree.emplace(points_[i][0], (payload_t)i);
}
break;
}
case MULTIMAP:
case MULTIMAP2:
case STD_MULTIMAP: {
for (size_t i = 0; i < num_entities_; ++i) {
tree.emplace(points_[i][0], (payload_t)i);
}
break;
}
case HASH_MAP:
case STD_MAP: {
for (size_t i = 0; i < num_entities_; ++i) {
tree.emplace(points_[i][0], (payload_t)i);
}
Expand Down
2 changes: 1 addition & 1 deletion benchmark/hd_erase_d_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ using payload_t = std::uint32_t;
template <dimension_t DIM>
class IndexBenchmark {
public:
IndexBenchmark(benchmark::State& state);
explicit IndexBenchmark(benchmark::State& state);
void Benchmark(benchmark::State& state);

private:
Expand Down
16 changes: 12 additions & 4 deletions benchmark/knn_d_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ void IndexBenchmark<DIM>::SetupWorld(benchmark::State& state) {
tree_.emplace(points_[i], (int)i);
}

state.counters["total_result_count"] = benchmark::Counter(0);
state.counters["total_query_count"] = benchmark::Counter(0);
state.counters["query_rate"] = benchmark::Counter(0, benchmark::Counter::kIsRate);
state.counters["result_rate"] = benchmark::Counter(0, benchmark::Counter::kIsRate);
state.counters["avg_result_count"] = benchmark::Counter(0, benchmark::Counter::kAvgIterations);
Expand All @@ -104,8 +102,6 @@ void IndexBenchmark<DIM>::QueryWorld(benchmark::State& state, PhPointD<DIM>& cen
++n;
}

state.counters["total_query_count"] += 1;
state.counters["total_result_count"] += n;
state.counters["query_rate"] += 1;
state.counters["result_rate"] += n;
state.counters["avg_result_count"] += n;
Expand Down Expand Up @@ -140,6 +136,12 @@ BENCHMARK_CAPTURE(PhTree3D, KNN_CU_10_of_10K, TestGenerator::CUBE, 10000, 10)
BENCHMARK_CAPTURE(PhTree3D, KNN_CU_10_of_1M, TestGenerator::CUBE, 1000000, 10)
->Unit(benchmark::kMillisecond);

BENCHMARK_CAPTURE(PhTree3D, KNN_CU_100_of_10K, TestGenerator::CUBE, 10000, 100)
->Unit(benchmark::kMillisecond);

BENCHMARK_CAPTURE(PhTree3D, KNN_CU_100_of_1M, TestGenerator::CUBE, 1000000, 100)
->Unit(benchmark::kMillisecond);

// index type, scenario name, data_type, num_entities, query_result_size
// PhTree 3D CLUSTER
BENCHMARK_CAPTURE(PhTree3D, KNN_CL_1_of_10K, TestGenerator::CLUSTER, 10000, 1)
Expand All @@ -154,4 +156,10 @@ BENCHMARK_CAPTURE(PhTree3D, KNN_CL_10_of_10K, TestGenerator::CLUSTER, 10000, 10)
BENCHMARK_CAPTURE(PhTree3D, KNN_CL_10_of_1M, TestGenerator::CLUSTER, 1000000, 10)
->Unit(benchmark::kMillisecond);

BENCHMARK_CAPTURE(PhTree3D, KNN_CL_100_of_10K, TestGenerator::CLUSTER, 10000, 100)
->Unit(benchmark::kMillisecond);

BENCHMARK_CAPTURE(PhTree3D, KNN_CL_100_of_1M, TestGenerator::CLUSTER, 1000000, 100)
->Unit(benchmark::kMillisecond);

BENCHMARK_MAIN();
Loading

0 comments on commit 731a21e

Please sign in to comment.