Commit 9f76dcd6 authored by Dave Hylands's avatar Dave Hylands Committed by Damien George
Browse files

py: Prevent many extra vstr allocations.

I checked the entire codebase, and every place that vstr_init_len
was called, there was a call to mp_obj_new_str_from_vstr after it.

mp_obj_new_str_from_vstr always tries to reallocate a new buffer
1 byte larger than the original to store the terminating null
character.

In many cases, if we allocated the initial buffer to be 1 byte
longer, we can prevent this extra allocation, and just reuse
the originally allocated buffer.

Asking to read 256 bytes and only getting 100 will still cause
the extra allocation, but if you ask to read 256 and get 256
then the extra allocation will be optimized away.

Yes - the reallocation is optimized in the heap to try and reuse
the buffer if it can, but it takes quite a few cycles to figure
this out.

Note by Damien: vstr_init_len should now be considered as a
string-init convenience function and used only when creating
null-terminated objects.
parent ef7dd8db
......@@ -1922,7 +1922,11 @@ mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr) {
o->base.type = type;
o->len = vstr->len;
o->hash = qstr_compute_hash((byte*)vstr->buf, vstr->len);
o->data = (byte*)m_renew(char, vstr->buf, vstr->alloc, vstr->len + 1);
if (vstr->len + 1 == vstr->alloc) {
o->data = (byte*)vstr->buf;
} else {
o->data = (byte*)m_renew(char, vstr->buf, vstr->alloc, vstr->len + 1);
}
((byte*)o->data)[o->len] = '\0'; // add null byte
vstr->buf = NULL;
vstr->alloc = 0;
......
......@@ -52,9 +52,10 @@ void vstr_init(vstr_t *vstr, size_t alloc) {
vstr->fixed_buf = false;
}
// Init the vstr so it allocs exactly enough ram to hold given length, and set the length.
// Init the vstr so it allocs exactly enough ram to hold a null-terminated
// string of the given length, and set the length.
void vstr_init_len(vstr_t *vstr, size_t len) {
vstr_init(vstr, len);
vstr_init(vstr, len + 1);
vstr->len = len;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment