Skip to content

Commit 470a44b

Browse files
jimmodpgeorge
authored andcommitted
extmod/modframebuf: Optimise argument handling.
Several methods extract mp_int_t from adjacent arguments. This reduces code size for the repeated calls to mp_obj_get_int. Signed-off-by: Jim Mussared <[email protected]>
1 parent 59e3348 commit 470a44b

File tree

1 file changed

+77
-88
lines changed

1 file changed

+77
-88
lines changed

extmod/modframebuf.c

Lines changed: 77 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -263,21 +263,21 @@ STATIC void fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, u
263263
formats[fb->format].fill_rect(fb, x, y, xend - x, yend - y, col);
264264
}
265265

266-
STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
266+
STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args_in) {
267267
mp_arg_check_num(n_args, n_kw, 4, 5, false);
268268

269269
mp_obj_framebuf_t *o = mp_obj_malloc(mp_obj_framebuf_t, type);
270-
o->buf_obj = args[0];
270+
o->buf_obj = args_in[0];
271271

272272
mp_buffer_info_t bufinfo;
273-
mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_WRITE);
273+
mp_get_buffer_raise(args_in[0], &bufinfo, MP_BUFFER_WRITE);
274274
o->buf = bufinfo.buf;
275275

276-
o->width = mp_obj_get_int(args[1]);
277-
o->height = mp_obj_get_int(args[2]);
278-
o->format = mp_obj_get_int(args[3]);
276+
o->width = mp_obj_get_int(args_in[1]);
277+
o->height = mp_obj_get_int(args_in[2]);
278+
o->format = mp_obj_get_int(args_in[3]);
279279
if (n_args >= 5) {
280-
o->stride = mp_obj_get_int(args[4]);
280+
o->stride = mp_obj_get_int(args_in[4]);
281281
} else {
282282
o->stride = o->width;
283283
}
@@ -305,6 +305,12 @@ STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size
305305
return MP_OBJ_FROM_PTR(o);
306306
}
307307

308+
STATIC void framebuf_args(const mp_obj_t *args_in, mp_int_t *args_out, int n) {
309+
for (int i = 0; i < n; ++i) {
310+
args_out[i] = mp_obj_get_int(args_in[i + 1]);
311+
}
312+
}
313+
308314
STATIC mp_int_t framebuf_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
309315
(void)flags;
310316
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(self_in);
@@ -322,98 +328,71 @@ STATIC mp_obj_t framebuf_fill(mp_obj_t self_in, mp_obj_t col_in) {
322328
}
323329
STATIC MP_DEFINE_CONST_FUN_OBJ_2(framebuf_fill_obj, framebuf_fill);
324330

325-
STATIC mp_obj_t framebuf_fill_rect(size_t n_args, const mp_obj_t *args) {
326-
(void)n_args;
327-
328-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
329-
mp_int_t x = mp_obj_get_int(args[1]);
330-
mp_int_t y = mp_obj_get_int(args[2]);
331-
mp_int_t width = mp_obj_get_int(args[3]);
332-
mp_int_t height = mp_obj_get_int(args[4]);
333-
mp_int_t col = mp_obj_get_int(args[5]);
334-
335-
fill_rect(self, x, y, width, height, col);
336-
331+
STATIC mp_obj_t framebuf_fill_rect(size_t n_args, const mp_obj_t *args_in) {
332+
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]);
333+
mp_int_t args[5]; // x, y, w, h, col
334+
framebuf_args(args_in, args, 5);
335+
fill_rect(self, args[0], args[1], args[2], args[3], args[4]);
337336
return mp_const_none;
338337
}
339338
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_fill_rect_obj, 6, 6, framebuf_fill_rect);
340339

341-
STATIC mp_obj_t framebuf_pixel(size_t n_args, const mp_obj_t *args) {
342-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
343-
mp_int_t x = mp_obj_get_int(args[1]);
344-
mp_int_t y = mp_obj_get_int(args[2]);
340+
STATIC mp_obj_t framebuf_pixel(size_t n_args, const mp_obj_t *args_in) {
341+
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]);
342+
mp_int_t x = mp_obj_get_int(args_in[1]);
343+
mp_int_t y = mp_obj_get_int(args_in[2]);
345344
if (0 <= x && x < self->width && 0 <= y && y < self->height) {
346345
if (n_args == 3) {
347346
// get
348347
return MP_OBJ_NEW_SMALL_INT(getpixel(self, x, y));
349348
} else {
350349
// set
351-
setpixel(self, x, y, mp_obj_get_int(args[3]));
350+
setpixel(self, x, y, mp_obj_get_int(args_in[3]));
352351
}
353352
}
354353
return mp_const_none;
355354
}
356355
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_pixel_obj, 3, 4, framebuf_pixel);
357356

358-
STATIC mp_obj_t framebuf_hline(size_t n_args, const mp_obj_t *args) {
357+
STATIC mp_obj_t framebuf_hline(size_t n_args, const mp_obj_t *args_in) {
359358
(void)n_args;
360359

361-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
362-
mp_int_t x = mp_obj_get_int(args[1]);
363-
mp_int_t y = mp_obj_get_int(args[2]);
364-
mp_int_t w = mp_obj_get_int(args[3]);
365-
mp_int_t col = mp_obj_get_int(args[4]);
360+
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]);
361+
mp_int_t args[4]; // x, y, w, col
362+
framebuf_args(args_in, args, 4);
366363

