Skip to content

Commit

Permalink
Add some caching, formatting optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
f0reachARR committed Oct 3, 2024
1 parent aecf7c8 commit 65e5610
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ struct Action : public Scope, public ComplexType, public StoryboardElement
{
bool overridden = false;

std::string type_name;

explicit Action(const pugi::xml_node &, Scope &);

using StoryboardElement::evaluate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ struct Condition : public ComplexType, private SimulatorCore::ConditionEvaluatio

bool current_value;

std::string type_name;

private:
struct History
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ struct SimulationTimeCondition : private SimulatorCore::ConditionEvaluation

Double result;

std::string description_condition_part;

auto description() const -> String;

auto evaluate() -> Object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ auto Interpreter::on_shutdown(const rclcpp_lifecycle::State &) -> Result
auto Interpreter::publishCurrentContext() const -> void
{
auto start = std::chrono::steady_clock::now();
static size_t string_size = 0;
Context context;
{
boost::json::monotonic_resource mr;
Expand All @@ -294,7 +295,15 @@ auto Interpreter::publishCurrentContext() const -> void
if (publish_empty_context) {
context.data = "";
} else {
context.data = boost::json::serialize(json << *script);
json << *script;

std::ostringstream os(context.data);

os << json;
context.data = os.str();

string_size = context.data.size();

auto end = std::chrono::steady_clock::now();
RCLCPP_INFO_STREAM(
get_logger(),
Expand Down
4 changes: 2 additions & 2 deletions openscenario/openscenario_interpreter/src/syntax/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Action::Action(const pugi::xml_node & node, Scope & scope)
std::make_pair( "PrivateAction", [this](auto && node) { return make< PrivateAction>(node, local()); })))
// clang-format on
{
type_name = apply<std::string>([](auto && action) { return makeTypename(action.type()); }, *this);
}

auto Action::accomplished() const -> bool { return ComplexType::accomplished(); }
Expand Down Expand Up @@ -64,8 +65,7 @@ auto operator<<(boost::json::object & json, const Action & datum) -> boost::json

json["currentState"] = boost::lexical_cast<std::string>(datum.state());

json["type"] =
apply<std::string>([](auto && action) { return makeTypename(action.type()); }, datum);
json["type"] = datum.type_name;

return json;
}
Expand Down
23 changes: 12 additions & 11 deletions openscenario/openscenario_interpreter/src/syntax/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ Condition::Condition(const pugi::xml_node & node, Scope & scope)
current_value(false)
// clang-format on
{
// clang-format off
static const std::unordered_map<
std::type_index,
std::function<std::string(const Condition &)>> table
{
{ typeid(ByEntityCondition), [&](const Condition & condition) { return makeTypename(condition.as<ByEntityCondition>().type()); } },
{ typeid( ByValueCondition), [&](const Condition & condition) { return makeTypename(condition.as< ByValueCondition>().type()); } },
};
// clang-format on

type_name = table.at(type())(*this);
}

auto Condition::evaluate() -> Object
Expand Down Expand Up @@ -76,17 +87,7 @@ auto operator<<(boost::json::object & json, const Condition & datum) -> boost::j

json["name"] = datum.name;

// clang-format off
static const std::unordered_map<
std::type_index,
std::function<std::string(const Condition &)>> table
{
{ typeid(ByEntityCondition), [&](const Condition & condition) { return makeTypename(condition.as<ByEntityCondition>().type()); } },
{ typeid( ByValueCondition), [&](const Condition & condition) { return makeTypename(condition.as< ByValueCondition>().type()); } },
};
// clang-format on

json["type"] = table.at(datum.type())(datum);
json["type"] = datum.type_name;

return json;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#define FMT_HEADER_ONLY

#include <fmt/format.h>

#include <iomanip>
#include <openscenario_interpreter/reader/attribute.hpp>
#include <openscenario_interpreter/syntax/simulation_time_condition.hpp>
Expand All @@ -24,16 +28,16 @@ SimulationTimeCondition::SimulationTimeCondition(const pugi::xml_node & node, Sc
: value(readAttribute<Double>("value", node, scope)),
compare(readAttribute<Rule>("rule", node, scope))
{
std::stringstream os;
os << "is " << compare << " " << value << "?";
description_condition_part = os.str();
}

auto SimulationTimeCondition::description() const -> String
{
std::stringstream description;

description << "Is the simulation time (= " << std::fixed << std::setprecision(6) << result
<< ") is " << compare << " " << value << "?";

return description.str();
return fmt::format(
"Is the simulation time (= {:.30f}) {}", static_cast<double>(result),
description_condition_part);
}

auto SimulationTimeCondition::evaluate() -> Object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ auto Trigger::activeConditionGroupDescription() const

auto operator<<(boost::json::object & json, const Trigger & datum) -> boost::json::object &
{
json["currentValue"] = boost::lexical_cast<std::string>(Boolean(datum.current_value));
json.emplace("currentValue", boost::lexical_cast<std::string>(Boolean(datum.current_value)));

auto & condition_groups = json["ConditionGroup"].emplace_array();

Expand Down

0 comments on commit 65e5610

Please sign in to comment.