Browse Source

py: Slightly improve efficiency of mp_obj_new_str; rename str_new.

Reorder interning logic in mp_obj_new_str, to be more efficient.

str_new is globally accessible, so should be prefixed with mp_obj_.
pull/629/head
Damien George 11 years ago
parent
commit
f600a6a085
  1. 45
      py/objstr.c
  2. 2
      py/objstr.h
  3. 2
      py/objstringio.c
  4. 6
      py/stream.c

45
py/objstr.c

@ -54,7 +54,6 @@ const mp_obj_t mp_const_empty_bytes;
STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str); STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str);
STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str); STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str);
mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len);
STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in); STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in);
STATIC NORETURN void arg_type_mixup(); STATIC NORETURN void arg_type_mixup();
@ -143,7 +142,7 @@ STATIC mp_obj_t str_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_
} }
GET_STR_DATA_LEN(args[0], str_data, str_len); GET_STR_DATA_LEN(args[0], str_data, str_len);
GET_STR_HASH(args[0], str_hash); GET_STR_HASH(args[0], str_hash);
mp_obj_str_t *o = str_new(&mp_type_str, NULL, str_len); mp_obj_str_t *o = mp_obj_new_str_of_type(&mp_type_str, NULL, str_len);
o->data = str_data; o->data = str_data;
o->hash = str_hash; o->hash = str_hash;
return o; return o;
@ -171,7 +170,7 @@ STATIC mp_obj_t bytes_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
} }
GET_STR_DATA_LEN(args[0], str_data, str_len); GET_STR_DATA_LEN(args[0], str_data, str_len);
GET_STR_HASH(args[0], str_hash); GET_STR_HASH(args[0], str_hash);
mp_obj_str_t *o = str_new(&mp_type_bytes, NULL, str_len); mp_obj_str_t *o = mp_obj_new_str_of_type(&mp_type_bytes, NULL, str_len);
o->data = str_data; o->data = str_data;
o->hash = str_hash; o->hash = str_hash;
return o; return o;
@ -356,7 +355,7 @@ STATIC mp_obj_t str_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError, nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
"Only slices with step=1 (aka None) are supported")); "Only slices with step=1 (aka None) are supported"));
} }
return str_new(type, self_data + slice.start, slice.stop - slice.start); return mp_obj_new_str_of_type(type, self_data + slice.start, slice.stop - slice.start);
} }
#endif #endif
uint index_val = mp_get_index(type, self_len, index, false); uint index_val = mp_get_index(type, self_len, index, false);
@ -447,7 +446,7 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
while (s < top && splits != 0) { while (s < top && splits != 0) {
const byte *start = s; const byte *start = s;
while (s < top && !is_ws(*s)) s++; while (s < top && !is_ws(*s)) s++;
mp_obj_list_append(res, str_new(self_type, start, s - start)); mp_obj_list_append(res, mp_obj_new_str_of_type(self_type, start, s - start));
if (s >= top) { if (s >= top) {
break; break;
} }
@ -458,7 +457,7 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
} }
if (s < top) { if (s < top) {
mp_obj_list_append(res, str_new(self_type, s, top - s)); mp_obj_list_append(res, mp_obj_new_str_of_type(self_type, s, top - s));
} }
} else { } else {
@ -482,7 +481,7 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
} }
s++; s++;
} }
mp_obj_list_append(res, str_new(self_type, start, s - start)); mp_obj_list_append(res, mp_obj_new_str_of_type(self_type, start, s - start));
if (s >= top) { if (s >= top) {
break; break;
} }
@ -537,10 +536,10 @@ STATIC mp_obj_t str_rsplit(uint n_args, const mp_obj_t *args) {
s--; s--;
} }
if (s < beg || splits == 0) { if (s < beg || splits == 0) {
res->items[idx] = str_new(self_type, beg, last - beg); res->items[idx] = mp_obj_new_str_of_type(self_type, beg, last - beg);
break; break;
} }
res->items[idx--] = str_new(self_type, s + sep_len, last - s - sep_len); res->items[idx--] = mp_obj_new_str_of_type(self_type, s + sep_len, last - s - sep_len);
last = s; last = s;
if (splits > 0) { if (splits > 0) {
splits--; splits--;
@ -692,7 +691,7 @@ STATIC mp_obj_t str_uni_strip(int type, uint n_args, const mp_obj_t *args) {
assert(last_good_char_pos >= first_good_char_pos); assert(last_good_char_pos >= first_good_char_pos);
//+1 to accomodate the last character //+1 to accomodate the last character
machine_uint_t stripped_len = last_good_char_pos - first_good_char_pos + 1; machine_uint_t stripped_len = last_good_char_pos - first_good_char_pos + 1;
return str_new(self_type, orig_str + first_good_char_pos, stripped_len); return mp_obj_new_str_of_type(self_type, orig_str + first_good_char_pos, stripped_len);
} }
STATIC mp_obj_t str_strip(uint n_args, const mp_obj_t *args) { STATIC mp_obj_t str_strip(uint n_args, const mp_obj_t *args) {
@ -1455,9 +1454,9 @@ STATIC mp_obj_t str_partitioner(mp_obj_t self_in, mp_obj_t arg, machine_int_t di
const byte *position_ptr = find_subbytes(str, str_len, sep, sep_len, direction); const byte *position_ptr = find_subbytes(str, str_len, sep, sep_len, direction);
if (position_ptr != NULL) { if (position_ptr != NULL) {
machine_uint_t position = position_ptr - str; machine_uint_t position = position_ptr - str;
result[0] = str_new(self_type, str, position); result[0] = mp_obj_new_str_of_type(self_type, str, position);
result[1] = arg; result[1] = arg;
result[2] = str_new(self_type, str + position + sep_len, str_len - position - sep_len); result[2] = mp_obj_new_str_of_type(self_type, str + position + sep_len, str_len - position - sep_len);
} }
return mp_obj_new_tuple(3, result); return mp_obj_new_tuple(3, result);
@ -1641,7 +1640,7 @@ mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in) {
return o; return o;
} }
mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len) { mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte* data, uint len) {
mp_obj_str_t *o = m_new_obj(mp_obj_str_t); mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
o->base.type = type; o->base.type = type;
o->len = len; o->len = len;
@ -1656,21 +1655,23 @@ mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len) {
} }
mp_obj_t mp_obj_new_str(const char* data, uint len, bool make_qstr_if_not_already) { mp_obj_t mp_obj_new_str(const char* data, uint len, bool make_qstr_if_not_already) {
qstr q = qstr_find_strn(data, len); if (make_qstr_if_not_already) {
if (q != MP_QSTR_NULL) { // use existing, or make a new qstr
// qstr with this data already exists
return MP_OBJ_NEW_QSTR(q);
} else if (make_qstr_if_not_already) {
// no existing qstr, make a new one
return MP_OBJ_NEW_QSTR(qstr_from_strn(data, len)); return MP_OBJ_NEW_QSTR(qstr_from_strn(data, len));
} else { } else {
// no existing qstr, don't make one qstr q = qstr_find_strn(data, len);
return str_new(&mp_type_str, (const byte*)data, len); if (q != MP_QSTR_NULL) {
// qstr with this data already exists
return MP_OBJ_NEW_QSTR(q);
} else {
// no existing qstr, don't make one
return mp_obj_new_str_of_type(&mp_type_str, (const byte*)data, len);
}
} }
} }
mp_obj_t mp_obj_new_bytes(const byte* data, uint len) { mp_obj_t mp_obj_new_bytes(const byte* data, uint len) {
return str_new(&mp_type_bytes, data, len); return mp_obj_new_str_of_type(&mp_type_bytes, data, len);
} }
bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) { bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) {

2
py/objstr.h

@ -36,4 +36,4 @@ typedef struct _mp_obj_str_t {
#define MP_DEFINE_STR_OBJ(obj_name, str) mp_obj_str_t obj_name = {{&mp_type_str}, 0, sizeof(str) - 1, (const byte*)str}; #define MP_DEFINE_STR_OBJ(obj_name, str) mp_obj_str_t obj_name = {{&mp_type_str}, 0, sizeof(str) - 1, (const byte*)str};
mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args); mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args);
mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len); mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte* data, uint len);