367-
fill_rect(self, x, y, w, 1, col);
364+
fill_rect(self, args[0], args[1], args[2], 1, args[3]);
368365

369366
return mp_const_none;
370367
}
371368
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_hline_obj, 5, 5, framebuf_hline);
372369

373-
STATIC mp_obj_t framebuf_vline(size_t n_args, const mp_obj_t *args) {
370+
STATIC mp_obj_t framebuf_vline(size_t n_args, const mp_obj_t *args_in) {
374371
(void)n_args;
375372

376-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
377-
mp_int_t x = mp_obj_get_int(args[1]);
378-
mp_int_t y = mp_obj_get_int(args[2]);
379-
mp_int_t h = mp_obj_get_int(args[3]);
380-
mp_int_t col = mp_obj_get_int(args[4]);
373+
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]);
374+
mp_int_t args[4]; // x, y, h, col
375+
framebuf_args(args_in, args, 4);
381376

382-
fill_rect(self, x, y, 1, h, col);
377+
fill_rect(self, args[0], args[1], 1, args[2], args[3]);
383378

384379
return mp_const_none;
385380
}
386381
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_vline_obj, 5, 5, framebuf_vline);
387382

388-
STATIC mp_obj_t framebuf_rect(size_t n_args, const mp_obj_t *args) {
389-
(void)n_args;
390-
391-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
392-
mp_int_t x = mp_obj_get_int(args[1]);
393-
mp_int_t y = mp_obj_get_int(args[2]);
394-
mp_int_t w = mp_obj_get_int(args[3]);
395-
mp_int_t h = mp_obj_get_int(args[4]);
396-
mp_int_t col = mp_obj_get_int(args[5]);
397-
398-
fill_rect(self, x, y, w, 1, col);
399-
fill_rect(self, x, y + h - 1, w, 1, col);
400-
fill_rect(self, x, y, 1, h, col);
401-
fill_rect(self, x + w - 1, y, 1, h, col);
402-
383+
STATIC mp_obj_t framebuf_rect(size_t n_args, const mp_obj_t *args_in) {
384+
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]);
385+
mp_int_t args[5]; // x, y, w, h, col
386+
framebuf_args(args_in, args, 5);
387+
fill_rect(self, args[0], args[1], args[2], 1, args[4]);
388+
fill_rect(self, args[0], args[1] + args[3] - 1, args[2], 1, args[4]);
389+
fill_rect(self, args[0], args[1], 1, args[3], args[4]);
390+
fill_rect(self, args[0] + args[2] - 1, args[1], 1, args[3], args[4]);
403391
return mp_const_none;
404392
}
405393
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_rect_obj, 6, 6, framebuf_rect);
406394

