Skip to content

Commit 5972b4c

Browse files
committed
objstr: Switch from in-object string data to ptr to separate memory area.
This is pre-requisite for having efficient implementation of str<->bytes conversion, and having that efficient is required with unfortunare str vs bytes dichotomy in Python3.
1 parent 4290155 commit 5972b4c

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

py/objstr.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ typedef struct _mp_obj_str_t {
1414
mp_obj_base_t base;
1515
machine_uint_t hash : 16; // XXX here we assume the hash size is 16 bits (it is at the moment; see qstr.c)
1616
machine_uint_t len : 16; // len == number of bytes used in data, alloc = len + 1 because (at the moment) we also append a null byte
17-
byte data[];
17+
const byte *data;
1818
} mp_obj_str_t;
1919

2020
// use this macro to extract the string hash
@@ -636,28 +636,35 @@ const mp_obj_type_t bytes_type = {
636636
};
637637

638638
mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, uint len, byte **data) {
639-
mp_obj_str_t *o = m_new_obj_var(mp_obj_str_t, byte, len + 1);
639+
mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
640640
o->base.type = type;
641641
o->len = len;
642-
*data = o->data;
642+
byte *p = m_new(byte, len + 1);
643+
o->data = p;
644+
*data = p;
643645
return o;
644646
}
645647

646648
mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in) {
647649
assert(MP_OBJ_IS_STR(o_in));
648650
mp_obj_str_t *o = o_in;
649651
o->hash = qstr_compute_hash(o->data, o->len);
650-
o->data[o->len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
652+
byte *p = (byte*)o->data;
653+
p[o->len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
651654
return o;
652655
}
653656

654657
STATIC mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len) {
655-
mp_obj_str_t *o = m_new_obj_var(mp_obj_str_t, byte, len + 1);
658+
mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
656659
o->base.type = type;
657-
o->hash = qstr_compute_hash(data, len);
658660
o->len = len;
659-
memcpy(o->data, data, len * sizeof(byte));
660-
o->data[len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
661+
if (data) {
662+
o->hash = qstr_compute_hash(data, len);
663+
byte *p = m_new(byte, len + 1);
664+
o->data = p;
665+
memcpy(p, data, len * sizeof(byte));
666+
p[len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
667+
}
661668
return o;
662669
}
663670

0 commit comments

Comments
 (0)