2
py/objstringio.c

@ -83,7 +83,7 @@ STATIC machine_int_t stringio_write(mp_obj_t o_in, const void *buf, machine_uint
STATIC mp_obj_t stringio_getvalue(mp_obj_t self_in) { STATIC mp_obj_t stringio_getvalue(mp_obj_t self_in) {
mp_obj_stringio_t *self = self_in; mp_obj_stringio_t *self = self_in;
return str_new(STREAM_TO_CONTENT_TYPE(self), (byte*)self->vstr->buf, self->vstr->len); return mp_obj_new_str_of_type(STREAM_TO_CONTENT_TYPE(self), (byte*)self->vstr->buf, self->vstr->len);
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_getvalue_obj, stringio_getvalue); STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_getvalue_obj, stringio_getvalue);

6
py/stream.c

@ -81,7 +81,7 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
} }
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error)); nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
} else { } else {
mp_obj_t s = str_new(STREAM_CONTENT_TYPE(o->type->stream_p), buf, out_sz); // will reallocate to use exact size mp_obj_t s = mp_obj_new_str_of_type(STREAM_CONTENT_TYPE(o->type->stream_p), buf, out_sz); // will reallocate to use exact size
m_free(buf, sz); m_free(buf, sz);
return s; return s;
} }
@ -158,7 +158,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
} }
} }
mp_obj_t s = str_new(STREAM_CONTENT_TYPE(o->type->stream_p), (byte*)vstr->buf, total_size); mp_obj_t s = mp_obj_new_str_of_type(STREAM_CONTENT_TYPE(o->type->stream_p), (byte*)vstr->buf, total_size);
vstr_free(vstr); vstr_free(vstr);
return s; return s;
} }
@ -207,7 +207,7 @@ STATIC mp_obj_t stream_unbuffered_readline(uint n_args, const mp_obj_t *args) {
} }
} }
// TODO need a string creation API that doesn't copy the given data // TODO need a string creation API that doesn't copy the given data
mp_obj_t ret = str_new(STREAM_CONTENT_TYPE(o->type->stream_p), (byte*)vstr->buf, vstr->len); mp_obj_t ret = mp_obj_new_str_of_type(STREAM_CONTENT_TYPE(o->type->stream_p), (byte*)vstr->buf, vstr->len);
vstr_free(vstr); vstr_free(vstr);
return ret; return ret;
} }

Loading…
Cancel
Save