Skip to content

Commit 86e7b72

Browse files
committed
(C++/performance) Removing passing c_str() to a function that takes s… (#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)
1 parent ff46262 commit 86e7b72

File tree

67 files changed

+901
-1121
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+901
-1121
lines changed

docqueries/ordering/topologicalSort.result

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ SELECT * FROM pgr_topologicalsort(
6262
SELECT * FROM pgr_topologicalsort(
6363
$$SELECT id, source, target, cost, reverse_cost FROM edges$$);
6464
ERROR: Graph is not DAG
65-
HINT:
6665
CONTEXT: SQL function "pgr_topologicalsort" statement 1
6766
/* -- q4 */
6867
ROLLBACK;

include/c_common/e_report.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
4141
* ~~~~{.c}
4242
* std::ostringstream log;
4343
* log << "the message";
44-
* *log_msg = pgr_msg(log.str().c_str());
44+
* *log_msg = to_pg_msg(log.str());
4545
* ~~~~
4646
*
4747
*

include/cpp_common/alloc.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3333

3434

3535
#include <string>
36+
#include <sstream>
3637

3738
extern "C" {
3839

@@ -86,8 +87,8 @@ pgr_free(T* ptr) {
8687
return nullptr;
8788
}
8889

89-
char *
90-
pgr_msg(const std::string &msg);
90+
char* to_pg_msg(const std::string&);
91+
char* to_pg_msg(const std::ostringstream&);
9192

9293
} // namespace pgrouting
9394

src/allpairs/floydWarshall_driver.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pgr_do_floydWarshall(
4949
size_t *return_count,
5050
char ** log_msg,
5151
char ** err_msg) {
52-
using pgrouting::pgr_msg;
52+
using pgrouting::to_pg_msg;
5353
using pgrouting::pgr_free;
5454

5555
std::ostringstream log;
@@ -85,35 +85,33 @@ pgr_do_floydWarshall(
8585

8686
if (*return_count == 0) {
8787
err << "No result generated, report this error\n";
88-
*err_msg = pgr_msg(err.str().c_str());
88+
*err_msg = to_pg_msg(err);
8989
*return_tuples = NULL;
9090
*return_count = 0;
9191
return;
9292
}
9393

94-
*log_msg = log.str().empty()?
95-
*log_msg :
96-
pgr_msg(log.str().c_str());
94+
*log_msg = to_pg_msg(log);
9795
} catch (AssertFailedException &except) {
9896
(*return_tuples) = pgr_free(*return_tuples);
9997
(*return_count) = 0;
10098
err << except.what();
101-
*err_msg = pgr_msg(err.str().c_str());
102-
*log_msg = pgr_msg(log.str().c_str());
99+
*err_msg = to_pg_msg(err);
100+
*log_msg = to_pg_msg(log);
103101
} catch (const std::string &ex) {
104-
*err_msg = pgr_msg(ex.c_str());
105-
*log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str());
102+
*err_msg = to_pg_msg(ex);
103+
*log_msg = hint? to_pg_msg(hint) : to_pg_msg(log);
106104
} catch (std::exception &except) {
107105
(*return_tuples) = pgr_free(*return_tuples);
108106
(*return_count) = 0;
109107
err << except.what();
110-
*err_msg = pgr_msg(err.str().c_str());
111-
*log_msg = pgr_msg(log.str().c_str());
108+
*err_msg = to_pg_msg(err);
109+
*log_msg = to_pg_msg(log);
112110
} catch(...) {
113111
(*return_tuples) = pgr_free(*return_tuples);
114112
(*return_count) = 0;
115113
err << "Caught unknown exception!";
116-
*err_msg = pgr_msg(err.str().c_str());
117-
*log_msg = pgr_msg(log.str().c_str());
114+
*err_msg = to_pg_msg(err);
115+
*log_msg = to_pg_msg(log);
118116
}
119117
}

src/allpairs/johnson_driver.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pgr_do_johnson(
4848
size_t *return_count,
4949
char ** log_msg,
5050
char ** err_msg) {
51-
using pgrouting::pgr_msg;
51+
using pgrouting::to_pg_msg;
5252
using pgrouting::pgr_free;
5353

5454
std::ostringstream log;
@@ -84,35 +84,33 @@ pgr_do_johnson(
8484

8585
if (*return_count == 0) {
8686
err << "No result generated, report this error\n";
87-
*err_msg = pgr_msg(err.str().c_str());
87+
*err_msg = to_pg_msg(err);
8888
*return_tuples = NULL;
8989
*return_count = 0;
9090
return;
9191
}
9292

93-
*log_msg = log.str().empty()?
94-
*log_msg :
95-
pgr_msg(log.str().c_str());
93+
*log_msg = to_pg_msg(log);
9694
} catch (AssertFailedException &except) {
9795
(*return_tuples) = pgr_free(*return_tuples);
9896
(*return_count) = 0;
9997
err << except.what();
100-
*err_msg = pgr_msg(err.str().c_str());
101-
*log_msg = pgr_msg(log.str().c_str());
98+
*err_msg = to_pg_msg(err);
99+
*log_msg = to_pg_msg(log);
102100
} catch (const std::string &ex) {
103-
*err_msg = pgr_msg(ex.c_str());
104-
*log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str());
101+
*err_msg = to_pg_msg(ex);
102+
*log_msg = hint? to_pg_msg(hint) : to_pg_msg(log);
105103
} catch (std::exception &except) {
106104
(*return_tuples) = pgr_free(*return_tuples);
107105
(*return_count) = 0;
108106
err << except.what();
109-
*err_msg = pgr_msg(err.str().c_str());
110-
*log_msg = pgr_msg(log.str().c_str());
107+
*err_msg = to_pg_msg(err);
108+
*log_msg = to_pg_msg(log);
111109
} catch(...) {
112110
(*return_tuples) = pgr_free(*return_tuples);
113111
(*return_count) = 0;
114112
err << "Caught unknown exception!";
115-
*err_msg = pgr_msg(err.str().c_str());
116-
*log_msg = pgr_msg(log.str().c_str());
113+
*err_msg = to_pg_msg(err);
114+
*log_msg = to_pg_msg(log);
117115
}
118116
}

