Skip to content

Commit 6abede2

Browse files
committed
py/stream: Introduce and use efficient mp_get_stream to access stream_p.
The existing mp_get_stream_raise() helper does explicit checks that the input object is a real pointer object, has a non-NULL stream protocol, and has the desired stream C method (read/write/ioctl). In most cases it is not necessary to do these checks because it is guaranteed that the input object has the stream protocol and desired C methods. For example, native objects that use the stream wrappers (eg mp_stream_readinto_obj) in their locals dict always have the stream protocol (or else they shouldn't have these wrappers in their locals dict). This patch introduces an efficient mp_get_stream() which doesn't do any checks and just extracts the stream protocol struct. This should be used in all cases where the argument object is known to be a stream. The existing mp_get_stream_raise() should be used primarily to verify that an object does have the correct stream protocol methods. All uses of mp_get_stream_raise() in py/stream.c have been converted to use mp_get_stream() because the argument is guaranteed to be a proper stream object. This patch improves efficiency of stream operations and reduces code size.
1 parent 31cf49c commit 6abede2

2 files changed

Lines changed: 15 additions & 15 deletions

File tree

py/stream.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
/*
23
* This file is part of the MicroPython project, http://micropython.org/
34
*
@@ -47,10 +48,9 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in);
4748
// be equal to input size).
4849
mp_uint_t mp_stream_rw(mp_obj_t stream, void *buf_, mp_uint_t size, int *errcode, byte flags) {
4950
byte *buf = buf_;
50-
mp_obj_base_t* s = (mp_obj_base_t*)MP_OBJ_TO_PTR(stream);
5151
typedef mp_uint_t (*io_func_t)(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode);
5252
io_func_t io_func;
53-
const mp_stream_p_t *stream_p = s->type->protocol;
53+
const mp_stream_p_t *stream_p = mp_get_stream(stream);
5454
if (flags & MP_STREAM_RW_WRITE) {
5555
io_func = (io_func_t)stream_p->write;
5656
} else {
@@ -99,8 +99,6 @@ const mp_stream_p_t *mp_get_stream_raise(mp_obj_t self_in, int flags) {
9999
}
100100

101101
STATIC mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte flags) {
102-
const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
103-
104102
// What to do if sz < -1? Python docs don't specify this case.
105103
// CPython does a readall, but here we silently let negatives through,
106104
// and they will cause a MemoryError.
@@ -109,6 +107,8 @@ STATIC mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte fl
109107
return stream_readall(args[0]);
110108
}
111109

110+
const mp_stream_p_t *stream_p = mp_get_stream(args[0]);
111+
112112
#if MICROPY_PY_BUILTINS_STR_UNICODE
113113
if (stream_p->is_text) {
114114
// We need to read sz number of unicode characters. Because we don't have any
@@ -227,8 +227,6 @@ STATIC mp_obj_t stream_read1(size_t n_args, const mp_obj_t *args) {
227227
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read1_obj, 1, 2, stream_read1);
228228

229229
mp_obj_t mp_stream_write(mp_obj_t self_in, const void *buf, size_t len, byte flags) {
230-
mp_get_stream_raise(self_in, MP_STREAM_OP_WRITE);
231-
232230
int error;
233231
mp_uint_t out_sz = mp_stream_rw(self_in, (void*)buf, len, &error, flags);
234232
if (error != 0) {
@@ -276,7 +274,6 @@ STATIC mp_obj_t stream_write1_method(mp_obj_t self_in, mp_obj_t arg) {
276274
MP_DEFINE_CONST_FUN_OBJ_2(mp_stream_write1_obj, stream_write1_method);
277275

278276
STATIC mp_obj_t stream_readinto(size_t n_args, const mp_obj_t *args) {
279-
mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
280277
mp_buffer_info_t bufinfo;
281278
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
282279

@@ -305,7 +302,7 @@ STATIC mp_obj_t stream_readinto(size_t n_args, const mp_obj_t *args) {
305302
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto_obj, 2, 3, stream_readinto);
306303

307304
STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
308-
const mp_stream_p_t *stream_p = mp_get_stream_raise(self_in, MP_STREAM_OP_READ);
305+
const mp_stream_p_t *stream_p = mp_get_stream(self_in);
309306

310307
mp_uint_t total_size = 0;
311308
vstr_t vstr;
@@ -346,7 +343,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) {
346343

347344
// Unbuffered, inefficient implementation of readline() for raw I/O files.
348345
STATIC mp_obj_t stream_unbuffered_readline(size_t n_args, const mp_obj_t *args) {
349-
const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
346+
const mp_stream_p_t *stream_p = mp_get_stream(args[0]);
350347

351348
mp_int_t max_size = -1;
352349
if (n_args > 1) {
@@ -421,7 +418,7 @@ mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
421418
}
422419

423420
mp_obj_t mp_stream_close(mp_obj_t stream) {
424-
const mp_stream_p_t *stream_p = mp_get_stream_raise(stream, MP_STREAM_OP_IOCTL);
421+
const mp_stream_p_t *stream_p = mp_get_stream(stream);
425422
int error;
426423
mp_uint_t res = stream_p->ioctl(stream, MP_STREAM_CLOSE, 0, &error);
427424
if (res == MP_STREAM_ERROR) {
@@ -432,8 +429,6 @@ mp_obj_t mp_stream_close(mp_obj_t stream) {
432429
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_close_obj, mp_stream_close);
433430

434431
STATIC mp_obj_t stream_seek(size_t n_args, const mp_obj_t *args) {
435-
const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL);
436-
437432
struct mp_stream_seek_t seek_s;
438433
// TODO: Could be uint64
439434
seek_s.offset = mp_obj_get_int(args[1]);
@@ -447,6 +442,7 @@ STATIC mp_obj_t stream_seek(size_t n_args, const mp_obj_t *args) {
447442
mp_raise_OSError(MP_EINVAL);
448443
}
449444

445+
const mp_stream_p_t *stream_p = mp_get_stream(args[0]);
450446
int error;
451447
mp_uint_t res = stream_p->ioctl(args[0], MP_STREAM_SEEK, (mp_uint_t)(uintptr_t)&seek_s, &error);
452448
if (res == MP_STREAM_ERROR) {
@@ -467,7 +463,7 @@ STATIC mp_obj_t stream_tell(mp_obj_t self) {
467463
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_tell_obj, stream_tell);
468464

469465
STATIC mp_obj_t stream_flush(mp_obj_t self) {
470-
const mp_stream_p_t *stream_p = mp_get_stream_raise(self, MP_STREAM_OP_IOCTL);
466+
const mp_stream_p_t *stream_p = mp_get_stream(self);
471467
int error;
472468
mp_uint_t res = stream_p->ioctl(self, MP_STREAM_FLUSH, 0, &error);
473469
if (res == MP_STREAM_ERROR) {
@@ -478,8 +474,6 @@ STATIC mp_obj_t stream_flush(mp_obj_t self) {
478474
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_flush_obj, stream_flush);
479475

480476
STATIC mp_obj_t stream_ioctl(size_t n_args, const mp_obj_t *args) {
481-
const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL);
482-
483477
mp_buffer_info_t bufinfo;
484478
uintptr_t val = 0;
485479
if (n_args > 2) {
@@ -490,6 +484,7 @@ STATIC mp_obj_t stream_ioctl(size_t n_args, const mp_obj_t *args) {
490484
}
491485
}
492486

487+
const mp_stream_p_t *stream_p = mp_get_stream(args[0]);
493488
int error;
494489
mp_uint_t res = stream_p->ioctl(args[0], mp_obj_get_int(args[1]), val, &error);
495490
if (res == MP_STREAM_ERROR) {

py/stream.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_ioctl_obj);
9090
#define MP_STREAM_OP_WRITE (2)
9191
#define MP_STREAM_OP_IOCTL (4)
9292

93+
// Object is assumed to have a non-NULL stream protocol with valid r/w/ioctl methods
94+
static inline const mp_stream_p_t *mp_get_stream(mp_const_obj_t self) {
95+
return (const mp_stream_p_t*)((const mp_obj_base_t*)MP_OBJ_TO_PTR(self))->type->protocol;
96+
}
97+
9398
const mp_stream_p_t *mp_get_stream_raise(mp_obj_t self_in, int flags);
9499
mp_obj_t mp_stream_close(mp_obj_t stream);
95100

0 commit comments

Comments
 (0)