py/obj: Optimise code size and performance for make_new as a slot.
The check for make_new (i.e. used to determine something's type) is now more complicated due to the slot access. This commit changes the inlining of a few frequently-used helpers to overall improve code size and performance.
This commit is contained in:
parent
94beeabd2e
commit
b41aaaa8a9
6
py/obj.h
6
py/obj.h
@ -521,6 +521,7 @@ typedef mp_obj_t (*mp_fun_kw_t)(size_t n, const mp_obj_t *, mp_map_t *);
|
||||
// mp_getiter_iternext_custom_t struct instance (with both .getiter and .iternext set).
|
||||
// If MP_TYPE_FLAG_ITER_IS_STREAM is set then the type implicitly gets a "return self"
|
||||
// getiter, and mp_stream_unbuffered_iter for iternext.
|
||||
// If MP_TYPE_FLAG_INSTANCE_TYPE is set then this is an instance type (i.e. defined in Python).
|
||||
#define MP_TYPE_FLAG_NONE (0x0000)
|
||||
#define MP_TYPE_FLAG_IS_SUBCLASSED (0x0001)
|
||||
#define MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS (0x0002)
|
||||
@ -533,6 +534,7 @@ typedef mp_obj_t (*mp_fun_kw_t)(size_t n, const mp_obj_t *, mp_map_t *);
|
||||
#define MP_TYPE_FLAG_ITER_IS_ITERNEXT (0x0080)
|
||||
#define MP_TYPE_FLAG_ITER_IS_CUSTOM (0x0100)
|
||||
#define MP_TYPE_FLAG_ITER_IS_STREAM (MP_TYPE_FLAG_ITER_IS_ITERNEXT | MP_TYPE_FLAG_ITER_IS_CUSTOM)
|
||||
#define MP_TYPE_FLAG_INSTANCE_TYPE (0x0200)
|
||||
|
||||
typedef enum {
|
||||
PRINT_STR = 0,
|
||||
@ -914,7 +916,7 @@ void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *type);
|
||||
#define mp_obj_is_int(o) (mp_obj_is_small_int(o) || mp_obj_is_exact_type(o, &mp_type_int))
|
||||
#define mp_obj_is_str(o) (mp_obj_is_qstr(o) || mp_obj_is_exact_type(o, &mp_type_str))
|
||||
#define mp_obj_is_str_or_bytes(o) (mp_obj_is_qstr(o) || (mp_obj_is_obj(o) && MP_OBJ_TYPE_GET_SLOT_OR_NULL(((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type, binary_op) == mp_obj_str_binary_op))
|
||||
#define mp_obj_is_dict_or_ordereddict(o) (mp_obj_is_obj(o) && MP_OBJ_TYPE_GET_SLOT_OR_NULL(((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type, make_new) == mp_obj_dict_make_new)
|
||||
bool mp_obj_is_dict_or_ordereddict(mp_obj_t o);
|
||||
#define mp_obj_is_fun(o) (mp_obj_is_obj(o) && (((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->name == MP_QSTR_function))
|
||||
|
||||
mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict);
|
||||
@ -1030,7 +1032,7 @@ mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in);
|
||||
mp_uint_t mp_obj_int_get_uint_checked(mp_const_obj_t self_in);
|
||||
|
||||
// exception
|
||||
#define mp_obj_is_native_exception_instance(o) (MP_OBJ_TYPE_GET_SLOT_OR_NULL(mp_obj_get_type(o), make_new) == mp_obj_exception_make_new)
|
||||
bool mp_obj_is_native_exception_instance(mp_obj_t self_in);
|
||||
bool mp_obj_is_exception_type(mp_obj_t self_in);
|
||||
bool mp_obj_is_exception_instance(mp_obj_t self_in);
|
||||
bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type);
|
||||
|
@ -33,6 +33,10 @@
|
||||
#include "py/objtype.h"
|
||||
#include "py/objstr.h"
|
||||
|
||||
bool mp_obj_is_dict_or_ordereddict(mp_obj_t o) {
|
||||
return mp_obj_is_obj(o) && MP_OBJ_TYPE_GET_SLOT_OR_NULL(((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type, make_new) == mp_obj_dict_make_new;
|
||||
}
|
||||
|
||||
const mp_obj_dict_t mp_const_empty_dict_obj = {
|
||||
.base = { .type = &mp_type_dict },
|
||||
.map = {
|
||||
|
@ -111,6 +111,10 @@ mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in) {
|
||||
#endif
|
||||
#endif // MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
|
||||
|
||||
bool mp_obj_is_native_exception_instance(mp_obj_t self_in) {
|
||||
return MP_OBJ_TYPE_GET_SLOT_OR_NULL(mp_obj_get_type(self_in), make_new) == mp_obj_exception_make_new;
|
||||
}
|
||||
|
||||
STATIC mp_obj_exception_t *get_native_exception(mp_obj_t self_in) {
|
||||
assert(mp_obj_is_exception_instance(self_in));
|
||||
if (mp_obj_is_native_exception_instance(self_in)) {
|
||||
|
20
py/objstr.c
20
py/objstr.c
@ -63,6 +63,10 @@ STATIC void str_check_arg_type(const mp_obj_type_t *self_type, const mp_obj_t ar
|
||||
}
|
||||
}
|
||||
|
||||
STATIC void check_is_str_or_bytes(mp_obj_t self_in) {
|
||||
mp_check_self(mp_obj_is_str_or_bytes(self_in));
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/* str */
|
||||
|
||||
@ -468,7 +472,7 @@ STATIC mp_obj_t bytes_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
|
||||
}
|
||||
|
||||
STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
|
||||
mp_check_self(mp_obj_is_str_or_bytes(self_in));
|
||||
check_is_str_or_bytes(self_in);
|
||||
const mp_obj_type_t *self_type = mp_obj_get_type(self_in);
|
||||
const mp_obj_type_t *ret_type = self_type;
|
||||
|
||||
@ -724,7 +728,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_rsplit_obj, 1, 3, str_rsplit);
|
||||
|
||||
STATIC mp_obj_t str_finder(size_t n_args, const mp_obj_t *args, int direction, bool is_index) {
|
||||
const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
|
||||
mp_check_self(mp_obj_is_str_or_bytes(args[0]));
|
||||
check_is_str_or_bytes(args[0]);
|
||||
|
||||
// check argument type
|
||||
str_check_arg_type(self_type, args[1]);
|
||||
@ -820,7 +824,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_endswith_obj, 2, 3, str_endswith);
|
||||
enum { LSTRIP, RSTRIP, STRIP };
|
||||
|
||||
STATIC mp_obj_t str_uni_strip(int type, size_t n_args, const mp_obj_t *args) {
|
||||
mp_check_self(mp_obj_is_str_or_bytes(args[0]));
|
||||
check_is_str_or_bytes(args[0]);
|
||||
const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
|
||||
|
||||
const byte *chars_to_del;
|
||||
@ -1422,7 +1426,7 @@ STATIC vstr_t mp_obj_str_format_helper(const char *str, const char *top, int *ar
|
||||
}
|
||||
|
||||
mp_obj_t mp_obj_str_format(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
|
||||
mp_check_self(mp_obj_is_str_or_bytes(args[0]));
|
||||
check_is_str_or_bytes(args[0]);
|
||||
|
||||
GET_STR_DATA_LEN(args[0], str, len);
|
||||
int arg_i = 0;
|
||||
@ -1433,7 +1437,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(str_format_obj, 1, mp_obj_str_format);
|
||||
|
||||
#if MICROPY_PY_BUILTINS_STR_OP_MODULO
|
||||
STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_t *args, mp_obj_t dict) {
|
||||
mp_check_self(mp_obj_is_str_or_bytes(pattern));
|
||||
check_is_str_or_bytes(pattern);
|
||||
|
||||
GET_STR_DATA_LEN(pattern, str, len);
|
||||
#if MICROPY_ERROR_REPORTING > MICROPY_ERROR_REPORTING_TERSE
|
||||
@ -1639,7 +1643,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_
|
||||
// The implementation is optimized, returning the original string if there's
|
||||
// nothing to replace.
|
||||
STATIC mp_obj_t str_replace(size_t n_args, const mp_obj_t *args) {
|
||||
mp_check_self(mp_obj_is_str_or_bytes(args[0]));
|
||||
check_is_str_or_bytes(args[0]);
|
||||
|
||||
mp_int_t max_rep = -1;
|
||||
if (n_args == 4) {
|
||||
@ -1742,7 +1746,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_replace_obj, 3, 4, str_replace);
|
||||
#if MICROPY_PY_BUILTINS_STR_COUNT
|
||||
STATIC mp_obj_t str_count(size_t n_args, const mp_obj_t *args) {
|
||||
const mp_obj_type_t *self_type = mp_obj_get_type(args[0]);
|
||||
mp_check_self(mp_obj_is_str_or_bytes(args[0]));
|
||||
check_is_str_or_bytes(args[0]);
|
||||
|
||||
// check argument type
|
||||
str_check_arg_type(self_type, args[1]);
|
||||
@ -1782,7 +1786,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(str_count_obj, 2, 4, str_count);
|
||||
|
||||
#if MICROPY_PY_BUILTINS_STR_PARTITION
|
||||
STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, int direction) {
|
||||
mp_check_self(mp_obj_is_str_or_bytes(self_in));
|
||||
check_is_str_or_bytes(self_in);
|
||||
const mp_obj_type_t *self_type = mp_obj_get_type(self_in);
|
||||
str_check_arg_type(self_type, arg);
|
||||
|
||||
|
@ -277,7 +277,7 @@ STATIC void instance_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k
|
||||
mp_printf(print, "<%s object at %p>", mp_obj_get_type_str(self_in), self);
|
||||
}
|
||||
|
||||
mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
||||
STATIC mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
||||
assert(mp_obj_is_instance_type(self));
|
||||
|
||||
// look for __new__ function
|
||||
@ -1131,7 +1131,8 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
|
||||
uint16_t base_flags = MP_TYPE_FLAG_EQ_NOT_REFLEXIVE
|
||||
| MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE
|
||||
| MP_TYPE_FLAG_EQ_HAS_NEQ_TEST
|
||||
| MP_TYPE_FLAG_ITER_IS_GETITER;
|
||||
| MP_TYPE_FLAG_ITER_IS_GETITER
|
||||
| MP_TYPE_FLAG_INSTANCE_TYPE;
|
||||
size_t bases_len;
|
||||
mp_obj_t *bases_items;
|
||||
mp_obj_tuple_get(bases_tuple, &bases_len, &bases_items);
|
||||
|
@ -46,10 +46,8 @@ mp_obj_instance_t *mp_obj_new_instance(const mp_obj_type_t *cls, const mp_obj_ty
|
||||
bool mp_obj_instance_is_callable(mp_obj_t self_in);
|
||||
mp_obj_t mp_obj_instance_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args);
|
||||
|
||||
#define mp_obj_is_instance_type(type) (MP_OBJ_TYPE_GET_SLOT_OR_NULL(type, make_new) == mp_obj_instance_make_new)
|
||||
#define mp_obj_is_native_type(type) (MP_OBJ_TYPE_GET_SLOT_OR_NULL(type, make_new) != mp_obj_instance_make_new)
|
||||
// this needs to be exposed for the above macros to work correctly
|
||||
mp_obj_t mp_obj_instance_make_new(const mp_obj_type_t *self_in, size_t n_args, size_t n_kw, const mp_obj_t *args);
|
||||
#define mp_obj_is_instance_type(type) ((type)->flags & MP_TYPE_FLAG_INSTANCE_TYPE)
|
||||
#define mp_obj_is_native_type(type) (!((type)->flags & MP_TYPE_FLAG_INSTANCE_TYPE))
|
||||
|
||||
// this needs to be exposed for mp_getiter
|
||||
mp_obj_t mp_obj_instance_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf);
|
||||
|
Loading…
Reference in New Issue
Block a user