src/alpha_shape/alphaShape_driver.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pgr_do_alphaShape(
7373
char **notice_msg,
7474
char **err_msg) {
7575
using pgrouting::pgr_alloc;
76-
using pgrouting::pgr_msg;
76+
using pgrouting::to_pg_msg;
7777
using pgrouting::pgr_free;
7878

7979
std::ostringstream log;
@@ -180,46 +180,42 @@ pgr_do_alphaShape(
180180
*return_tuples = pgr_alloc(*return_count, (*return_tuples));
181181
std::stringstream ss;
182182
ss << "MULTIPOLYGON EMPTY";
183-
(*return_tuples)[0].geom = pgr_msg(ss.str().c_str());
183+
(*return_tuples)[0].geom = to_pg_msg(ss.str());
184184
} else {
185185
*return_count = results.size();
186186
*return_tuples = pgr_alloc(*return_count, (*return_tuples));
187187
size_t row = 0;
188188
for (const auto &r : results) {
189189
std::stringstream ss;
190190
ss << bg::wkt(r);
191-
(*return_tuples)[row].geom = pgr_msg(ss.str().c_str());
191+
(*return_tuples)[row].geom = to_pg_msg(ss.str());
192192
++row;
193193
}
194194
}
195195

196196

197-
*log_msg = log.str().empty()?
198-
*log_msg :
199-
pgr_msg(log.str().c_str());
200-
*notice_msg = notice.str().empty()?
201-
*notice_msg :
202-
pgr_msg(notice.str().c_str());
197+
*log_msg = to_pg_msg(log);
198+
*notice_msg = to_pg_msg(notice);
203199
} catch (AssertFailedException &except) {
204200
(*return_tuples) = pgr_free(*return_tuples);
205201
(*return_count) = 0;
206202
err << except.what();
207-
*err_msg = pgr_msg(err.str().c_str());
208-
*log_msg = pgr_msg(log.str().c_str());
203+
*err_msg = to_pg_msg(err);
204+
*log_msg = to_pg_msg(log);
209205
} catch (const std::string &ex) {
210-
*err_msg = pgr_msg(ex.c_str());
211-
*log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str());
206+
*err_msg = to_pg_msg(ex);
207+
*log_msg = hint? to_pg_msg(hint) : to_pg_msg(log);
212208
} catch (std::exception &except) {
213209
(*return_tuples) = pgr_free(*return_tuples);
214210
(*return_count) = 0;
215211
err << except.what();
216-
*err_msg = pgr_msg(err.str().c_str());
217-
*log_msg = pgr_msg(log.str().c_str());
212+
*err_msg = to_pg_msg(err);
213+
*log_msg = to_pg_msg(log);
218214
} catch(...) {
219215
(*return_tuples) = pgr_free(*return_tuples);
220216
(*return_count) = 0;
221217
err << "Caught unknown exception!";
222-
*err_msg = pgr_msg(err.str().c_str());
223-
*log_msg = pgr_msg(log.str().c_str());
218+
*err_msg = to_pg_msg(err);
219+
*log_msg = to_pg_msg(log);
224220
}
225221
}

