@@ -174,6 +174,12 @@ _check_read(BufferObject *data, Py_ssize_t need) {
174174 return 1 ;
175175}
176176
177+ /*
178+ bool format: single byte
179+ \x00 - False
180+ \x01 - True
181+ */
182+
177183static char
178184read_bool_internal (PyObject * data ) {
179185 if (_check_buffer (data ) == 2 )
@@ -233,6 +239,12 @@ write_bool(PyObject *self, PyObject *args, PyObject *kwds) {
233239 return Py_None ;
234240}
235241
242+ /*
243+ str format: size followed by UTF-8 bytes
244+ short strings (len <= 127): single byte for size as `(uint8_t)size << 1`
245+ long strings: \x01 followed by size as Py_ssize_t
246+ */
247+
236248static PyObject *
237249read_str_internal (PyObject * data ) {
238250 if (_check_buffer (data ) == 2 )
@@ -246,6 +258,7 @@ read_str_internal(PyObject *data) {
246258 uint8_t first = * (uint8_t * )(buf + ((BufferObject * )data )-> pos );
247259 ((BufferObject * )data )-> pos += 1 ;
248260 if (first != LONG_STR_TAG ) {
261+ // Common case: short string (len <= 127).
249262 size = (Py_ssize_t )(first >> 1 );
250263 } else {
251264 size = * (Py_ssize_t * )(buf + ((BufferObject * )data )-> pos );
@@ -328,6 +341,11 @@ write_str(PyObject *self, PyObject *args, PyObject *kwds) {
328341 return Py_None ;
329342}
330343
344+ /*
345+ float format:
346+ stored as a C double
347+ */
348+
331349static double
332350read_float_internal (PyObject * data ) {
333351 if (_check_buffer (data ) == 2 )
@@ -386,6 +404,13 @@ write_float(PyObject *self, PyObject *args, PyObject *kwds) {
386404 return Py_None ;
387405}
388406
407+ /*
408+ int format:
409+ most common values (-10 <= value <= 117): single byte as `(uint8_t)(value + 10) << 1`
410+ medium values (fit in CPyTagged): \x01 followed by CPyTagged value
411+ long values (very rare): \x03 followed by decimal string (see str format)
412+ */
413+
389414static CPyTagged
390415read_int_internal (PyObject * data ) {
391416 if (_check_buffer (data ) == 2 )
@@ -398,14 +423,14 @@ read_int_internal(PyObject *data) {
398423 uint8_t first = * (uint8_t * )(buf + ((BufferObject * )data )-> pos );
399424 ((BufferObject * )data )-> pos += 1 ;
400425 if ((first & MEDIUM_INT_TAG ) == 0 ) {
426+ // Most common case: int that is small in absolute value.
401427 return ((Py_ssize_t )(first >> 1 ) + MIN_SHORT_INT ) << 1 ;
402428 }
403429 if (first == MEDIUM_INT_TAG ) {
404430 CPyTagged ret = * (CPyTagged * )(buf + ((BufferObject * )data )-> pos );
405431 ((BufferObject * )data )-> pos += sizeof (CPyTagged );
406432 return ret ;
407433 }
408- // first == LONG_INT_TAG
409434 // People who have literal ints not fitting in size_t should be punished :-)
410435 PyObject * str_ret = read_str_internal (data );
411436 if (str_ret == NULL )
@@ -491,6 +516,11 @@ write_int(PyObject *self, PyObject *args, PyObject *kwds) {
491516 return Py_None ;
492517}
493518
519+ /*
520+ integer tag format (0 <= t <= 255):
521+ stored as a uint8_t
522+ */
523+
494524static uint8_t
495525read_tag_internal (PyObject * data ) {
496526 if (_check_buffer (data ) == 2 )
0 commit comments