Skip to content

Commit 638cfad

Browse files
committed
refactor(ThingFactory): replace calls to Tree::get_ancestors with a static map, as Tree is too slow when getting ancestors
1 parent 5905bba commit 638cfad

3 files changed

Lines changed: 9 additions & 4 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.30.0..4.3.0)
22
project(Nostalgia
3-
VERSION 0.19.1
3+
VERSION 0.19.2
44
LANGUAGES CXX C)
55

66
option(BUILD_SHARED_LIBS "Build the shared version of Nostalgia (Linux only)" OFF)

Nostalgia/Nostalgia.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#define NOSTALGIA_VERSION_MAJOR 0
88
#define NOSTALGIA_VERSION_MINOR 19
9-
#define NOSTALGIA_VERSION_PATCH 1
9+
#define NOSTALGIA_VERSION_PATCH 2
1010
#define NOSTALGIA_VERSION_STRING \
1111
__n_make_string(NOSTALGIA_VERSION_MAJOR) "." \
1212
__n_make_string(NOSTALGIA_VERSION_MINOR) "." \

Nostalgia/things/thing_factory.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ static std::map<PID, PID> sTypeDeclarations{};
3333
static bool sIsInitialized{false};
3434
static std::map<ID, pThingMakerTemplate_t> sThingMakers{};
3535
static std::map<ID, int> sTypePriorities{};
36+
static std::map<ID, std::unordered_set<PID>> sTypeAncestors{};
3637
static Tree<PID> sTypes{};
3738

3839
bool ThingFactory::Init()
@@ -117,6 +118,7 @@ Error ThingFactory::AddThing(pThingMakerTemplate_t inPtr,
117118
}
118119
sTypePriorities[inType] = inPriority;
119120
sThingMakers[inType] = inPtr;
121+
sTypeAncestors[inType] = sTypes.get_ancestors(inType);
120122
if(Console::GetVariable("ThingFactory.debug_register_msgs").int_value)
121123
{
122124
if(inType == ThingType::Thing.name())
@@ -160,7 +162,7 @@ PID ThingFactory::BaseOf(FPID inType) noexcept
160162
{ return sTypes.get_node(inType).parent; }
161163

162164
bool ThingFactory::IsThing(FPID inTypeID)
163-
{ return sTypes.has_node(inTypeID) or sTypeDeclarations.contains(inTypeID); }
165+
{ return sTypeAncestors.contains(inTypeID) or sTypeDeclarations.contains(inTypeID); }
164166

165167
bool ThingFactory::IsThinker(FPID inTypeID)
166168
{ return IsDerivedFrom(inTypeID, ThingType::Thinker); }
@@ -169,4 +171,7 @@ bool ThingFactory::IsResource(FPID inTypeID)
169171
{ return IsDerivedFrom(inTypeID, ThingType::Resource); }
170172

171173
bool ThingFactory::IsDerivedFrom(FPID inTypeID1, FPID inTypeID2)
172-
{ return inTypeID1 == inTypeID2 or sTypes.get_descendants(inTypeID2).contains(inTypeID1); }
174+
{
175+
return inTypeID1 == inTypeID2
176+
or (sTypeAncestors.contains(inTypeID1) and sTypeAncestors.at(inTypeID1).contains(inTypeID2));
177+
}

0 commit comments

Comments
 (0)