src/astar/astar_driver.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void pgr_do_astar(
6161
char** log_msg, char** notice_msg, char** err_msg) {
6262
using pgrouting::Path;
6363
using pgrouting::pgr_alloc;
64-
using pgrouting::pgr_msg;
64+
using pgrouting::to_pg_msg;
6565
using pgrouting::pgr_free;
6666
using pgrouting::utilities::get_combinations;
6767

@@ -85,8 +85,8 @@ void pgr_do_astar(
8585
hint = nullptr;
8686

8787
if (combinations.empty() && combinations_sql) {
88-
*notice_msg = pgr_msg("No (source, target) pairs found");
89-
*log_msg = pgr_msg(combinations_sql);
88+
*notice_msg = to_pg_msg("No (source, target) pairs found");
89+
*log_msg = to_pg_msg(combinations_sql);
9090
return;
9191
}
9292

@@ -96,8 +96,8 @@ void pgr_do_astar(
9696
hint = nullptr;
9797

9898
if (edges.empty()) {
99-
*notice_msg = pgr_msg("No edges found");
100-
*log_msg = pgr_msg(edges_sql);
99+
*notice_msg = to_pg_msg("No edges found");
100+
*log_msg = to_pg_msg(edges_sql);
101101
return;
102102
}
103103

@@ -128,39 +128,35 @@ void pgr_do_astar(
128128
(*return_tuples) = nullptr;
129129
(*return_count) = 0;
130130
notice << "No paths found\n";
131-
*log_msg = pgr_msg(notice.str().c_str());
131+
*log_msg = to_pg_msg(notice);
132132
return;
133133
}
134134

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

138-
*log_msg = log.str().empty()?
139-
*log_msg :
140-
pgr_msg(log.str().c_str());
141-
*notice_msg = notice.str().empty()?
142-
*notice_msg :
143-
pgr_msg(notice.str().c_str());
138+
*log_msg = to_pg_msg(log);
139+
*notice_msg = to_pg_msg(notice);
144140
} catch (AssertFailedException &except) {
145141
(*return_tuples) = pgr_free(*return_tuples);
146142
(*return_count) = 0;
147143
err << except.what();
148-
*err_msg = pgr_msg(err.str().c_str());
149-
*log_msg = pgr_msg(log.str().c_str());
144+
*err_msg = to_pg_msg(err);
145+
*log_msg = to_pg_msg(log);
150146
} catch (const std::string &ex) {
151-
*err_msg = pgr_msg(ex.c_str());
152-
*log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str());
147+
*err_msg = to_pg_msg(ex);
148+
*log_msg = hint? to_pg_msg(hint) : to_pg_msg(log);
153149
} catch (std::exception &except) {
154150
(*return_tuples) = pgr_free(*return_tuples);
155151
(*return_count) = 0;
156152
err << except.what();
157-
*err_msg = pgr_msg(err.str().c_str());
158-
*log_msg = pgr_msg(log.str().c_str());
153+
*err_msg = to_pg_msg(err);
154+
*log_msg = to_pg_msg(log);
159155
} catch(...) {
160156
(*return_tuples) = pgr_free(*return_tuples);
161157
(*return_count) = 0;
162158
err << "Caught unknown exception!";
163-
*err_msg = pgr_msg(err.str().c_str());
164-
*log_msg = pgr_msg(log.str().c_str());
159+
*err_msg = to_pg_msg(err);
160+
*log_msg = to_pg_msg(log);
165161
}
166162
}

