File: SpatialIndexPerfTest.cpp

package info (click to toggle)
geos 3.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,212 kB
  • sloc: cpp: 199,103; xml: 56,065; ansic: 6,162; sh: 287; makefile: 26
file content (361 lines) | stat: -rw-r--r-- 10,645 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**********************************************************************
 *
 * GEOS - Geometry Engine Open Source
 * http://geos.osgeo.org
 *
 * Copyright (C) 2021 Daniel Baston
 *
 * This is free software; you can redistribute and/or modify it under
 * the terms of the GNU Lesser General Public Licence as published
 * by the Free Software Foundation.
 * See the COPYING file for more information.
 *
 **********************************************************************/

#include <random>

#include <benchmark/benchmark.h>

#include <geos/index/strtree/STRtree.h>
#include <geos/index/strtree/SimpleSTRtree.h>
#include <geos/index/strtree/TemplateSTRtree.h>
#include <geos/index/quadtree/Quadtree.h>
#include <geos/index/intervalrtree/SortedPackedIntervalRTree.h>

using geos::geom::CoordinateXY;
using geos::geom::Envelope;
using geos::index::intervalrtree::SortedPackedIntervalRTree;
using geos::index::quadtree::Quadtree;
using geos::index::strtree::STRtree;
using geos::index::strtree::SimpleSTRtree;
using geos::index::strtree::TemplateSTRtree;
using geos::index::strtree::Interval;
using geos::index::strtree::ItemDistance;
using geos::index::strtree::ItemBoundable;

using TemplateIntervalTree = TemplateSTRtree<const Interval*, geos::index::strtree::IntervalTraits>;

//////////////////////////
// Test Data Generation //
//////////////////////////

static std::vector<Envelope> generate_envelopes(std::default_random_engine & e,
                                                const Envelope& extent,
                                                std::size_t n) {
    std::uniform_real_distribution<> centroid_x(extent.getMinX(), extent.getMaxX());
    std::uniform_real_distribution<> centroid_y(extent.getMinY(), extent.getMaxY());

    // distributions of width and aspect ratio fit from HydroBASINS level 7 (Africa)
    std::weibull_distribution<> size_x(1.606, 0.00989);
    std::lognormal_distribution<> y_rat(-0.027, 0.4884);

    std::vector<Envelope> envelopes(n);
    for (std::size_t i = 0; i < n; i++) {
        double cx = centroid_x(e);
        double cy = centroid_y(e);

        double width = size_x(e) * extent.getWidth();
        double height = width * y_rat(e);

        envelopes[i] = Envelope(cx - width / 2, cx + width / 2,
                               cy - height / 2, cy + height / 2);

    }

    return envelopes;
}

std::vector<CoordinateXY> generate_uniform_points(std::default_random_engine& eng,
                                                const Envelope& box,
                                                std::size_t n) {
    std::vector<CoordinateXY> pts(n);

    std::uniform_real_distribution<> qx(box.getMinX(), box.getMaxX());
    std::uniform_real_distribution<> qy(box.getMinY(), box.getMaxY());

    for (std::size_t i = 0; i < n; i++) {
        pts[i] = { qx(eng), qy(eng) };
    }

    return pts;
}

//////////////
// Visitors //
//////////////

class CountingVisitor : public geos::index::ItemVisitor {
    size_t hits = 0;
    void visitItem(void* item) override {
        hits += (item != nullptr);
    }
};

template<typename T>
struct Counter {
    size_t hits = 0;

    void operator()(const T& item) {
        (void) item;
        hits++;
    }

    void operator()(const T& item1, const T& item2) {
        (void) item1;
        (void) item2;
        hits++;
    }
};

struct EnvelopeDistance : public ItemDistance {
    double distance(const ItemBoundable* a, const ItemBoundable* b) override {
        Envelope* ea = static_cast<Envelope*>(a->getItem());
        Envelope* eb = static_cast<Envelope*>(b->getItem());

        return ea->distance(*eb);
    }

    double operator()(const Envelope* a, const Envelope* b) {
        return a->distance(*b);
    }
};

