Skip to content

Commit

Permalink
Change logic of modify slot processing to avoid (big) objects copies
Browse files Browse the repository at this point in the history
  • Loading branch information
wawanbreton committed Jul 18, 2024
1 parent 5327c02 commit 931023a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 31 deletions.
2 changes: 1 addition & 1 deletion include/plugins/converters.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ struct gcode_paths_modify_response : public details::converter<gcode_paths_modif
[[nodiscard]] static constexpr PrintFeatureType getPrintFeatureType(const v0::PrintFeature feature) noexcept;
[[nodiscard]] static GCodePathConfig buildConfig(const v0::GCodePath& path);
[[nodiscard]] static constexpr SpaceFillType getSpaceFillType(const v0::SpaceFillType space_fill_type) noexcept;
native_value_type operator()(native_value_type& original_value, const value_type& message) const;
void operator()(native_value_type& value, const value_type& message) const;
};

} // namespace cura::plugins
Expand Down
14 changes: 6 additions & 8 deletions include/plugins/pluginproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,16 @@ class PluginProxy
return ret_value;
}

value_type modify(auto& original_value, auto&&... args)
void modify(value_type& value, auto&&... args)
{
agrpc::GrpcContext grpc_context;
value_type ret_value{};
grpc::Status status;

boost::asio::co_spawn(
grpc_context,
[this, &grpc_context, &status, &ret_value, &original_value, &args...]()
[this, &grpc_context, &status, &value, &args...]()
{
return this->modifyCall(grpc_context, status, ret_value, original_value, std::forward<decltype(args)>(args)...);
return this->modifyCall(grpc_context, status, value, std::forward<decltype(args)>(args)...);
},
boost::asio::detached);
grpc_context.run();
Expand All @@ -228,7 +227,6 @@ class PluginProxy
spdlog::error("Plugin for slot {} failed with error: {}", slot_info_.slot_id, status.error_message());
throw exceptions::RemoteException(slot_info_, status.error_message());
}
return ret_value;
}

template<plugins::v0::SlotID Subscription>
Expand Down Expand Up @@ -305,19 +303,19 @@ class PluginProxy
co_return;
}

boost::asio::awaitable<void> modifyCall(agrpc::GrpcContext& grpc_context, grpc::Status& status, value_type& ret_value, auto& original_value, auto&&... args)
boost::asio::awaitable<void> modifyCall(agrpc::GrpcContext& grpc_context, grpc::Status& status, value_type& value, auto&&... args)
{
using RPC = agrpc::ClientRPC<&invoke_stub_t::PrepareAsyncCall>;
grpc::ClientContext client_context{};
prep_client_context(client_context, slot_info_);

// Construct request
auto request{ req_(original_value, std::forward<decltype(args)>(args)...) };
auto request{ req_(value, std::forward<decltype(args)>(args)...) };

// Make unary request
rsp_msg_type response;
status = co_await RPC::request(grpc_context, invoke_stub_, client_context, request, response, boost::asio::use_awaitable);
ret_value = std::move(rsp_(original_value, response));
rsp_(value, response);
co_return;
}

Expand Down
13 changes: 5 additions & 8 deletions include/plugins/slotproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,21 @@ class SlotProxy
return std::invoke(default_process, std::forward<decltype(args)>(args)...);
}

constexpr auto modify(auto& original_value, auto&&... args)
constexpr auto modify(auto& value, auto&&... args)
{
if (! plugins_.empty())
{
auto modified_value = original_value;

for (value_type& plugin : plugins_)
{
modified_value = plugin.modify(modified_value, std::forward<decltype(args)>(args)...);
plugin.modify(value, std::forward<decltype(args)>(args)...);
}

return modified_value;
return;
}
if constexpr (sizeof...(args) == 0)
{
return std::invoke(default_process, original_value);
return std::invoke(default_process, value);
}
return std::invoke(default_process, original_value, std::forward<decltype(args)>(args)...);
return std::invoke(default_process, value, std::forward<decltype(args)>(args)...);
}

template<v0::SlotID S>
Expand Down
12 changes: 10 additions & 2 deletions include/plugins/slots.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ struct default_process
};
};

struct modify_default
{
constexpr void operator()(auto&& arg, auto&&...)
{
return; // Do nothing, but add some code for clangd-format not to mess up the syntax
};
};

