Skip to content

Commit

Permalink
(C++/performance) Removing passing c_str() to a function that takes s… (
Browse files Browse the repository at this point in the history
#2725)

(C++/performance) Removing passing c_str() to a function that takes std::string as argument
* - rename pgr_msg -> to_pg_msg
* -Adding overload of to_pg_msg
* - Using the new functions
* (docqueries) empty hint is not shown anymore

(cherry picked from commit bf1d91e)
  • Loading branch information
cvvergara committed Jan 19, 2025
1 parent ff46262 commit 86e7b72
Show file tree
Hide file tree
Showing 67 changed files with 901 additions and 1,121 deletions.
1 change: 0 additions & 1 deletion docqueries/ordering/topologicalSort.result
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ SELECT * FROM pgr_topologicalsort(
SELECT * FROM pgr_topologicalsort(
$$SELECT id, source, target, cost, reverse_cost FROM edges$$);
ERROR: Graph is not DAG
HINT:
CONTEXT: SQL function "pgr_topologicalsort" statement 1
/* -- q4 */
ROLLBACK;
Expand Down
2 changes: 1 addition & 1 deletion include/c_common/e_report.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* ~~~~{.c}
* std::ostringstream log;
* log << "the message";
* *log_msg = pgr_msg(log.str().c_str());
* *log_msg = to_pg_msg(log.str());
* ~~~~
*
*
Expand Down
5 changes: 3 additions & 2 deletions include/cpp_common/alloc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


#include <string>
#include <sstream>

extern "C" {

Expand Down Expand Up @@ -86,8 +87,8 @@ pgr_free(T* ptr) {
return nullptr;
}

char *
pgr_msg(const std::string &msg);
char* to_pg_msg(const std::string&);
char* to_pg_msg(const std::ostringstream&);

} // namespace pgrouting

Expand Down
24 changes: 11 additions & 13 deletions src/allpairs/floydWarshall_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pgr_do_floydWarshall(
size_t *return_count,
char ** log_msg,
char ** err_msg) {
using pgrouting::pgr_msg;
using pgrouting::to_pg_msg;
using pgrouting::pgr_free;

std::ostringstream log;
Expand Down Expand Up @@ -85,35 +85,33 @@ pgr_do_floydWarshall(

if (*return_count == 0) {
err << "No result generated, report this error\n";
*err_msg = pgr_msg(err.str().c_str());
*err_msg = to_pg_msg(err);
*return_tuples = NULL;
*return_count = 0;
return;
}

*log_msg = log.str().empty()?
*log_msg :
pgr_msg(log.str().c_str());
*log_msg = to_pg_msg(log);
} catch (AssertFailedException &except) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
err << except.what();
*err_msg = pgr_msg(err.str().c_str());
*log_msg = pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(err);
*log_msg = to_pg_msg(log);
} catch (const std::string &ex) {
*err_msg = pgr_msg(ex.c_str());
*log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(ex);
*log_msg = hint? to_pg_msg(hint) : to_pg_msg(log);
} catch (std::exception &except) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
err << except.what();
*err_msg = pgr_msg(err.str().c_str());
*log_msg = pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(err);
*log_msg = to_pg_msg(log);
} catch(...) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
err << "Caught unknown exception!";
*err_msg = pgr_msg(err.str().c_str());
*log_msg = pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(err);
*log_msg = to_pg_msg(log);
}
}
24 changes: 11 additions & 13 deletions src/allpairs/johnson_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pgr_do_johnson(
size_t *return_count,
char ** log_msg,
char ** err_msg) {
using pgrouting::pgr_msg;
using pgrouting::to_pg_msg;
using pgrouting::pgr_free;

std::ostringstream log;
Expand Down Expand Up @@ -84,35 +84,33 @@ pgr_do_johnson(

if (*return_count == 0) {
err << "No result generated, report this error\n";
*err_msg = pgr_msg(err.str().c_str());
*err_msg = to_pg_msg(err);
*return_tuples = NULL;
*return_count = 0;
return;
}

*log_msg = log.str().empty()?
*log_msg :
pgr_msg(log.str().c_str());
*log_msg = to_pg_msg(log);
} catch (AssertFailedException &except) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
err << except.what();
*err_msg = pgr_msg(err.str().c_str());
*log_msg = pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(err);
*log_msg = to_pg_msg(log);
} catch (const std::string &ex) {
*err_msg = pgr_msg(ex.c_str());
*log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(ex);
*log_msg = hint? to_pg_msg(hint) : to_pg_msg(log);
} catch (std::exception &except) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
err << except.what();
*err_msg = pgr_msg(err.str().c_str());
*log_msg = pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(err);
*log_msg = to_pg_msg(log);
} catch(...) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
err << "Caught unknown exception!";
*err_msg = pgr_msg(err.str().c_str());
*log_msg = pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(err);
*log_msg = to_pg_msg(log);
}
}
30 changes: 13 additions & 17 deletions src/alpha_shape/alphaShape_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pgr_do_alphaShape(
char **notice_msg,
char **err_msg) {
using pgrouting::pgr_alloc;
using pgrouting::pgr_msg;
using pgrouting::to_pg_msg;
using pgrouting::pgr_free;

std::ostringstream log;
Expand Down Expand Up @@ -180,46 +180,42 @@ pgr_do_alphaShape(
*return_tuples = pgr_alloc(*return_count, (*return_tuples));
std::stringstream ss;
ss << "MULTIPOLYGON EMPTY";
(*return_tuples)[0].geom = pgr_msg(ss.str().c_str());
(*return_tuples)[0].geom = to_pg_msg(ss.str());
} else {
*return_count = results.size();
*return_tuples = pgr_alloc(*return_count, (*return_tuples));
size_t row = 0;
for (const auto &r : results) {
std::stringstream ss;
ss << bg::wkt(r);
(*return_tuples)[row].geom = pgr_msg(ss.str().c_str());
(*return_tuples)[row].geom = to_pg_msg(ss.str());
++row;
}
}


*log_msg = log.str().empty()?
*log_msg :
pgr_msg(log.str().c_str());
*notice_msg = notice.str().empty()?
*notice_msg :
pgr_msg(notice.str().c_str());
*log_msg = to_pg_msg(log);
*notice_msg = to_pg_msg(notice);
} catch (AssertFailedException &except) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
err << except.what();
*err_msg = pgr_msg(err.str().c_str());
*log_msg = pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(err);
*log_msg = to_pg_msg(log);
} catch (const std::string &ex) {
*err_msg = pgr_msg(ex.c_str());
*log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(ex);
*log_msg = hint? to_pg_msg(hint) : to_pg_msg(log);
} catch (std::exception &except) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
err << except.what();
*err_msg = pgr_msg(err.str().c_str());
*log_msg = pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(err);
*log_msg = to_pg_msg(log);
} catch(...) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
err << "Caught unknown exception!";
*err_msg = pgr_msg(err.str().c_str());
*log_msg = pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(err);
*log_msg = to_pg_msg(log);
}
}
36 changes: 16 additions & 20 deletions src/astar/astar_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void pgr_do_astar(
char** log_msg, char** notice_msg, char** err_msg) {
using pgrouting::Path;
using pgrouting::pgr_alloc;
using pgrouting::pgr_msg;
using pgrouting::to_pg_msg;
using pgrouting::pgr_free;
using pgrouting::utilities::get_combinations;

Expand All @@ -85,8 +85,8 @@ void pgr_do_astar(
hint = nullptr;

if (combinations.empty() && combinations_sql) {
*notice_msg = pgr_msg("No (source, target) pairs found");
*log_msg = pgr_msg(combinations_sql);
*notice_msg = to_pg_msg("No (source, target) pairs found");
*log_msg = to_pg_msg(combinations_sql);
return;
}

Expand All @@ -96,8 +96,8 @@ void pgr_do_astar(
hint = nullptr;

if (edges.empty()) {
*notice_msg = pgr_msg("No edges found");
*log_msg = pgr_msg(edges_sql);
*notice_msg = to_pg_msg("No edges found");
*log_msg = to_pg_msg(edges_sql);
return;
}

Expand Down Expand Up @@ -128,39 +128,35 @@ void pgr_do_astar(
(*return_tuples) = nullptr;
(*return_count) = 0;
notice << "No paths found\n";
*log_msg = pgr_msg(notice.str().c_str());
*log_msg = to_pg_msg(notice);
return;
}

(*return_tuples) = pgr_alloc(count, (*return_tuples));
(*return_count) = (collapse_paths(return_tuples, paths));

*log_msg = log.str().empty()?
*log_msg :
pgr_msg(log.str().c_str());
*notice_msg = notice.str().empty()?
*notice_msg :
pgr_msg(notice.str().c_str());
*log_msg = to_pg_msg(log);
*notice_msg = to_pg_msg(notice);
} catch (AssertFailedException &except) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
err << except.what();
*err_msg = pgr_msg(err.str().c_str());
*log_msg = pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(err);
*log_msg = to_pg_msg(log);
} catch (const std::string &ex) {
*err_msg = pgr_msg(ex.c_str());
*log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(ex);
*log_msg = hint? to_pg_msg(hint) : to_pg_msg(log);
} catch (std::exception &except) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
err << except.what();
*err_msg = pgr_msg(err.str().c_str());
*log_msg = pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(err);
*log_msg = to_pg_msg(log);
} catch(...) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
err << "Caught unknown exception!";
*err_msg = pgr_msg(err.str().c_str());
*log_msg = pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(err);
*log_msg = to_pg_msg(log);
}
}
36 changes: 16 additions & 20 deletions src/bdAstar/bdAstar_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void pgr_do_bdAstar(
char** log_msg, char** notice_msg, char** err_msg) {
using pgrouting::Path;
using pgrouting::pgr_alloc;
using pgrouting::pgr_msg;
using pgrouting::to_pg_msg;
using pgrouting::pgr_free;
using pgrouting::utilities::get_combinations;

Expand All @@ -83,17 +83,17 @@ void pgr_do_bdAstar(
hint = nullptr;

if (combinations.empty() && combinations_sql) {
*notice_msg = pgr_msg("No (source, target) pairs found");
*log_msg = pgr_msg(combinations_sql);
*notice_msg = to_pg_msg("No (source, target) pairs found");
*log_msg = to_pg_msg(combinations_sql);
return;
}

hint = edges_sql;
auto edges = pgrouting::pgget::get_edges_xy(std::string(edges_sql), true);

if (edges.empty()) {
*notice_msg = pgr_msg("No edges found");
*log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str());
*notice_msg = to_pg_msg("No edges found");
*log_msg = hint? to_pg_msg(hint) : to_pg_msg(log);
return;
}
hint = nullptr;
Expand All @@ -118,39 +118,35 @@ void pgr_do_bdAstar(
(*return_tuples) = nullptr;
(*return_count) = 0;
notice << "No paths found\n";
*log_msg = pgr_msg(notice.str().c_str());
*log_msg = to_pg_msg(notice);
return;
}

(*return_tuples) = pgr_alloc(count, (*return_tuples));
(*return_count) = (collapse_paths(return_tuples, paths));

*log_msg = log.str().empty()?
*log_msg :
pgr_msg(log.str().c_str());
*notice_msg = notice.str().empty()?
*notice_msg :
pgr_msg(notice.str().c_str());
*log_msg = to_pg_msg(log);
*notice_msg = to_pg_msg(notice);
} catch (AssertFailedException &except) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
err << except.what();
*err_msg = pgr_msg(err.str().c_str());
*log_msg = pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(err);
*log_msg = to_pg_msg(log);
} catch (const std::string &ex) {
*err_msg = pgr_msg(ex.c_str());
*log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(ex);
*log_msg = hint? to_pg_msg(hint) : to_pg_msg(log);
} catch (std::exception &except) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
err << except.what();
*err_msg = pgr_msg(err.str().c_str());
*log_msg = pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(err);
*log_msg = to_pg_msg(log);
} catch(...) {
(*return_tuples) = pgr_free(*return_tuples);
(*return_count) = 0;
err << "Caught unknown exception!";
*err_msg = pgr_msg(err.str().c_str());
*log_msg = pgr_msg(log.str().c_str());
*err_msg = to_pg_msg(err);
*log_msg = to_pg_msg(log);
}
}
Loading

0 comments on commit 86e7b72

Please sign in to comment.