Skip to content

Commit

Permalink
Optimizes hotpath resource reservation using upper bound for AabbTree…
Browse files Browse the repository at this point in the history
…World
  • Loading branch information
louis-langholtz committed Oct 12, 2023
1 parent 352d716 commit 878b830
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
5 changes: 3 additions & 2 deletions Library/source/playrho/d2/AabbTreeWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,9 @@ auto FindContacts(pmr::memory_resource& resource,
-> std::vector<AabbTreeWorld::ProxyKey, pmr::polymorphic_allocator<AabbTreeWorld::ProxyKey>>
{
std::vector<AabbTreeWorld::ProxyKey, pmr::polymorphic_allocator<AabbTreeWorld::ProxyKey>> proxyKeys{&resource};
static constexpr auto DefaultReserveSize = 512u;
proxyKeys.reserve(DefaultReserveSize); // upper bound is current size of tree
// Never need more than tree.GetLeafCount(), but in case big, use smaller default...
static constexpr auto DefaultReserveSize = 256u;
proxyKeys.reserve(std::min(tree.GetLeafCount(), DefaultReserveSize));

// Accumalate contact keys for pairs of nodes that are overlapping and aren't identical.
// Note that if the dynamic tree node provides the body index, it's assumed to be faster
Expand Down
12 changes: 6 additions & 6 deletions UnitTests/AabbTreeWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,12 @@ TEST(AabbTreeWorld, GetResourceStatsWhenOn)
Step(world, stepConf);
stats = GetResourceStats(world);
ASSERT_TRUE(stats.has_value());
EXPECT_GT(stats->blocksAllocated, oldstats->blocksAllocated);
EXPECT_GT(stats->bytesAllocated, oldstats->bytesAllocated);
EXPECT_GT(stats->maxBlocksAllocated, oldstats->maxBlocksAllocated);
EXPECT_GT(stats->maxBytesAllocated, oldstats->maxBytesAllocated);
EXPECT_GT(stats->maxBytes, oldstats->maxBytes);
EXPECT_GT(stats->maxAlignment, oldstats->maxAlignment);
EXPECT_EQ(stats->blocksAllocated, oldstats->blocksAllocated);
EXPECT_EQ(stats->bytesAllocated, oldstats->bytesAllocated);
EXPECT_EQ(stats->maxBlocksAllocated, oldstats->maxBlocksAllocated);
EXPECT_EQ(stats->maxBytesAllocated, oldstats->maxBytesAllocated);
EXPECT_EQ(stats->maxBytes, oldstats->maxBytes);
EXPECT_EQ(stats->maxAlignment, oldstats->maxAlignment);
}

TEST(AabbTreeWorld, CreateDestroyEmptyStaticBody)
Expand Down
12 changes: 6 additions & 6 deletions UnitTests/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3749,10 +3749,10 @@ TEST(World, GetResourceStatsWhenOn)
Step(world, stepConf);
stats = GetResourceStats(world);
ASSERT_TRUE(stats.has_value());
EXPECT_GT(stats->blocksAllocated, oldstats->blocksAllocated);
EXPECT_GT(stats->bytesAllocated, oldstats->bytesAllocated);
EXPECT_GT(stats->maxBlocksAllocated, oldstats->maxBlocksAllocated);
EXPECT_GT(stats->maxBytesAllocated, oldstats->maxBytesAllocated);
EXPECT_GT(stats->maxBytes, oldstats->maxBytes);
EXPECT_GT(stats->maxAlignment, oldstats->maxAlignment);
EXPECT_EQ(stats->blocksAllocated, oldstats->blocksAllocated);
EXPECT_EQ(stats->bytesAllocated, oldstats->bytesAllocated);
EXPECT_EQ(stats->maxBlocksAllocated, oldstats->maxBlocksAllocated);
EXPECT_EQ(stats->maxBytesAllocated, oldstats->maxBytesAllocated);
EXPECT_EQ(stats->maxBytes, oldstats->maxBytes);
EXPECT_EQ(stats->maxAlignment, oldstats->maxAlignment);
}

0 comments on commit 878b830

Please sign in to comment.