/////////////////
// 1D adapters //
/////////////////

static inline void insert(TemplateIntervalTree & tree, const Interval& i) {
    tree.insert(i, &i);
}

static inline void insert(SortedPackedIntervalRTree & tree, const Interval& i) {
    tree.insert(i.getMin(), i.getMax(), (void*) &i);
}

static inline void query(TemplateIntervalTree & tree, const Interval& i) {
    size_t hits = 0;
    tree.query(i, [&hits](const Interval* item) {
        hits += (item != nullptr);
    });
}

static inline void query(SortedPackedIntervalRTree & tree, const Interval& i) {
    CountingVisitor cv;
    tree.query(i.getMin(), i.getMax(), &cv);
}

/////////////////
// 2D adapters //
/////////////////

template<class Tree>
static inline const void* nearestNeighbour(Tree & tree, const Envelope & e) {
    EnvelopeDistance dist;
    return tree.nearestNeighbour(e, &e, dist);
}

static inline const void* nearestNeighbour(SimpleSTRtree & tree, const Envelope & e) {
    EnvelopeDistance dist;
    return tree.nearestNeighbour(&e, &e, &dist);
}

static inline const void* nearestNeighbour(STRtree & tree, const Envelope & e) {
    EnvelopeDistance dist;
    return tree.nearestNeighbour(&e, &e, &dist);
}

///////////////////
// 1D benchmarks //
///////////////////

template<class Tree>
static void BM_STRtree1DConstruct(benchmark::State& state) {
    std::default_random_engine eng(12345);
    Envelope extent(0, 1, 0, 1);
    auto envelopes = generate_envelopes(eng, extent, 10000);
    std::vector<Interval> intervals;
    intervals.reserve(envelopes.size());

    for (const auto& e : envelopes) {
        intervals.emplace_back(e.getMinY(), e.getMaxY());
    }

    Interval outside_extent(extent.getMaxY() + 100, extent.getMaxY() + 101);

    for (auto _ : state) {
        Tree tree(10);

        for (const auto& interval : intervals) {
            insert(tree, interval);
        }
        query(tree, outside_extent);
    }
}

template<class Tree>
static void BM_STRtree1DQuery(benchmark::State& state) {
    std::default_random_engine eng(12345);
    Envelope extent(0, 1, 0, 1);
    auto envelopes = generate_envelopes(eng, extent, 10000);
    std::vector<Interval> intervals;
    intervals.reserve(envelopes.size());

    for (const auto& e : envelopes) {
        intervals.emplace_back(e.getMinY(), e.getMaxY());
    }

    Tree tree(10);

    for (const auto& interval : intervals) {
        insert(tree, interval);
    }
    Interval outside_extent(extent.getMaxY() + 100, extent.getMaxY() + 101);
    query(tree, outside_extent); // force construction

    for (auto _ : state) {
        for (auto& i : intervals) {
            query(tree, i);
        }
    }
}

///////////////////
// 2D benchmarks //
///////////////////

template<class Tree>
static void BM_STRtree2DConstruct(benchmark::State& state) {
    std::default_random_engine eng(12345);
    Envelope extent(0, 1, 0, 1);
    auto envelopes = generate_envelopes(eng, extent, 10000);
    Envelope empty_env;

    std::vector<void*> hits;

    for (auto _ : state) {
        Tree tree;
        for (auto& e : envelopes) {
            tree.insert(&e, &e);
        }
        tree.query(&empty_env, hits); // query with empty envelope to force construction
    }
}

template<class Tree>
static void BM_STRtree2DQuery(benchmark::State& state) {
    std::default_random_engine eng(12345);
    Envelope extent(0, 1, 0, 1);
    auto envelopes = generate_envelopes(eng, extent, 10000);
    Envelope empty_env;

    std::vector<void*> hits;

    Tree tree;
    for (auto& e : envelopes) {
        tree.insert(&e, &e);
    }
    tree.query(&empty_env, hits); // query with empty envelope to force construction

    for (auto _ : state) {
        hits.clear();
        for (auto& e : envelopes) {
            tree.query(&e, hits);
        }
    }
}

