Skip to content

Commit 3e5c33d

Browse files
committed
Engine: Optimize get entity methods by passing target
1 parent 3402d16 commit 3e5c33d

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed

src/engine/internal/engine.cpp

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ void Engine::resolveIds()
107107
const auto &blocks = target->blocks();
108108
for (auto block : blocks) {
109109
auto container = blockSectionContainer(block->opcode());
110-
block->setNext(getBlock(block->nextId()));
111-
block->setParent(getBlock(block->parentId()));
110+
block->setNext(getBlock(block->nextId(), target.get()));
111+
block->setParent(getBlock(block->parentId(), target.get()));
112112

113113
if (container) {
114114
block->setCompileFunction(container->resolveBlockCompileFunc(block->opcode()));
@@ -117,16 +117,16 @@ void Engine::resolveIds()
117117

118118
const auto &inputs = block->inputs();
119119
for (const auto &input : inputs) {
120-
input->setValueBlock(getBlock(input->valueBlockId()));
120+
input->setValueBlock(getBlock(input->valueBlockId(), target.get()));
121121

122122
if (container)
123123
input->setInputId(container->resolveInput(input->name()));
124124

125125
InputValue *value = input->primaryValue();
126126
std::string id = value->valueId(); // no reference!
127-
value->setValuePtr(getEntity(id));
127+
value->setValuePtr(getEntity(id, target.get()));
128128
assert(input->secondaryValue()->type() != InputValue::Type::Variable && input->secondaryValue()->type() != InputValue::Type::List); // secondary values never have a variable or list
129-
input->secondaryValue()->setValuePtr(getEntity(input->secondaryValue()->valueId()));
129+
input->secondaryValue()->setValuePtr(getEntity(input->secondaryValue()->valueId(), target.get()));
130130

131131
// Add missing variables and lists
132132
if (!value->valuePtr()) {
@@ -149,7 +149,7 @@ void Engine::resolveIds()
149149
const auto &fields = block->fields();
150150
for (auto field : fields) {
151151
std::string id = field->valueId(); // no reference!
152-
field->setValuePtr(getEntity(id));
152+
field->setValuePtr(getEntity(id, target.get()));
153153

154154
if (container) {
155155
field->setFieldId(container->resolveField(field->name()));
@@ -179,7 +179,7 @@ void Engine::resolveIds()
179179
block->updateInputMap();
180180
block->updateFieldMap();
181181

182-
auto comment = getComment(block->commentId());
182+
auto comment = getComment(block->commentId(), target.get());
183183
block->setComment(comment);
184184

185185
if (comment) {
@@ -209,7 +209,7 @@ void Engine::resolveIds()
209209
assert(target);
210210

211211
for (auto field : fields) {
212-
field->setValuePtr(getEntity(field->valueId()));
212+
field->setValuePtr(getEntity(field->valueId(), target));
213213

214214
if (container) {
215215
field->setFieldId(container->resolveField(field->name()));
@@ -1411,17 +1411,27 @@ const std::unordered_map<std::shared_ptr<Block>, std::shared_ptr<Script>> &Engin
14111411
}
14121412

14131413
// Returns the block with the given ID.
1414-
std::shared_ptr<Block> Engine::getBlock(const std::string &id)
1414+
std::shared_ptr<Block> Engine::getBlock(const std::string &id, Target *target)
14151415
{
14161416
if (id.empty())
14171417
return nullptr;
14181418

1419-
for (auto target : m_targets) {
1420-
int index = target->findBlock(id);
1419+
int index;
1420+
1421+
if (target) {
1422+
index = target->findBlock(id);
1423+
14211424
if (index != -1)
14221425
return target->blockAt(index);
14231426
}
14241427

1428+
for (auto t : m_targets) {
1429+
index = t->findBlock(id);
1430+
1431+
if (index != -1)
1432+
return t->blockAt(index);
1433+
}
1434+
14251435
return nullptr;
14261436
}
14271437

@@ -1469,22 +1479,32 @@ std::shared_ptr<Broadcast> Engine::getBroadcast(const std::string &id)
14691479
}
14701480

14711481
// Returns the comment with the given ID.
1472-
std::shared_ptr<Comment> Engine::getComment(const std::string &id)
1482+
std::shared_ptr<Comment> Engine::getComment(const std::string &id, Target *target)
14731483
{
14741484
if (id.empty())
14751485
return nullptr;
14761486

1477-
for (auto target : m_targets) {
1478-
int index = target->findComment(id);
1487+
int index;
1488+
1489+
if (target) {
1490+
index = target->findComment(id);
1491+
14791492
if (index != -1)
14801493
return target->commentAt(index);
14811494
}
14821495

1496+
for (auto t : m_targets) {
1497+
index = t->findComment(id);
1498+
1499+
if (index != -1)
1500+
return t->commentAt(index);
1501+
}
1502+
14831503
return nullptr;
14841504
}
14851505

14861506
// Returns the entity with the given ID. \see IEntity
1487-
std::shared_ptr<Entity> Engine::getEntity(const std::string &id)
1507+
std::shared_ptr<Entity> Engine::getEntity(const std::string &id, Target *target)
14881508
{
14891509
// Variables
14901510
auto variable = getVariable(id);
@@ -1502,12 +1522,12 @@ std::shared_ptr<Entity> Engine::getEntity(const std::string &id)
15021522
return std::static_pointer_cast<Entity>(broadcast);
15031523

15041524
// Blocks
1505-
auto block = getBlock(id);
1525+
auto block = getBlock(id, target);
15061526
if (block)
15071527
return std::static_pointer_cast<Entity>(block);
15081528

15091529
// Comments
1510-
auto comment = getComment(id);
1530+
auto comment = getComment(id, target);
15111531
if (comment)
15121532
return std::static_pointer_cast<Entity>(comment);
15131533

src/engine/internal/engine.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,12 @@ class Engine : public IEngine
202202
void removeExecutableClones();
203203
void createMissingMonitors();
204204
void addVarOrListMonitor(std::shared_ptr<Monitor> monitor, Target *target);
205-
std::shared_ptr<Block> getBlock(const std::string &id);
205+
std::shared_ptr<Block> getBlock(const std::string &id, Target *target);
206206
std::shared_ptr<Variable> getVariable(const std::string &id);
207207
std::shared_ptr<List> getList(const std::string &id);
208208
std::shared_ptr<Broadcast> getBroadcast(const std::string &id);
209-
std::shared_ptr<Comment> getComment(const std::string &id);
210-
std::shared_ptr<Entity> getEntity(const std::string &id);
209+
std::shared_ptr<Comment> getComment(const std::string &id, Target *target);
210+
std::shared_ptr<Entity> getEntity(const std::string &id, Target *target);
211211
std::shared_ptr<IBlockSection> blockSection(const std::string &opcode) const;
212212

213213
void addHatToMap(std::unordered_map<Target *, std::vector<Script *>> &map, Script *script);

0 commit comments

Comments
 (0)