Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add big int format to msgpack.pack #7811

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 34 additions & 15 deletions shared-module/msgpack/__init__.c
Expand Up @@ -30,6 +30,7 @@
#include "py/obj.h"
#include "py/binary.h"
#include "py/objarray.h"
#include "py/objint.h"
#include "py/objlist.h"
#include "py/objstringio.h"
#include "py/parsenum.h"
Expand Down Expand Up @@ -195,19 +196,34 @@ STATIC mp_map_elem_t *dict_iter_next(mp_obj_dict_t *dict, size_t *cur) {
return NULL;
}

STATIC void pack_int(msgpack_stream_t *s, int32_t x) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function works as is for pack_small_int. I wouldn't bother trying to optimize for 1 byte of packing for some int values.

if (x > -32 && x < 128) {
write1(s, x);
} else if ((int8_t)x == x) {
write1(s, 0xd0);
write1(s, x);
} else if ((int16_t)x == x) {
write1(s, 0xd1);
write2(s, x);
STATIC void pack_int(msgpack_stream_t *s, mp_obj_t obj, bool _signed) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity I would split this into 2 functions for the two cases:

STATIC void pack_small_int(msgpack_stream_t *s, mp_int_t value)
STATIC void pack_int(msgpack_stream_t *s, mp_obj_t obj)

byte buffer[9];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module is already setup with write functions to write its output. It seems unnecessary to add another layer of indirection (buffer) to write the output.

byte *buf = (buffer + 1);
byte *type = buffer;
size_t len = 0;
if (mp_obj_is_small_int(obj)) {
int32_t x = MP_OBJ_SMALL_INT_VALUE(obj);
if (x > -32 && x < 128) {
write1(s, x);
return;
} else if (-0x80 <= x && x <= 0xff) {
*type = _signed ? 0xd0 : 0xcc;
len = 1;
} else if (-0x8000 <= x && x <= 0xffff) {
*type = _signed ? 0xd1 : 0xcd;
len = 2;
} else {
*type = _signed ? 0xd2 : 0xce;
len = 4;
}
mp_binary_set_int(len, true, buf, x);
} else {
write1(s, 0xd2);
write4(s, x);
// todo: encode remaining 32 bit values as 0xd2/0xce ?
*type = _signed ? 0xd3 : 0xcf;
len = 8;
mp_obj_int_to_bytes_impl(obj, true, len, buf);
}
write(s, buffer, len + 1);
}

STATIC void pack_bin(msgpack_stream_t *s, const uint8_t *data, size_t len) {
Expand Down Expand Up @@ -275,10 +291,13 @@ STATIC void pack_dict(msgpack_stream_t *s, size_t len) {
}

STATIC void pack(mp_obj_t obj, msgpack_stream_t *s, mp_obj_t default_handler) {
if (mp_obj_is_small_int(obj)) {
// int
int32_t x = MP_OBJ_SMALL_INT_VALUE(obj);
pack_int(s, x);
if (mp_obj_is_int(obj)) {
// big int
// encode signed or unsigned
bool _signed = mp_obj_int_sign(obj) < 0;
// raise if overflow
mp_obj_int_buffer_overflow_check(obj, 8, _signed);
pack_int(s, obj, _signed);
} else if (mp_obj_is_str(obj)) {
// string
size_t len;
Expand Down