Skip to content

Commit 04353cc

Browse files
committed
py: With obj repr "C", change raw str accessor from macro to function.
This saves around 1000 bytes (Thumb2 arch) because in repr "C" it is costly to check and extract a qstr. So making such check/extract a function instead of a macro saves lots of code space.
1 parent 183edef commit 04353cc

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

py/objstr.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,6 +2038,17 @@ const char *mp_obj_str_get_data(mp_obj_t self_in, mp_uint_t *len) {
20382038
}
20392039
}
20402040

2041+
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
2042+
const byte *mp_obj_str_get_data_no_check(mp_obj_t self_in, mp_uint_t *len) {
2043+
if (MP_OBJ_IS_QSTR(self_in)) {
2044+
return qstr_data(MP_OBJ_QSTR_VALUE(self_in), len);
2045+
} else {
2046+
*len = ((mp_obj_str_t*)self_in)->len;
2047+
return ((mp_obj_str_t*)self_in)->data;
2048+
}
2049+
}
2050+
#endif
2051+
20412052
/******************************************************************************/
20422053
/* str iterator */
20432054

py/objstr.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,16 @@ typedef struct _mp_obj_str_t {
4949
{ str_len = qstr_len(MP_OBJ_QSTR_VALUE(str_obj_in)); } else { str_len = ((mp_obj_str_t*)str_obj_in)->len; }
5050

5151
// use this macro to extract the string data and length
52+
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
53+
const byte *mp_obj_str_get_data_no_check(mp_obj_t self_in, mp_uint_t *len);
54+
#define GET_STR_DATA_LEN(str_obj_in, str_data, str_len) \
55+
mp_uint_t str_len; const byte *str_data = mp_obj_str_get_data_no_check(str_obj_in, &str_len);
56+
#else
5257
#define GET_STR_DATA_LEN(str_obj_in, str_data, str_len) \
5358
const byte *str_data; mp_uint_t str_len; if (MP_OBJ_IS_QSTR(str_obj_in)) \
5459
{ str_data = qstr_data(MP_OBJ_QSTR_VALUE(str_obj_in), &str_len); } \
5560
else { str_len = ((mp_obj_str_t*)str_obj_in)->len; str_data = ((mp_obj_str_t*)str_obj_in)->data; }
61+
#endif
5662

5763
mp_obj_t mp_obj_str_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args);
5864
void mp_str_print_json(const mp_print_t *print, const byte *str_data, mp_uint_t str_len);

0 commit comments

Comments
 (0)