Skip to content

Commit

Permalink
Use PyBytes_AsStringAndSize()
Browse files Browse the repository at this point in the history
Let Python give us the length of the "bytes" it already knows, instead
of doing an strlen(). This improves performance a bit.
  • Loading branch information
spbnick committed Sep 15, 2020
1 parent c7b9332 commit efdfee0
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions jq.pyx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import threading

from cpython.bytes cimport PyBytes_AsString
from cpython.bytes cimport PyBytes_AsStringAndSize


cdef extern from "jv.h":
Expand Down Expand Up @@ -161,10 +161,11 @@ cdef class _Parser(object):

cdef bint _ready_next_bytes(self) except 1:
cdef char* cbytes
cdef ssize_t clen
try:
self._bytes = next(self._text_iter).encode("utf8")
cbytes = PyBytes_AsString(self._bytes)
jv_parser_set_buf(self._parser, cbytes, len(cbytes), 1)
PyBytes_AsStringAndSize(self._bytes, &cbytes, &clen)
jv_parser_set_buf(self._parser, cbytes, clen, 1)
except StopIteration:
self._bytes = None
jv_parser_set_buf(self._parser, "", 0, 0)
Expand Down Expand Up @@ -349,8 +350,10 @@ cdef class _ResultIterator(object):
self._bytes_input = bytes_input
self._ready = False
cdef jv_parser* parser = jv_parser_new(0)
cdef char* cbytes_input = PyBytes_AsString(bytes_input)
jv_parser_set_buf(parser, cbytes_input, len(cbytes_input), 0)
cdef char* cbytes_input
cdef ssize_t clen_input
PyBytes_AsStringAndSize(bytes_input, &cbytes_input, &clen_input)
jv_parser_set_buf(parser, cbytes_input, clen_input, 0)
self._parser = parser

def __iter__(self):
Expand Down

0 comments on commit efdfee0

Please sign in to comment.