diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2017-01-27 00:40:47 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2017-01-27 00:49:39 +0300 |
commit | e2e663291dd1408cc9c5118d258a568470e32f8c (patch) | |
tree | c0e4419089fbedc97b91d530becaa54857606ea0 /py/objstr.c | |
parent | 221f88d1f3f67640e72912ad8473782360b0e306 (diff) | |
download | micropython-e2e663291dd1408cc9c5118d258a568470e32f8c.tar.gz micropython-e2e663291dd1408cc9c5118d258a568470e32f8c.zip |
py/objstr: Optimize string concatenation with empty string.
In this, don't allocate copy, just return non-empty string. This helps
with a standard pattern of buffering data in case of short reads:
buf = b""
while ...:
s = f.read(...)
buf += s
...
For a typical case when single read returns all data needed, there won't
be extra allocation. This optimization helps uasyncio.
Diffstat (limited to 'py/objstr.c')
-rw-r--r-- | py/objstr.c | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/py/objstr.c b/py/objstr.c index f082e95591..7478198e02 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -358,6 +358,13 @@ mp_obj_t mp_obj_str_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { switch (op) { case MP_BINARY_OP_ADD: case MP_BINARY_OP_INPLACE_ADD: { + if (lhs_len == 0) { + return rhs_in; + } + if (rhs_len == 0) { + return lhs_in; + } + vstr_t vstr; vstr_init_len(&vstr, lhs_len + rhs_len); memcpy(vstr.buf, lhs_data, lhs_len); |