407-
STATIC mp_obj_t framebuf_line(size_t n_args, const mp_obj_t *args) {
408-
(void)n_args;
409-
410-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
411-
mp_int_t x1 = mp_obj_get_int(args[1]);
412-
mp_int_t y1 = mp_obj_get_int(args[2]);
413-
mp_int_t x2 = mp_obj_get_int(args[3]);
414-
mp_int_t y2 = mp_obj_get_int(args[4]);
415-
mp_int_t col = mp_obj_get_int(args[5]);
416-
395+
STATIC void line(const mp_obj_framebuf_t *fb, mp_int_t x1, mp_int_t y1, mp_int_t x2, mp_int_t y2, mp_int_t col) {
417396
mp_int_t dx = x2 - x1;
418397
mp_int_t sx;
419398
if (dx > 0) {
@@ -452,12 +431,12 @@ STATIC mp_obj_t framebuf_line(size_t n_args, const mp_obj_t *args) {
452431
mp_int_t e = 2 * dy - dx;
453432
for (mp_int_t i = 0; i < dx; ++i) {
454433
if (steep) {
455-
if (0 <= y1 && y1 < self->width && 0 <= x1 && x1 < self->height) {
456-
setpixel(self, y1, x1, col);
434+
if (0 <= y1 && y1 < fb->width && 0 <= x1 && x1 < fb->height) {
435+
setpixel(fb, y1, x1, col);
457436
}
458437
} else {
459-
if (0 <= x1 && x1 < self->width && 0 <= y1 && y1 < self->height) {
460-
setpixel(self, x1, y1, col);
438+
if (0 <= x1 && x1 < fb->width && 0 <= y1 && y1 < fb->height) {
439+
setpixel(fb, x1, y1, col);
461440
}
462441
}
463442
while (e >= 0) {
@@ -468,31 +447,41 @@ STATIC mp_obj_t framebuf_line(size_t n_args, const mp_obj_t *args) {
468447
e += 2 * dy;
469448
}
470449

471-
if (0 <= x2 && x2 < self->width && 0 <= y2 && y2 < self->height) {
472-
setpixel(self, x2, y2, col);
450+
if (0 <= x2 && x2 < fb->width && 0 <= y2 && y2 < fb->height) {
451+
setpixel(fb, x2, y2, col);
473452
}
453+
}
454+
455+
STATIC mp_obj_t framebuf_line(size_t n_args, const mp_obj_t *args_in) {
456+
(void)n_args;
457+
458+
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]);
459+
mp_int_t args[5]; // x1, y1, x2, y2, col
460+
framebuf_args(args_in, args, 5);
461+
462+
line(self, args[0], args[1], args[2], args[3], args[4]);
474463

475464
return mp_const_none;
476465
}
477466
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(framebuf_line_obj, 6, 6, framebuf_line);
478467

479-
STATIC mp_obj_t framebuf_blit(size_t n_args, const mp_obj_t *args) {
480-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
481-
mp_obj_t source_in = mp_obj_cast_to_native_base(args[1], MP_OBJ_FROM_PTR(&mp_type_framebuf));
468+
STATIC mp_obj_t framebuf_blit(size_t n_args, const mp_obj_t *args_in) {
469+
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]);
470+
mp_obj_t source_in = mp_obj_cast_to_native_base(args_in[1], MP_OBJ_FROM_PTR(&mp_type_framebuf));
482471
if (source_in == MP_OBJ_NULL) {
483472
mp_raise_TypeError(NULL);
484473
}
485474
mp_obj_framebuf_t *source = MP_OBJ_TO_PTR(source_in);
486475

487-
mp_int_t x = mp_obj_get_int(args[2]);
488-
mp_int_t y = mp_obj_get_int(args[3]);
476+
mp_int_t x = mp_obj_get_int(args_in[2]);
477+
mp_int_t y = mp_obj_get_int(args_in[3]);
489478
mp_int_t key = -1;
490479
if (n_args > 4) {
491-
key = mp_obj_get_int(args[4]);
480+
key = mp_obj_get_int(args_in[4]);
492481
}
493482
mp_obj_framebuf_t *palette = NULL;
494-
if (n_args > 5 && args[5] != mp_const_none) {
495-
palette = MP_OBJ_TO_PTR(mp_obj_cast_to_native_base(args[5], MP_OBJ_FROM_PTR(&mp_type_framebuf)));
483+
if (n_args > 5 && args_in[5] != mp_const_none) {
484+
palette = MP_OBJ_TO_PTR(mp_obj_cast_to_native_base(args_in[5], MP_OBJ_FROM_PTR(&mp_type_framebuf)));
496485
}
497486

498487
if (
@@ -563,15 +552,15 @@ STATIC mp_obj_t framebuf_scroll(mp_obj_t self_in, mp_obj_t xstep_in, mp_obj_t ys
563552
}
564553
STATIC MP_DEFINE_CONST_FUN_OBJ_3(framebuf_scroll_obj, framebuf_scroll);
565554

566-
STATIC mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args) {
555+
STATIC mp_obj_t framebuf_text(size_t n_args, const mp_obj_t *args_in) {
567556
// extract arguments
568-
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args[0]);
569-
const char *str = mp_obj_str_get_str(args[1]);
570-
mp_int_t x0 = mp_obj_get_int(args[2]);
571-
mp_int_t y0 = mp_obj_get_int(args[3]);
557+
mp_obj_framebuf_t *self = MP_OBJ_TO_PTR(args_in[0]);
558+
const char *str = mp_obj_str_get_str(args_in[1]);
559+
mp_int_t x0 = mp_obj_get_int(args_in[2]);
560+
mp_int_t y0 = mp_obj_get_int(args_in[3]);
572561
mp_int_t col = 1;
573562
if (n_args >= 5) {
574-
col = mp_obj_get_int(args[4]);
563+
col = mp_obj_get_int(args_in[4]);
575564
}
576565

577566
// loop over chars
@@ -626,18 +615,18 @@ STATIC const mp_obj_type_t mp_type_framebuf = {
626615
#endif
627616

628617
// this factory function is provided for backwards compatibility with old FrameBuffer1 class
629-
STATIC mp_obj_t legacy_framebuffer1(size_t n_args, const mp_obj_t *args) {
618+
STATIC mp_obj_t legacy_framebuffer1(size_t n_args, const mp_obj_t *args_in) {
630619
mp_obj_framebuf_t *o = mp_obj_malloc(mp_obj_framebuf_t, &mp_type_framebuf);
631620

632621
mp_buffer_info_t bufinfo;
633-
mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_WRITE);
622+
mp_get_buffer_raise(args_in[0], &bufinfo, MP_BUFFER_WRITE);
634623
o->buf = bufinfo.buf;
635624

636-
o->width = mp_obj_get_int(args[1]);
637-
o->height = mp_obj_get_int(args[2]);
625+
o->width = mp_obj_get_int(args_in[1]);
626+
o->height = mp_obj_get_int(args_in[2]);
638627
o->format = FRAMEBUF_MVLSB;
639628
if (n_args >= 4) {
640-
o->stride = mp_obj_get_int(args[3]);
629+
o->stride = mp_obj_get_int(args_in[3]);
641630
} else {
642631
o->stride = o->width;
643632
}

0 commit comments

Comments
 (0)