Skip to content

Commit 9ff0af1

Browse files
committed
refactor: Use stack buffer in round_trip function
Remove heap allocation (malloc/free) in round_trip by using stack-allocated buffer with PROCESSED_WORD_CAPACITY. This improves performance and simplifies memory management.
1 parent 9de4b2a commit 9ff0af1

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

pandas/_libs/src/parser/tokenizer.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,10 +1871,9 @@ double round_trip(const char *p, char **q, char decimal, char Py_UNUSED(sci),
18711871
char tsep, int skip_trailing, int *error, int *maybe_int) {
18721872
// 'normalize' representation to C-locale; replace decimal with '.' and
18731873
// remove thousands separator.
1874-
const size_t length = strlen(p);
1875-
char *pc = malloc(length + 1);
1874+
char pc[PROCESSED_WORD_CAPACITY];
18761875
char *endptr;
1877-
_str_copy_decimal_str_c(pc, length + 1, p, &endptr, decimal, tsep);
1876+
_str_copy_decimal_str_c(pc, sizeof(pc), p, &endptr, decimal, tsep);
18781877
// This is called from a nogil block in parsers.pyx
18791878
// so need to explicitly get GIL before Python calls
18801879
PyGILState_STATE gstate = PyGILState_Ensure();
@@ -1903,7 +1902,6 @@ double round_trip(const char *p, char **q, char decimal, char Py_UNUSED(sci),
19031902
PyErr_Clear();
19041903

19051904
PyGILState_Release(gstate);
1906-
free(pc);
19071905
if (skip_trailing && q != NULL && *q != p) {
19081906
while (isspace_ascii(**q)) {
19091907
(*q)++;

0 commit comments

Comments
 (0)