static void BM_STRtree2DQueryPairs(benchmark::State& state) {
    std::default_random_engine eng(12345);
    Envelope extent(0, 1, 0, 1);
    auto envelopes = generate_envelopes(eng, extent, 10000);
    Envelope empty_env;

    TemplateSTRtree<const Envelope*> tree;
    for (auto& e : envelopes) {
        tree.insert(&e, &e);
    }
    Counter<const Envelope*> q;
    tree.query(empty_env, q); // query with empty envelope to force construction

    for (auto _ : state) {
        Counter<const Envelope*> c;
        tree.queryPairs(c);
    }
}

static void BM_STRtree2DQueryPairsNaive(benchmark::State& state) {
    std::default_random_engine eng(12345);
    Envelope extent(0, 1, 0, 1);
    auto envelopes = generate_envelopes(eng, extent, 10000);
    Envelope empty_env;

    TemplateSTRtree<const Envelope*> tree;
    for (auto& e : envelopes) {
        tree.insert(&e, &e);
    }
    Counter<const Envelope*> q;
    tree.query(empty_env, q); // query with empty envelope to force construction

    for (auto _ : state) {
        Counter<const Envelope*> c;
        for (const auto& env : envelopes) {
            tree.query(env, c);
        }
    }
}

template<class Tree>
static void BM_STRtree2DNearest(benchmark::State& state) {
    std::default_random_engine eng(12345);
    Envelope extent(0, 1, 0, 1);
    auto envelopes = generate_envelopes(eng, extent, 10000);
    Envelope empty_env;

    Envelope queryEnv = extent;
    queryEnv.expandBy(0.25 * queryEnv.getWidth(), 0.25 * queryEnv.getHeight());
    auto queryPoints = generate_uniform_points(eng, queryEnv, 10000);

    std::vector<Envelope> queryEnvelopes(queryPoints.size());
    for (std::size_t i = 0; i < queryEnvelopes.size(); i++) {
        queryEnvelopes[i] = Envelope(queryPoints[i]);
    }

    std::vector<void*> hits;

    Tree tree;
    for (auto& e : envelopes) {
        tree.insert(&e, &e);
    }
    tree.query(&empty_env, hits); // query with empty envelope to force construction

    for (auto _ : state) {
        for (auto& e : queryEnvelopes) {
            nearestNeighbour(tree, e);
        }
    }
}

BENCHMARK_TEMPLATE(BM_STRtree1DConstruct, SortedPackedIntervalRTree);
BENCHMARK_TEMPLATE(BM_STRtree1DConstruct, TemplateIntervalTree);
BENCHMARK_TEMPLATE(BM_STRtree1DQuery, SortedPackedIntervalRTree);
BENCHMARK_TEMPLATE(BM_STRtree1DQuery, TemplateIntervalTree);

BENCHMARK_TEMPLATE(BM_STRtree2DConstruct, Quadtree);
BENCHMARK_TEMPLATE(BM_STRtree2DConstruct, STRtree);
BENCHMARK_TEMPLATE(BM_STRtree2DConstruct, SimpleSTRtree);
BENCHMARK_TEMPLATE(BM_STRtree2DConstruct, TemplateSTRtree<const Envelope*>);

BENCHMARK_TEMPLATE(BM_STRtree2DNearest, STRtree);
BENCHMARK_TEMPLATE(BM_STRtree2DNearest, SimpleSTRtree);
BENCHMARK_TEMPLATE(BM_STRtree2DNearest, TemplateSTRtree<const Envelope*>);

BENCHMARK_TEMPLATE(BM_STRtree2DQuery, Quadtree);
BENCHMARK_TEMPLATE(BM_STRtree2DQuery, STRtree);
BENCHMARK_TEMPLATE(BM_STRtree2DQuery, SimpleSTRtree);
BENCHMARK_TEMPLATE(BM_STRtree2DQuery, TemplateSTRtree<const Envelope*>);

BENCHMARK(BM_STRtree2DQueryPairs);
BENCHMARK(BM_STRtree2DQueryPairsNaive);

BENCHMARK_MAIN();