src/bdAstar/bdAstar_driver.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void pgr_do_bdAstar(
6262
char** log_msg, char** notice_msg, char** err_msg) {
6363
using pgrouting::Path;
6464
using pgrouting::pgr_alloc;
65-
using pgrouting::pgr_msg;
65+
using pgrouting::to_pg_msg;
6666
using pgrouting::pgr_free;
6767
using pgrouting::utilities::get_combinations;
6868

@@ -83,17 +83,17 @@ void pgr_do_bdAstar(
8383
hint = nullptr;
8484

8585
if (combinations.empty() && combinations_sql) {
86-
*notice_msg = pgr_msg("No (source, target) pairs found");
87-
*log_msg = pgr_msg(combinations_sql);
86+
*notice_msg = to_pg_msg("No (source, target) pairs found");
87+
*log_msg = to_pg_msg(combinations_sql);
8888
return;
8989
}
9090

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

9494
if (edges.empty()) {
95-
*notice_msg = pgr_msg("No edges found");
96-
*log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str());
95+
*notice_msg = to_pg_msg("No edges found");
96+
*log_msg = hint? to_pg_msg(hint) : to_pg_msg(log);
9797
return;
9898
}
9999
hint = nullptr;
@@ -118,39 +118,35 @@ void pgr_do_bdAstar(
118118
(*return_tuples) = nullptr;
119119
(*return_count) = 0;
120120
notice << "No paths found\n";
121-
*log_msg = pgr_msg(notice.str().c_str());
121+
*log_msg = to_pg_msg(notice);
122122
return;
123123
}
124124

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

128-
*log_msg = log.str().empty()?
129-
*log_msg :
130-
pgr_msg(log.str().c_str());
131-
*notice_msg = notice.str().empty()?
132-
*notice_msg :
133-
pgr_msg(notice.str().c_str());
128+
*log_msg = to_pg_msg(log);
129+
*notice_msg = to_pg_msg(notice);
134130
} catch (AssertFailedException &except) {
135131
(*return_tuples) = pgr_free(*return_tuples);
136132
(*return_count) = 0;
137133
err << except.what();
138-
*err_msg = pgr_msg(err.str().c_str());
139-
*log_msg = pgr_msg(log.str().c_str());
134+
*err_msg = to_pg_msg(err);
135+
*log_msg = to_pg_msg(log);
140136
} catch (const std::string &ex) {
141-
*err_msg = pgr_msg(ex.c_str());
142-
*log_msg = hint? pgr_msg(hint) : pgr_msg(log.str().c_str());
137+
*err_msg = to_pg_msg(ex);
138+
*log_msg = hint? to_pg_msg(hint) : to_pg_msg(log);
143139
} catch (std::exception &except) {
144140
(*return_tuples) = pgr_free(*return_tuples);
145141
(*return_count) = 0;
146142
err << except.what();
147-
*err_msg = pgr_msg(err.str().c_str());
148-
*log_msg = pgr_msg(log.str().c_str());
143+
*err_msg = to_pg_msg(err);
144+
*log_msg = to_pg_msg(log);
149145
} catch(...) {
150146
(*return_tuples) = pgr_free(*return_tuples);
151147
(*return_count) = 0;
152148
err << "Caught unknown exception!";
153-
*err_msg = pgr_msg(err.str().c_str());
154-
*log_msg = pgr_msg(log.str().c_str());
149+
*err_msg = to_pg_msg(err);
150+
*log_msg = to_pg_msg(log);
155151
}
156152
}

0 commit comments

Comments
 (0)