Skip to content

Commit f600a6a

Browse files
committed
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_.
1 parent 2617eeb commit f600a6a

File tree

4 files changed

+28
-27
lines changed

4 files changed

+28
-27
lines changed

py/objstr.c

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ const mp_obj_t mp_const_empty_bytes;
5454

5555
STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str);
5656
STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str);
57-
mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len);
5857
STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in);
5958
STATIC NORETURN void arg_type_mixup();
6059

@@ -143,7 +142,7 @@ STATIC mp_obj_t str_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_
143142
}
144143
GET_STR_DATA_LEN(args[0], str_data, str_len);
145144
GET_STR_HASH(args[0], str_hash);
146-
mp_obj_str_t *o = str_new(&mp_type_str, NULL, str_len);
145+
mp_obj_str_t *o = mp_obj_new_str_of_type(&mp_type_str, NULL, str_len);
147146
o->data = str_data;
148147
o->hash = str_hash;
149148
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
171170
}
172171
GET_STR_DATA_LEN(args[0], str_data, str_len);
173172
GET_STR_HASH(args[0], str_hash);
174-
mp_obj_str_t *o = str_new(&mp_type_bytes, NULL, str_len);
173+
mp_obj_str_t *o = mp_obj_new_str_of_type(&mp_type_bytes, NULL, str_len);
175174
o->data = str_data;
176175
o->hash = str_hash;
177176
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) {
356355
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
357356
"Only slices with step=1 (aka None) are supported"));
358357
}
359-
return str_new(type, self_data + slice.start, slice.stop - slice.start);
358+
return mp_obj_new_str_of_type(type, self_data + slice.start, slice.stop - slice.start);
360359
}
361360
#endif
362361
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) {
447446
while (s < top && splits != 0) {
448447
const byte *start = s;
449448
while (s < top && !is_ws(*s)) s++;
450-
mp_obj_list_append(res, str_new(self_type, start, s - start));
449+
mp_obj_list_append(res, mp_obj_new_str_of_type(self_type, start, s - start));
451450
if (s >= top) {
452451
break;
453452
}
@@ -458,7 +457,7 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
458457
}
459458

460459
if (s < top) {
461-
mp_obj_list_append(res, str_new(self_type, s, top - s));
460+
mp_obj_list_append(res, mp_obj_new_str_of_type(self_type, s, top - s));
462461
}
463462

464463
} else {
@@ -482,7 +481,7 @@ STATIC mp_obj_t str_split(uint n_args, const mp_obj_t *args) {
482481
}
483482
s++;
484483
}
485-
mp_obj_list_append(res, str_new(self_type, start, s - start));
484+
mp_obj_list_append(res, mp_obj_new_str_of_type(self_type, start, s - start));
486485
if (s >= top) {
487486
break;
488487
}
@@ -537,10 +536,10 @@ STATIC mp_obj_t str_rsplit(uint n_args, const mp_obj_t *args) {
537536
s--;
538537
}
539538
if (s < beg || splits == 0) {
540-
res->items[idx] = str_new(self_type, beg, last - beg);
539+
res->items[idx] = mp_obj_new_str_of_type(self_type, beg, last - beg);
541540
break;
542541
}
543-
res->items[idx--] = str_new(self_type, s + sep_len, last - s - sep_len);
542+
res->items[idx--] = mp_obj_new_str_of_type(self_type, s + sep_len, last - s - sep_len);
544543
last = s;
545544
if (splits > 0) {
546545
splits--;
@@ -692,7 +691,7 @@ STATIC mp_obj_t str_uni_strip(int type, uint n_args, const mp_obj_t *args) {
692691
assert(last_good_char_pos >= first_good_char_pos);
693692
//+1 to accomodate the last character
694693
machine_uint_t stripped_len = last_good_char_pos - first_good_char_pos + 1;
695-
return str_new(self_type, orig_str + first_good_char_pos, stripped_len);
694+
return mp_obj_new_str_of_type(self_type, orig_str + first_good_char_pos, stripped_len);
696695
}
697696

698697
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
14551454
const byte *position_ptr = find_subbytes(str, str_len, sep, sep_len, direction);
14561455
if (position_ptr != NULL) {
14571456
machine_uint_t position = position_ptr - str;
1458-
result[0] = str_new(self_type, str, position);
1457+
result[0] = mp_obj_new_str_of_type(self_type, str, position);
14591458
result[1] = arg;
1460-
result[2] = str_new(self_type, str + position + sep_len, str_len - position - sep_len);
1459+
result[2] = mp_obj_new_str_of_type(self_type, str + position + sep_len, str_len - position - sep_len);
14611460
}
14621461

14631462
return mp_obj_new_tuple(3, result);
@@ -1641,7 +1640,7 @@ mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in) {
16411640
return o;
16421641
}
16431642

