mirror of
https://github.com/micropython/micropython.git
synced 2026-01-04 19:20:22 +01:00
py: Add mp_obj_new_str_from_vstr, and use it where relevant.
This patch allows to reuse vstr memory when creating str/bytes object. This improves memory usage. Also saves code ROM: 128 bytes on stmhal, 92 bytes on bare-arm, and 88 bytes on unix x64.
This commit is contained in:
20
py/vstr.c
20
py/vstr.c
@@ -136,26 +136,6 @@ char *vstr_extend(vstr_t *vstr, size_t size) {
|
||||
return p;
|
||||
}
|
||||
|
||||
// Shrink vstr to be given size
|
||||
bool vstr_set_size(vstr_t *vstr, size_t size) {
|
||||
if (vstr->fixed_buf) {
|
||||
return false;
|
||||
}
|
||||
char *new_buf = m_renew(char, vstr->buf, vstr->alloc, size);
|
||||
if (new_buf == NULL) {
|
||||
vstr->had_error = true;
|
||||
return false;
|
||||
}
|
||||
vstr->buf = new_buf;
|
||||
vstr->alloc = vstr->len = size;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Shrink vstr allocation to its actual length
|
||||
bool vstr_shrink(vstr_t *vstr) {
|
||||
return vstr_set_size(vstr, vstr->len);
|
||||
}
|
||||
|
||||
STATIC bool vstr_ensure_extra(vstr_t *vstr, size_t size) {
|
||||
if (vstr->len + size + 1 > vstr->alloc) {
|
||||
if (vstr->fixed_buf) {
|
||||
|
||||
Reference in New Issue
Block a user