struct simplify_default
{
auto operator()(auto&& arg, auto&&... args)
Expand Down Expand Up @@ -218,9 +226,9 @@ struct Holder

} // namespace details

using slot_gcode_paths_modify = details::slot_gcode_paths_modify_<>;
using slot_gcode_paths_modify = details::slot_gcode_paths_modify_<details::modify_default>;
using slot_infill_generate = details::slot_infill_generate_<details::infill_generate_default>;
using slot_postprocess = details::slot_postprocess_<>;
using slot_postprocess = details::slot_postprocess_<details::modify_default>;
using slot_settings_broadcast = details::slot_settings_broadcast_<>;
using slot_simplify = details::slot_simplify_<details::simplify_default>;

Expand Down
2 changes: 1 addition & 1 deletion src/LayerPlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2586,7 +2586,7 @@ void LayerPlan::applyModifyPlugin()
scripta::CellVDI{ "is_travel_path", &GCodePath::isTravelPath },
scripta::CellVDI{ "extrusion_mm3_per_mm", &GCodePath::getExtrusionMM3perMM });

extruder_plan.paths_ = slots::instance().modify<plugins::v0::SlotID::GCODE_PATHS_MODIFY>(extruder_plan.paths_, extruder_plan.extruder_nr_, layer_nr_);
slots::instance().modify<plugins::v0::SlotID::GCODE_PATHS_MODIFY>(extruder_plan.paths_, extruder_plan.extruder_nr_, layer_nr_);

scripta::log(
"extruder_plan_1",
Expand Down
9 changes: 5 additions & 4 deletions src/communication/ArcusCommunication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,13 @@ void ArcusCommunication::beginGCode()
void ArcusCommunication::flushGCode()
{
std::string gcode_output_stream = private_data->gcode_output_stream.str();
auto message_str = slots::instance().modify<plugins::v0::SlotID::POSTPROCESS_MODIFY>(gcode_output_stream);
if (message_str.size() == 0)
slots::instance().modify<plugins::v0::SlotID::POSTPROCESS_MODIFY>(gcode_output_stream);
if (gcode_output_stream.size() == 0)
{
return;
}
std::shared_ptr<proto::GCodeLayer> message = std::make_shared<proto::GCodeLayer>();
message->set_data(message_str);
message->set_data(gcode_output_stream);

// Send the g-code to the front-end! Yay!
private_data->socket->sendMessage(message);
Expand Down Expand Up @@ -390,7 +390,8 @@ void ArcusCommunication::sendGCodePrefix(const std::string& prefix) const
{
std::shared_ptr<proto::GCodePrefix> message = std::make_shared<proto::GCodePrefix>();
std::string message_str = prefix;
message->set_data(slots::instance().modify<plugins::v0::SlotID::POSTPROCESS_MODIFY>(message_str));
slots::instance().modify<plugins::v0::SlotID::POSTPROCESS_MODIFY>(message_str);
message->set_data(message_str);
private_data->socket->sendMessage(message);
}

Expand Down
13 changes: 6 additions & 7 deletions src/plugins/converters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,12 +434,10 @@ gcode_paths_modify_request::value_type
.fan_speed = path.fan_speed() };
}

gcode_paths_modify_response::native_value_type
gcode_paths_modify_response::operator()(gcode_paths_modify_response::native_value_type& original_value, const gcode_paths_modify_response::value_type& message) const
void gcode_paths_modify_response::operator()(gcode_paths_modify_response::native_value_type& value, const gcode_paths_modify_response::value_type& message) const
{
std::vector<GCodePath> paths;
using map_t = std::unordered_map<std::string, std::shared_ptr<const SliceMeshStorage>>;
auto meshes = original_value
auto meshes = value
| ranges::views::filter(
[](const auto& path)
{
Expand All @@ -452,6 +450,9 @@ gcode_paths_modify_response::native_value_type
})
| ranges::to<map_t>;

value.clear();
value.reserve(message.gcode_paths().size());

for (const auto& gcode_path_msg : message.gcode_paths())
{
GCodePath path{
Expand Down Expand Up @@ -482,10 +483,8 @@ gcode_paths_modify_response::native_value_type
})
| ranges::to_vector;

paths.emplace_back(path);
value.push_back(path);
}

return paths;
}
} // namespace cura::plugins

Expand Down

0 comments on commit 931023a

Please sign in to comment.