1644-
mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len) {
1643+
mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte* data, uint len) {
16451644
mp_obj_str_t *o = m_new_obj(mp_obj_str_t);
16461645
o->base.type = type;
16471646
o->len = len;
@@ -1656,21 +1655,23 @@ mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len) {
16561655
}
16571656

16581657
mp_obj_t mp_obj_new_str(const char* data, uint len, bool make_qstr_if_not_already) {
1659-
qstr q = qstr_find_strn(data, len);
1660-
if (q != MP_QSTR_NULL) {
1661-
// qstr with this data already exists
1662-
return MP_OBJ_NEW_QSTR(q);
1663-
} else if (make_qstr_if_not_already) {
1664-
// no existing qstr, make a new one
1658+
if (make_qstr_if_not_already) {
1659+
// use existing, or make a new qstr
16651660
return MP_OBJ_NEW_QSTR(qstr_from_strn(data, len));
16661661
} else {
1667-
// no existing qstr, don't make one
1668-
return str_new(&mp_type_str, (const byte*)data, len);
1662+
qstr q = qstr_find_strn(data, len);
1663+
if (q != MP_QSTR_NULL) {
1664+
// qstr with this data already exists
1665+
return MP_OBJ_NEW_QSTR(q);
1666+
} else {
1667+
// no existing qstr, don't make one
1668+
return mp_obj_new_str_of_type(&mp_type_str, (const byte*)data, len);
1669+
}
16691670
}
16701671
}
16711672

16721673
mp_obj_t mp_obj_new_bytes(const byte* data, uint len) {
1673-
return str_new(&mp_type_bytes, data, len);
1674+
return mp_obj_new_str_of_type(&mp_type_bytes, data, len);
16741675
}
16751676

16761677
bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) {

py/objstr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ typedef struct _mp_obj_str_t {
3636
#define MP_DEFINE_STR_OBJ(obj_name, str) mp_obj_str_t obj_name = {{&mp_type_str}, 0, sizeof(str) - 1, (const byte*)str};
3737

3838
mp_obj_t mp_obj_str_format(uint n_args, const mp_obj_t *args);
39-
mp_obj_t str_new(const mp_obj_type_t *type, const byte* data, uint len);
39+
mp_obj_t mp_obj_new_str_of_type(const mp_obj_type_t *type, const byte* data, uint len);

py/objstringio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ STATIC machine_int_t stringio_write(mp_obj_t o_in, const void *buf, machine_uint
8383

8484
STATIC mp_obj_t stringio_getvalue(mp_obj_t self_in) {
8585
mp_obj_stringio_t *self = self_in;
86-
return str_new(STREAM_TO_CONTENT_TYPE(self), (byte*)self->vstr->buf, self->vstr->len);
86+
return mp_obj_new_str_of_type(STREAM_TO_CONTENT_TYPE(self), (byte*)self->vstr->buf, self->vstr->len);
8787
}
8888
STATIC MP_DEFINE_CONST_FUN_OBJ_1(stringio_getvalue_obj, stringio_getvalue);
8989

py/stream.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ STATIC mp_obj_t stream_read(uint n_args, const mp_obj_t *args) {
8181
}
8282
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", error));
8383
} else {
84-
mp_obj_t s = str_new(STREAM_CONTENT_TYPE(o->type->stream_p), buf, out_sz); // will reallocate to use exact size
84+
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
8585
m_free(buf, sz);
8686
return s;
8787
}
@@ -158,7 +158,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
158158
}
159159
}
160160

161-
mp_obj_t s = str_new(STREAM_CONTENT_TYPE(o->type->stream_p), (byte*)vstr->buf, total_size);
161+
mp_obj_t s = mp_obj_new_str_of_type(STREAM_CONTENT_TYPE(o->type->stream_p), (byte*)vstr->buf, total_size);
162162
vstr_free(vstr);
163163
return s;
164164
}
@@ -207,7 +207,7 @@ STATIC mp_obj_t stream_unbuffered_readline(uint n_args, const mp_obj_t *args) {
207207
}
208208
}
209209
// TODO need a string creation API that doesn't copy the given data
210-
mp_obj_t ret = str_new(STREAM_CONTENT_TYPE(o->type->stream_p), (byte*)vstr->buf, vstr->len);
210+
mp_obj_t ret = mp_obj_new_str_of_type(STREAM_CONTENT_TYPE(o->type->stream_p), (byte*)vstr->buf, vstr->len);
211211
vstr_free(vstr);
212212
return ret;
213213
}

0 commit comments

Comments
 (0)