-
Notifications
You must be signed in to change notification settings - Fork 7
Closed
Labels
P3Priority: LowPriority: LowbugSomething isn't workingSomething isn't workinggood first issueGood for newcomersGood for newcomers
Description
There is some unnecessary copying in the Engine class (in resolveIds() and compile()).
libscratchcpp/src/engine/internal/engine.cpp
Lines 55 to 145 in 19283ed
| void Engine::resolveIds() | |
| { | |
| for (auto target : m_targets) { | |
| std::cout << "Processing target " << target->name() << "..." << std::endl; | |
| auto blocks = target->blocks(); | |
| for (auto block : blocks) { | |
| auto container = blockSectionContainer(block->opcode()); | |
| block->setNext(getBlock(block->nextId())); | |
| block->setParent(getBlock(block->parentId())); | |
| if (container) | |
| block->setCompileFunction(container->resolveBlockCompileFunc(block->opcode())); | |
| auto inputs = block->inputs(); | |
| for (auto input : inputs) { | |
| input->setValueBlock(getBlock(input->valueBlockId())); | |
| if (container) | |
| input->setInputId(container->resolveInput(input->name())); | |
| input->primaryValue()->setValuePtr(getEntity(input->primaryValue()->valueId())); | |
| input->secondaryValue()->setValuePtr(getEntity(input->primaryValue()->valueId())); | |
| } | |
| auto fields = block->fields(); | |
| for (auto field : fields) { | |
| field->setValuePtr(getEntity(field->valueId())); | |
| if (container) { | |
| field->setFieldId(container->resolveField(field->name())); | |
| if (!field->valuePtr()) | |
| field->setSpecialValueId(container->resolveFieldValue(field->value().toString())); | |
| } | |
| } | |
| block->updateInputMap(); | |
| block->updateFieldMap(); | |
| auto comment = getComment(block->commentId()); | |
| block->setComment(comment); | |
| if (comment) { | |
| comment->setBlock(block); | |
| assert(comment->blockId() == block->id()); | |
| } | |
| } | |
| } | |
| } | |
| void Engine::compile() | |
| { | |
| // Resolve entities by ID | |
| resolveIds(); | |
| // Compile scripts to bytecode | |
| for (auto target : m_targets) { | |
| std::cout << "Compiling scripts in target " << target->name() << "..." << std::endl; | |
| std::unordered_map<std::string, unsigned int *> procedureBytecodeMap; | |
| Compiler compiler(this, target.get()); | |
| auto blocks = target->blocks(); | |
| for (auto block : blocks) { | |
| if (block->topLevel()) { | |
| auto section = blockSection(block->opcode()); | |
| if (section) { | |
| auto script = std::make_shared<Script>(target.get(), this); | |
| m_scripts[block] = script; | |
| compiler.compile(block); | |
| script->setFunctions(m_functions); | |
| script->setBytecode(compiler.bytecode()); | |
| if (block->opcode() == "procedures_definition") { | |
| auto b = block->inputAt(block->findInput("custom_block"))->valueBlock(); | |
| procedureBytecodeMap[b->mutationPrototype()->procCode()] = script->bytecode(); | |
| } | |
| } else | |
| std::cout << "warning: unsupported top level block: " << block->opcode() << std::endl; | |
| } | |
| } | |
| const std::vector<std::string> &procedures = compiler.procedures(); | |
| std::vector<unsigned int *> procedureBytecodes; | |
| for (const std::string &code : procedures) | |
| procedureBytecodes.push_back(procedureBytecodeMap[code]); | |
| for (auto block : blocks) { | |
| if (m_scripts.count(block) == 1) { | |
| m_scripts[block]->setProcedures(procedureBytecodes); | |
| m_scripts[block]->setConstValues(compiler.constValues()); | |
| m_scripts[block]->setVariables(compiler.variables()); | |
| m_scripts[block]->setLists(compiler.lists()); | |
| } | |
| } | |
| } | |
| } |
For example
auto blocks = target->blocks(); should be const auto &blocks = target->blocks();Metadata
Metadata
Assignees
Labels
P3Priority: LowPriority: LowbugSomething isn't workingSomething isn't workinggood first issueGood for newcomersGood for newcomers