tinygo-org / tinyfont

Text library for TinyGo displays
https://tinygo.org
BSD 3-Clause "New" or "Revised" License
49 stars 12 forks source link

all: avoid all heap allocations #41

Closed deadprogram closed 1 year ago

deadprogram commented 1 year ago

This PR modifies TinyFont to avoid all heap allocations, so that it can be called safely from within interrupt handlers.

It also includes a commit to fix the Makefile so running make by itself works as expected.

deadprogram commented 1 year ago

Additional benefit: binaries are slightly smaller as well :smile_cat:

sago35 commented 1 year ago

I will check on it this weekend.

sago35 commented 1 year ago

before:

sago3@B450GT3 MINGW64 ~/dev/src/tinygo.org/x/tinyfont (dev)
$ tinygo flash --target wioterminal --size short --print-allocs . ./examples/displays/
go: downloading tinygo.org/x/drivers v0.24.0
C:\Users\sago3\dev\pkg\mod\tinygo.org\x\drivers@v0.24.0\ili9341\spi_atsamd51.go:20:21: object allocated on the heap: escapes at line 20
C:\Users\sago3\dev\pkg\mod\tinygo.org\x\drivers@v0.24.0\ili9341\spi_atsamd51.go:15:16: object allocated on the heap: escapes at line 15
C:\Users\sago3\dev\src\tinygo.org\x\tinyfont\display.go:17:24: object allocated on the heap: escapes at line 17
C:\Users\sago3\dev\src\tinygo.org\x\tinyfont\concrete.go:100:20: object allocated on the heap: escapes at unknown line
C:\Users\sago3\dev\src\tinygo.org\x\tinyfont\concrete.go:97:10: object allocated on the heap: escapes at unknown line
C:\Users\sago3\dev\src\tinygo.org\x\tinyfont\concrete.go:95:20: object allocated on the heap: escapes at line 95
C:\Users\sago3\dev\src\tinygo.org\x\tinyfont\concrete.go:57:19: object allocated on the heap: escapes at line 57
C:\tinygo\tinygo\src\runtime\string.go:124:14: object allocated on the heap: size is not constant
C:\tinygo\tinygo\src\runtime\baremetal.go:41:14: object allocated on the heap: size is not constant
C:\tinygo\tinygo\src\runtime\slice.go:30:15: object allocated on the heap: size is not constant
C:\go\src\errors\errors.go:59:21: object allocated on the heap: escapes at line 59
C:\tinygo\tinygo\src\internal\task\task_stack.go:75:24: object allocated on the heap: size is not constant
C:\tinygo\tinygo\src\internal\task\task_stack.go:113:12: object allocated on the heap: escapes at line 115
   code    data     bss |   flash     ram
  12704   31364    6680 |   44068   38044

after:

sago3@B450GT3 MINGW64 ~/dev/src/tinygo.org/x/tinyfont (no-heap-alloc)
$ tinygo flash --target wioterminal --size short --print-allocs . ./examples/displays/
C:\Users\sago3\dev\pkg\mod\tinygo.org\x\drivers@v0.24.0\ili9341\spi_atsamd51.go:20:21: object allocated on the heap: escapes at line 20
C:\Users\sago3\dev\pkg\mod\tinygo.org\x\drivers@v0.24.0\ili9341\spi_atsamd51.go:15:16: object allocated on the heap: escapes at line 15
C:\Users\sago3\dev\src\tinygo.org\x\tinyfont\concrete.go:95:20: object allocated on the heap: escapes at line 95
C:\tinygo\tinygo\src\runtime\string.go:124:14: object allocated on the heap: size is not constant
C:\tinygo\tinygo\src\runtime\baremetal.go:41:14: object allocated on the heap: size is not constant
C:\tinygo\tinygo\src\runtime\slice.go:30:15: object allocated on the heap: size is not constant
C:\go\src\errors\errors.go:59:21: object allocated on the heap: escapes at line 59
C:\tinygo\tinygo\src\internal\task\task_stack.go:75:24: object allocated on the heap: size is not constant
C:\tinygo\tinygo\src\internal\task\task_stack.go:113:12: object allocated on the heap: escapes at line 115
   code    data     bss |   flash     ram
  12532   31364    6680 |   43896   38044
deadprogram commented 1 year ago

@sago35 I just modified GetGlyph as you suggested and force pushed this branch.

deadprogram commented 1 year ago

Turns out there were some additional allocations that were a problem in interrupts in the WriteLineColorsRotated() and LineWidth() functions, but they were relatively easy to take care of.

I think this PR is now ready.

deadprogram commented 1 year ago

Merging, thanks @sago35 and @aykevl for review.