Skip to content

Commit 7a3a5c7

Browse files
committed
Optimize hot path: replace std::string with char[] to avoid heap
1 parent 61ba182 commit 7a3a5c7

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

include/gen_utils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ inline constexpr bool fast_isspace(unsigned char c) noexcept
406406
return (c == ' ') | (static_cast<unsigned char>(c - '\t') < 5);
407407
}
408408

409-
inline constexpr char* fast_uint32toa(uint32_t value, char* out) {
409+
inline constexpr char* fast_uint32toa(uint32_t value, char* out) noexcept {
410410
char* p = out;
411411
do {
412412
*p++ = '0' + (value % 10);
@@ -423,4 +423,4 @@ inline constexpr char* fast_uint32toa(uint32_t value, char* out) {
423423
return p;
424424
}
425425

426-
#endif /* __GEN_FUNCTIONS */
426+
#endif /* __GEN_FUNCTIONS */

lib/PgSQL_Session.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,6 +2582,13 @@ void PgSQL_Session::handler_minus1_HandleBackendConnection(PgSQL_Data_Stream* my
25822582
}
25832583
}
25842584

2585+
inline void build_backend_stmt_name(char* buf, unsigned int stmt_backend_id) {
2586+
char* p = buf;
2587+
const char* prefix = PROXYSQL_PS_PREFIX;
2588+
while (*prefix) *p++ = *prefix++;
2589+
p = fast_uint32toa(stmt_backend_id, p);
2590+
}
2591+
25852592
// this function was inline
25862593
int PgSQL_Session::RunQuery(PgSQL_Data_Stream* myds, PgSQL_Connection* myconn) {
25872594
PROXY_TRACE2();
@@ -2599,9 +2606,10 @@ int PgSQL_Session::RunQuery(PgSQL_Data_Stream* myds, PgSQL_Connection* myconn) {
25992606
this, myconn, myconn->pgsql_conn, backend_stmt_id);
26002607
}
26012608
// this is used to generate the name of the prepared statement in the backend
2602-
const std::string& backend_stmt_name = std::string(PROXYSQL_PS_PREFIX) + std::to_string(CurrentQuery.extended_query_info.stmt_backend_id);
2609+
char backend_stmt_name[32];
2610+
build_backend_stmt_name(backend_stmt_name, CurrentQuery.extended_query_info.stmt_backend_id);
26032611
rc = myconn->async_query(myds->revents, (char*)CurrentQuery.QueryPointer, CurrentQuery.QueryLength,
2604-
backend_stmt_name.c_str(), PGSQL_EXTENDED_QUERY_TYPE_PARSE, &CurrentQuery.extended_query_info);
2612+
backend_stmt_name, PGSQL_EXTENDED_QUERY_TYPE_PARSE, &CurrentQuery.extended_query_info);
26052613
}
26062614
break;
26072615
case PROCESSING_STMT_DESCRIBE:
@@ -2610,9 +2618,10 @@ int PgSQL_Session::RunQuery(PgSQL_Data_Stream* myds, PgSQL_Connection* myconn) {
26102618
{
26112619
PgSQL_Extended_Query_Type type =
26122620
(status == PROCESSING_STMT_DESCRIBE) ? PGSQL_EXTENDED_QUERY_TYPE_DESCRIBE : PGSQL_EXTENDED_QUERY_TYPE_EXECUTE;
2613-
const std::string& backend_stmt_name =
2614-
std::string(PROXYSQL_PS_PREFIX) + std::to_string(CurrentQuery.extended_query_info.stmt_backend_id);
2615-
rc = myconn->async_query(myds->revents, nullptr, 0, backend_stmt_name.c_str(), type, &CurrentQuery.extended_query_info);
2621+
2622+
char backend_stmt_name[32];
2623+
build_backend_stmt_name(backend_stmt_name, CurrentQuery.extended_query_info.stmt_backend_id);
2624+
rc = myconn->async_query(myds->revents, nullptr, 0, backend_stmt_name, type, &CurrentQuery.extended_query_info);
26162625
}
26172626
break;
26182627
/* case PROCESSING_STMT_EXECUTE:

0 commit comments

Comments
 (0)