Skip to content

Commit

Permalink
Add benchmark for float envelope tree
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaston committed Dec 7, 2022
1 parent acd20e8 commit f8b9881
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
6 changes: 5 additions & 1 deletion benchmarks/ClassSizes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,15 @@ main()

check(index::strtree::Interval);
check(index::strtree::FloatInterval);

// Templates don't work with the "check" macro. Workaround by defining an alias.
using TreeNodePtrFloatInterval = index::strtree::TemplateSTRNode<void*,index::strtree::IntervalTraits<float>>;
using TreeNodePtrDoubleInterval = index::strtree::TemplateSTRNode<void*,index::strtree::IntervalTraits<double>>;
using TreeNodePtrDoubleEnvelope = index::strtree::TemplateSTRNode<void*,index::strtree::EnvelopeTraits>;
using TreeNodePtrFloatEnvelope = index::strtree::TemplateSTRNode<void*,index::strtree::EnvelopeTraits<float>>;
using TreeNodePtrDoubleEnvelope = index::strtree::TemplateSTRNode<void*,index::strtree::EnvelopeTraits<double>>;
check(TreeNodePtrFloatInterval);
check(TreeNodePtrDoubleInterval);
check(TreeNodePtrFloatEnvelope);
check(TreeNodePtrDoubleEnvelope);

}
Expand Down
26 changes: 25 additions & 1 deletion benchmarks/index/SpatialIndexPerfTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ using geos::index::strtree::Interval;
using geos::index::strtree::ItemDistance;
using geos::index::strtree::ItemBoundable;

using TemplateIntervalTree = TemplateSTRtree<const Interval*, geos::index::strtree::IntervalTraits>;
using TemplateIntervalTree = TemplateSTRtree<const Interval*, geos::index::strtree::IntervalTraits<double>>;
using TemplateFloatEnvelopeTree = TemplateSTRtree<const Envelope*, geos::index::strtree::EnvelopeTraits<float>>;

//////////////////////////
// Test Data Generation //
Expand Down Expand Up @@ -248,6 +249,29 @@ static void BM_STRtree2DQuery(benchmark::State& state) {
}
}

template<>
void BM_STRtree2DQuery<TemplateFloatEnvelopeTree>(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<const Envelope*> hits;

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

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

template<class Tree>
static void BM_STRtree2DNearest(benchmark::State& state) {
std::default_random_engine eng(12345);
Expand Down
34 changes: 29 additions & 5 deletions include/geos/geom/Envelope.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class GEOS_DLL EnvelopeBase {
* @param y1 the first y-value
* @param y2 the second y-value
*/
void init(double x1, double x2, double y1, double y2)
void init(T x1, T x2, T y1, T y2)
{
if(x1 < x2) {
minx = x1;
Expand Down Expand Up @@ -206,7 +206,7 @@ class GEOS_DLL EnvelopeBase {
* Returns the Envelope maximum y-value. `min y > max y` indicates
* that this is a null Envelope.
*/
double getMaxY() const
T getMaxY() const
{
assert(!isNull());
return maxy;
Expand All @@ -216,7 +216,7 @@ class GEOS_DLL EnvelopeBase {
* Returns the Envelope maximum x-value. `min x > max x` indicates
* that this is a null Envelope.
*/
double getMaxX() const
T getMaxX() const
{
assert(!isNull());
return maxx;
Expand All @@ -226,7 +226,7 @@ class GEOS_DLL EnvelopeBase {
* Returns the Envelope minimum y-value. `min y > max y` indicates
* that this is a null Envelope.
*/
double getMinY() const
T getMinY() const
{
assert(!isNull());
return miny;
Expand All @@ -236,7 +236,7 @@ class GEOS_DLL EnvelopeBase {
* Returns the Envelope minimum x-value. `min x > max x` indicates
* that this is a null Envelope.
*/
double getMinX() const
T getMinX() const
{
assert(!isNull());
return minx;
Expand Down Expand Up @@ -874,6 +874,30 @@ class GEOS_DLL Envelope : public EnvelopeBase<double> {

class GEOS_DLL FloatEnvelope : public EnvelopeBase<float> {
using EnvelopeBase<float>::EnvelopeBase;

public:

FloatEnvelope(const EnvelopeBase<double>& e)
{
minx = static_cast<float>(e.getMinX());
maxx = static_cast<float>(e.getMaxX());
miny = static_cast<float>(e.getMinY());
maxy = static_cast<float>(e.getMaxY());

if (static_cast<double>(minx) > e.getMinY()) {
minx = std::nextafter(minx, -std::numeric_limits<float>::infinity());
}
if (static_cast<double>(maxx) < e.getMaxY()) {
maxx = std::nextafter(maxx, std::numeric_limits<float>::infinity());
}
if (static_cast<double>(miny) > e.getMinY()) {
miny = std::nextafter(miny, -std::numeric_limits<float>::infinity());
}
if (static_cast<double>(maxy) < e.getMaxY()) {
maxy = std::nextafter(maxy, std::numeric_limits<float>::infinity());
}
}

};


Expand Down
4 changes: 2 additions & 2 deletions include/geos/index/strtree/TemplateSTRtree.h
Original file line number Diff line number Diff line change
Expand Up @@ -690,11 +690,11 @@ struct IntervalTraits {
return a.getWidth();
}

static double getX(const BoundsType& a) {
static T getX(const BoundsType& a) {
return a.getMin() + a.getMax();
}

static double getY(const BoundsType& a) {
static T getY(const BoundsType& a) {
return a.getMin() + a.getMax();
}

Expand Down

0 comments on commit f8b9881

Please sign in to comment.