Skip to content

Commit 29c4f92

Browse files
committed
objexcept: Optimize using messages without formatting substitutions.
They are directly cast to str object, skipping allocation of formatting buffer.
1 parent 3077fbf commit 29c4f92

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

py/objexcept.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -349,15 +349,19 @@ mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char
349349
// no message
350350
assert(0);
351351
} else {
352-
// render exception message and store as .args[0]
353-
// TODO: optimize bufferbloat
354-
vstr_t vstr;
355-
vstr_init(&vstr, 16);
356-
va_list ap;
357-
va_start(ap, fmt);
358-
vstr_vprintf(&vstr, fmt, ap);
359-
va_end(ap);
360-
o->args->items[0] = mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
352+
if (strchr(fmt, '%') == NULL) {
353+
// no formatting substitutions, avoid allocating vstr.
354+
o->args->items[0] = mp_obj_new_str(fmt, strlen(fmt), false);
355+
} else {
356+
// render exception message and store as .args[0]
357+
va_list ap;
358+
vstr_t vstr;
359+
vstr_init(&vstr, 16);
360+
va_start(ap, fmt);
361+
vstr_vprintf(&vstr, fmt, ap);
362+
va_end(ap);
363+
o->args->items[0] = mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
364+
}
361365
}
362366
}
363367

0 commit comments

Comments
 (0)