Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lua scripting speedups #4147

Merged
merged 2 commits into from
Jun 13, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/extractor/scripting_environment_lua.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ struct LuaScriptingContext final
bool has_way_function;
bool has_segment_function;

sol::function turn_function;
sol::function way_function;
sol::function node_function;
sol::function segment_function;

int api_version;
};

Expand Down
32 changes: 14 additions & 18 deletions src/extractor/scripting_environment_lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,16 @@ void Sol2ScriptingEnvironment::InitContext(LuaScriptingContext &context)

context.state.script_file(file_name);

sol::function turn_function = context.state["turn_function"];
sol::function node_function = context.state["node_function"];
sol::function way_function = context.state["way_function"];
sol::function segment_function = context.state["segment_function"];
// cache references to functions for faster execution
context.turn_function = context.state["turn_function"];
context.node_function = context.state["node_function"];
context.way_function = context.state["way_function"];
context.segment_function = context.state["segment_function"];

context.has_turn_penalty_function = turn_function.valid();
context.has_node_function = node_function.valid();
context.has_way_function = way_function.valid();
context.has_segment_function = segment_function.valid();
context.has_turn_penalty_function = context.turn_function.valid();
context.has_node_function = context.node_function.valid();
context.has_way_function = context.way_function.valid();
context.has_segment_function = context.segment_function.valid();

// Check profile API version
auto maybe_version = context.state.get<sol::optional<int>>("api_version");
Expand Down Expand Up @@ -590,13 +591,12 @@ void Sol2ScriptingEnvironment::ProcessTurn(ExtractionTurn &turn)
{
auto &context = GetSol2Context();

sol::function turn_function = context.state["turn_function"];
switch (context.api_version)
{
case 1:
if (context.has_turn_penalty_function)
{
turn_function(turn);
context.turn_function(turn);

// Turn weight falls back to the duration value in deciseconds
// or uses the extracted unit-less weight value
Expand All @@ -611,7 +611,7 @@ void Sol2ScriptingEnvironment::ProcessTurn(ExtractionTurn &turn)
if (turn.turn_type != guidance::TurnType::NoTurn)
{
// Get turn duration and convert deci-seconds to seconds
turn.duration = static_cast<double>(turn_function(turn.angle)) / 10.;
turn.duration = static_cast<double>(context.turn_function(turn.angle)) / 10.;
BOOST_ASSERT(turn.weight == 0);

// add U-turn penalty
Expand Down Expand Up @@ -642,14 +642,14 @@ void Sol2ScriptingEnvironment::ProcessSegment(ExtractionSegment &segment)

if (context.has_segment_function)
{
sol::function segment_function = context.state["segment_function"];
switch (context.api_version)
{
case 1:
segment_function(segment);
context.segment_function(segment);
break;
case 0:
segment_function(segment.source, segment.target, segment.distance, segment.duration);
context.segment_function(
segment.source, segment.target, segment.distance, segment.duration);
segment.weight = segment.duration; // back-compatibility fallback to duration
break;
}
Expand All @@ -660,17 +660,13 @@ void LuaScriptingContext::ProcessNode(const osmium::Node &node, ExtractionNode &
{
BOOST_ASSERT(state.lua_state() != nullptr);

sol::function node_function = state["node_function"];

node_function(node, result);
}

void LuaScriptingContext::ProcessWay(const osmium::Way &way, ExtractionWay &result)
{
BOOST_ASSERT(state.lua_state() != nullptr);

sol::function way_function = state["way_function"];

way_function(way, result);
}
}
Expand Down