diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2015-02-15 22:19:30 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2015-02-15 22:41:14 +0300 |
commit | 29c4f92e13f27a6fabf7556184325c2483ace358 (patch) | |
tree | 6b18f5378cbc5917becbfcf207fc26c8485208a6 /py/objexcept.c | |
parent | 3077fbff262fd64b5e71c8f7a4e33e0c0c949ca5 (diff) | |
download | micropython-29c4f92e13f27a6fabf7556184325c2483ace358.tar.gz micropython-29c4f92e13f27a6fabf7556184325c2483ace358.zip |
objexcept: Optimize using messages without formatting substitutions.
They are directly cast to str object, skipping allocation of formatting
buffer.
Diffstat (limited to 'py/objexcept.c')
-rw-r--r-- | py/objexcept.c | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/py/objexcept.c b/py/objexcept.c index f2f4012bc7..a8b81448ec 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -349,15 +349,19 @@ mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char // no message assert(0); } else { - // render exception message and store as .args[0] - // TODO: optimize bufferbloat - vstr_t vstr; - vstr_init(&vstr, 16); - va_list ap; - va_start(ap, fmt); - vstr_vprintf(&vstr, fmt, ap); - va_end(ap); - o->args->items[0] = mp_obj_new_str_from_vstr(&mp_type_str, &vstr); + if (strchr(fmt, '%') == NULL) { + // no formatting substitutions, avoid allocating vstr. + o->args->items[0] = mp_obj_new_str(fmt, strlen(fmt), false); + } else { + // render exception message and store as .args[0] + va_list ap; + vstr_t vstr; + vstr_init(&vstr, 16); + va_start(ap, fmt); + vstr_vprintf(&vstr, fmt, ap); + va_end(ap); + o->args->items[0] = mp_obj_new_str_from_vstr(&mp_type_str, &vstr); + } } } |