dtex / j5e

Framework for embedded devices using ECMA-419, the ECMAScript® embedded systems API specification, based on Johnny-Five's API
https://www.j5e.dev/
MIT License
64 stars 6 forks source link

preloading #57

Closed phoddie closed 4 years ago

phoddie commented 4 years ago

Enabled preloading for the fn and led modules. This saves memory by moving the class and functions from memory to flash. It also speeds start-up. If this works for you, the same should be applied to other modules.

The change to freeze the timer object saves an entry in the alias table (and eliminates a build warning when preloading fn).

dtex commented 4 years ago

This is great!

I've been meaning to explore the options in manifest.json as I figured there were opportunities to make j5e better. I will enable pre-loading for the other classes as well.

I don't totally understand the advantage of freezing timer, but I trust you on this. Is there anything to be gained by me reviewing my other classes and making sure all mutable properties/values are stored on a private state object so I can freeze those other classes as well?

phoddie commented 4 years ago

I don't totally understand the advantage of freezing timer...

Summary: it saves 4 bytes of memory and prevents code from modifying it.

This blog post has lots of information on preload and links to documentation with even more. There are lots of details, but the basics are pretty easy. Feel free to ask questions.

Is there anything to be gained by me reviewing my other classes and making sure all mutable properties/values are stored on a private state object so I can freeze those other classes as well?

Generally, no. The timer object was special because it isn't a class. You could make it one with static methods, which would eliminate the need for the call to Object.freeze (preload freezers classes automatically). You'll see XS linker warnings if there is mutable state in your preloaded modules, so you can just clean those up as you encounter them.

Separately, I should have mentioned that my measurements of the LED blink example show RAM heap use (slots and chunks) dropping by about 10 KB from 14 KB to 4 KB, if I remember correctly. I think there's more improvement possible with some more work. I'll explore further.