Skip to content

Commit e268cdd

Browse files
committed
Optimize string generation function
1 parent 76b21e4 commit e268cdd

3 files changed

Lines changed: 29 additions & 66 deletions

File tree

src/Handler.cpp

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -660,22 +660,7 @@ bool Handler::preHandleRequest(Request* req, const String& key)
660660

661661
// Debug log: cursor received from client
662662
char cursorBuf[64];
663-
int cursorLen = 0;
664-
if (cursor == 0) {
665-
cursorBuf[cursorLen++] = '0';
666-
} else {
667-
char temp[64];
668-
int tempLen = 0;
669-
__uint128_t c = cursor;
670-
while (c > 0) {
671-
temp[tempLen++] = '0' + (c % 10);
672-
c /= 10;
673-
}
674-
for (int i = tempLen - 1; i >= 0; i--) {
675-
cursorBuf[cursorLen++] = temp[i];
676-
}
677-
}
678-
cursorBuf[cursorLen] = '\0';
663+
Util::uint128ToString(cursor, cursorBuf);
679664

680665
int groupIdx = (unsigned long)(cursor & Const::ServGroupMask);
681666
auto g = sp->getGroup(groupIdx);
@@ -701,22 +686,7 @@ bool Handler::preHandleRequest(Request* req, const String& key)
701686

702687
// Debug log: cursor to be sent to backend server
703688
char actualCursorBuf[64];
704-
int actualLen = 0;
705-
if (actualCursor == 0) {
706-
actualCursorBuf[actualLen++] = '0';
707-
} else {
708-
char temp[64];
709-
int tempLen = 0;
710-
__uint128_t c = actualCursor;
711-
while (c > 0) {
712-
temp[tempLen++] = '0' + (c % 10);
713-
c /= 10;
714-
}
715-
for (int i = tempLen - 1; i >= 0; i--) {
716-
actualCursorBuf[actualLen++] = temp[i];
717-
}
718-
}
719-
actualCursorBuf[actualLen] = '\0';
689+
Util::uint128ToString(actualCursor, actualCursorBuf);
720690

721691
logDebug("h %d SCAN cursor from client: %s, group: %d, cursor to server: %s",
722692
id(), cursorBuf, groupIdx, actualCursorBuf);
@@ -901,23 +871,7 @@ void Handler::handleResponse(ConnectConnection* s, Request* req, Response* res)
901871
if ((p = strchr(p, '*')) != nullptr) {
902872
// Convert 128-bit integer to string
903873
char buf[64]; // 128-bit needs at most 39 decimal digits
904-
int n = 0;
905-
if (cursor == 0) {
906-
buf[n++] = '0';
907-
} else {
908-
char temp[64];
909-
int tempLen = 0;
910-
__uint128_t c = cursor;
911-
while (c > 0) {
912-
temp[tempLen++] = '0' + (c % 10);
913-
c /= 10;
914-
}
915-
// Reverse digits (extracted from low to high)
916-
for (int i = tempLen - 1; i >= 0; i--) {
917-
buf[n++] = temp[i];
918-
}
919-
}
920-
buf[n] = '\0';
874+
int n = Util::uint128ToString(cursor, buf);
921875

922876
// Debug log: cursor to be sent to client
923877
logDebug("h %d SCAN cursor from server: %s, group: %d, cursor to client: %s",

src/Request.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -249,23 +249,7 @@ void Request::setSentinelSlaves(const String& master)
249249
void Request::adjustScanCursor(__uint128_t cursor)
250250
{
251251
char buf[64]; // 128-bit integer needs at most 39 decimal digits
252-
int n = 0;
253-
if (cursor == 0) {
254-
buf[n++] = '0';
255-
} else {
256-
char temp[64];
257-
int tempLen = 0;
258-
__uint128_t c = cursor;
259-
while (c > 0) {
260-
temp[tempLen++] = '0' + (c % 10);
261-
c /= 10;
262-
}
263-
// Reverse digits (extracted from low to high)
264-
for (int i = tempLen - 1; i >= 0; i--) {
265-
buf[n++] = temp[i];
266-
}
267-
}
268-
buf[n] = '\0';
252+
int n = Util::uint128ToString(cursor, buf);
269253
if (mHead.empty()) {
270254
SegmentStr<64> str(mReq);
271255
const char* p = strchr(str.data(), '$');

src/Util.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,31 @@ namespace Util
8181
{
8282
return duration_cast<microseconds>(steady_clock::now().time_since_epoch()).count();
8383
}
84+
85+
// Convert 128-bit unsigned integer to string
86+
// Returns the length of the converted string (excluding null terminator)
87+
// The buffer must be at least 40 bytes (39 digits + null terminator for max __uint128_t value)
88+
inline int uint128ToString(__uint128_t value, char* buf)
89+
{
90+
int n = 0;
91+
if (value == 0) {
92+
buf[n++] = '0';
93+
} else {
94+
char temp[64];
95+
int tempLen = 0;
96+
__uint128_t v = value;
97+
while (v > 0) {
98+
temp[tempLen++] = '0' + (v % 10);
99+
v /= 10;
100+
}
101+
// Reverse digits (extracted from low to high)
102+
for (int i = tempLen - 1; i >= 0; i--) {
103+
buf[n++] = temp[i];
104+
}
105+
}
106+
buf[n] = '\0';
107+
return n;
108+
}
84109
};
85110

86111
#endif

0 commit comments

Comments
 (0)