Skip to content

Commit 1cf3225

Browse files
committed
Use PyBytes_AsStringAndSize()
Let Python give us the length of the "bytes" it already knows, instead of doing an strlen(). This improves performance a bit.
1 parent fb1e58b commit 1cf3225

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

jq.pyx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
import threading
33

4-
from cpython.bytes cimport PyBytes_AsString
4+
from cpython.bytes cimport PyBytes_AsStringAndSize
55

66

77
cdef extern from "jv.h":
@@ -161,10 +161,11 @@ cdef class _JSONParser(object):
161161

162162
cdef bint _ready_next_bytes(self) except 1:
163163
cdef char* cbytes
164+
cdef ssize_t clen
164165
try:
165166
self._bytes = next(self._text_iter).encode("utf8")
166-
cbytes = PyBytes_AsString(self._bytes)
167-
jv_parser_set_buf(self._parser, cbytes, len(cbytes), 1)
167+
PyBytes_AsStringAndSize(self._bytes, &cbytes, &clen)
168+
jv_parser_set_buf(self._parser, cbytes, clen, 1)
168169
except StopIteration:
169170
self._bytes = None
170171
jv_parser_set_buf(self._parser, "", 0, 0)
@@ -349,8 +350,10 @@ cdef class _ResultIterator(object):
349350
self._bytes_input = bytes_input
350351
self._ready = False
351352
cdef jv_parser* parser = jv_parser_new(0)
352-
cdef char* cbytes_input = PyBytes_AsString(bytes_input)
353-
jv_parser_set_buf(parser, cbytes_input, len(cbytes_input), 0)
353+
cdef char* cbytes_input
354+
cdef ssize_t clen_input
355+
PyBytes_AsStringAndSize(bytes_input, &cbytes_input, &clen_input)
356+
jv_parser_set_buf(parser, cbytes_input, clen_input, 0)
354357
self._parser = parser
355358

356359
def __iter__(self):

0 commit comments

Comments
 (0)