LAUNCHING RUBY IN 32
KILOBYTES
MIKHAIL BORTNYK
LAUNCHING RUBY IN 32 KILOBYTES
WHO AM I
▸ Mikhail Bortnyk, (not only) Ruby developer
▸ kottans.org co-founder
▸ github: @vessi
▸ twitter: @mikhailbortnyk
LAUNCHING RUBY IN 32 KILOBYTES
FOR WHY?
▸ because I can
▸ I love Ruby
▸ interested in IoT
▸ learn more about languages virtual machines
▸ learn more about optimisation
▸ get a touch with a real hardware
▸ I want to get fucking led blinking!
LAUNCHING RUBY IN 32 KILOBYTES
OPTIONS I KNEW BEFORE
▸ Ruby
▸ mRuby
LAUNCHING RUBY IN 32 KILOBYTES
RUBY
▸ Pros:
▸ full language stack
▸ very long story
▸ most of bugs cleared
▸ I have everyday work experience
▸ Cons:
▸ consumes too much memory (valgrind shows ~9Mb)
▸ too big
▸ platform-dependent
▸ bytecode is very experimental
LAUNCHING RUBY IN 32 KILOBYTES
MRUBY
▸ Pros:
▸ relatively smaller
▸ actively supported by @matz
▸ proved builds on bigger MCUs (Raspberry Pi and powerful ChipKIT boards)
▸ code can be compiled into byte code to reduce execution time
▸ Cons:
▸ still consumes too much memory (but only 4% from big Ruby, ~400kb)
▸ has complicated build system
▸ partially connected with filesystem so you can’t use FS-less configurations
LAUNCHING RUBY IN 32 KILOBYTES
LIMITATIONS
▸ Result build should:
▸ fit in 32K of RAM
▸ take less than 512K
▸ can operate without file system
▸ It will be good if result build can operate with target’s
hardware
LAUNCHING RUBY IN 32 KILOBYTES
FIRST TRY
▸ Author: Doug Bradbury
▸ Date: 19 Dec 2008
▸ Target board: Glomation GESBC-2440
▸ Target params: 400MHz ARM CPU, 128 MB SDRAM, 64
MB FLASH and Linux 2.6.18
▸ Ruby version: 1.8.6-p111
LAUNCHING RUBY IN 32 KILOBYTES
NEXT STEP (POSSIBLY NOT VERY ACCURATE)
▸ Author: kyab
▸ Date: 31 Dec 2012
▸ Target board: ChipKIT Max32
▸ Target params: PIC32MX795F512L 80MHz MCU, 128KB RAM, 512KB flash
▸ mRuby version: 1.0.0
▸ Caveats: lost Regexps, reduced heap 16 times (64 objects instead of 1024),
lost Time class, gems are disabled
▸ No REPL, only constant code evaluation
▸ requires MPIDE to cross-compile
AND I HAVE
FOUND
LAUNCHING RUBY IN 32 KILOBYTES
LAUNCHING RUBY IN 32 KILOBYTES
MRUBY/C PROTOTYPE
▸ Author: Shimane IT Open-Innovation Centre (in reality the
only developer is Kazuaki Tanaka)
▸ First presentation of idea: 11 Nov 2015, on slideshare
▸ I found this presentation and wrote email to achieve very
alpha version: 4 Mar 2016
▸ Project gone opensource: 17 Mar 2016
LAUNCHING RUBY IN 32 KILOBYTES
MRUBY/C DESCRIPTION
▸ Written in pure C, Makefiles are used to build
▸ Fits in 16K of memory
▸ Totally lost everything in stdlib, only Array, Float and Object
classes left
▸ More than 40% of VM instructions are not implemented yet
▸ Heap is statically linked (length is configurable in time of
compilation)
DEMO TIME
I AM GOING TO COMPILE RUBY AND BYTECODE,
FLASH BOARDS, BLINK LEDS AND LOOK PROUD
FOR NO REASON
LAUNCHING RUBY IN 32 KILOBYTES
LAUNCHING RUBY IN 32 KILOBYTES
WHAT HAVE I DID
▸ Added cross-compiling system (stolen idea from
micropython)
▸ Added simple HAL to avoid printf calls and remove libc
dependency (-10kb of flash size)
▸ Fixed lambda support (not totally)
▸ Added constants support
LAUNCHING RUBY IN 32 KILOBYTES
WHAT I AM PLANNING TO DO
▸ Add IREP subsequences support (IREP is byte code
sequence)
▸ Add runtime class declaration
▸ Move to dynamic memory management system
▸ Try to port lexer and compiler parts
▸ Try to implement REPL
▸ Try to implement network access (need ESP8266 board!)
LAUNCHING RUBY IN 32 KILOBYTES
THANKS SECTION
▸ To Mykhailo Odyniuk: this work would be totally
impossible without him. He provided me two
development boards I used in time of porting
▸ To Shimane ITOC and Kenji Sugihara personally for quick
replies and good presentation
▸ To Kazuaki Tanaka for the great work he did implementing
alpha and beta versions of mruby/c
LAUNCHING RUBY IN 32 KILOBYTES
KAZUAKI TANAKA
▸ The guy to say ‘Arigato!’
▸ @kaz0505 on Github
▸ Associate professor at Kyushu
Institute of Technology
▸ Profile on LinkedIn
QUESTIONS
AND ANSWERS
LAUNCHING RUBY IN 32 KILOBYTES

Let's Count Bytes! Launching Ruby in 32K of RAM

  • 1.
    LAUNCHING RUBY IN32 KILOBYTES MIKHAIL BORTNYK
  • 2.
    LAUNCHING RUBY IN32 KILOBYTES WHO AM I ▸ Mikhail Bortnyk, (not only) Ruby developer ▸ kottans.org co-founder ▸ github: @vessi ▸ twitter: @mikhailbortnyk
  • 3.
    LAUNCHING RUBY IN32 KILOBYTES FOR WHY? ▸ because I can ▸ I love Ruby ▸ interested in IoT ▸ learn more about languages virtual machines ▸ learn more about optimisation ▸ get a touch with a real hardware ▸ I want to get fucking led blinking!
  • 4.
    LAUNCHING RUBY IN32 KILOBYTES OPTIONS I KNEW BEFORE ▸ Ruby ▸ mRuby
  • 5.
    LAUNCHING RUBY IN32 KILOBYTES RUBY ▸ Pros: ▸ full language stack ▸ very long story ▸ most of bugs cleared ▸ I have everyday work experience ▸ Cons: ▸ consumes too much memory (valgrind shows ~9Mb) ▸ too big ▸ platform-dependent ▸ bytecode is very experimental
  • 6.
    LAUNCHING RUBY IN32 KILOBYTES MRUBY ▸ Pros: ▸ relatively smaller ▸ actively supported by @matz ▸ proved builds on bigger MCUs (Raspberry Pi and powerful ChipKIT boards) ▸ code can be compiled into byte code to reduce execution time ▸ Cons: ▸ still consumes too much memory (but only 4% from big Ruby, ~400kb) ▸ has complicated build system ▸ partially connected with filesystem so you can’t use FS-less configurations
  • 7.
    LAUNCHING RUBY IN32 KILOBYTES LIMITATIONS ▸ Result build should: ▸ fit in 32K of RAM ▸ take less than 512K ▸ can operate without file system ▸ It will be good if result build can operate with target’s hardware
  • 8.
    LAUNCHING RUBY IN32 KILOBYTES FIRST TRY ▸ Author: Doug Bradbury ▸ Date: 19 Dec 2008 ▸ Target board: Glomation GESBC-2440 ▸ Target params: 400MHz ARM CPU, 128 MB SDRAM, 64 MB FLASH and Linux 2.6.18 ▸ Ruby version: 1.8.6-p111
  • 9.
    LAUNCHING RUBY IN32 KILOBYTES NEXT STEP (POSSIBLY NOT VERY ACCURATE) ▸ Author: kyab ▸ Date: 31 Dec 2012 ▸ Target board: ChipKIT Max32 ▸ Target params: PIC32MX795F512L 80MHz MCU, 128KB RAM, 512KB flash ▸ mRuby version: 1.0.0 ▸ Caveats: lost Regexps, reduced heap 16 times (64 objects instead of 1024), lost Time class, gems are disabled ▸ No REPL, only constant code evaluation ▸ requires MPIDE to cross-compile
  • 10.
    AND I HAVE FOUND LAUNCHINGRUBY IN 32 KILOBYTES
  • 11.
    LAUNCHING RUBY IN32 KILOBYTES MRUBY/C PROTOTYPE ▸ Author: Shimane IT Open-Innovation Centre (in reality the only developer is Kazuaki Tanaka) ▸ First presentation of idea: 11 Nov 2015, on slideshare ▸ I found this presentation and wrote email to achieve very alpha version: 4 Mar 2016 ▸ Project gone opensource: 17 Mar 2016
  • 12.
    LAUNCHING RUBY IN32 KILOBYTES MRUBY/C DESCRIPTION ▸ Written in pure C, Makefiles are used to build ▸ Fits in 16K of memory ▸ Totally lost everything in stdlib, only Array, Float and Object classes left ▸ More than 40% of VM instructions are not implemented yet ▸ Heap is statically linked (length is configurable in time of compilation)
  • 13.
    DEMO TIME I AMGOING TO COMPILE RUBY AND BYTECODE, FLASH BOARDS, BLINK LEDS AND LOOK PROUD FOR NO REASON LAUNCHING RUBY IN 32 KILOBYTES
  • 14.
    LAUNCHING RUBY IN32 KILOBYTES WHAT HAVE I DID ▸ Added cross-compiling system (stolen idea from micropython) ▸ Added simple HAL to avoid printf calls and remove libc dependency (-10kb of flash size) ▸ Fixed lambda support (not totally) ▸ Added constants support
  • 15.
    LAUNCHING RUBY IN32 KILOBYTES WHAT I AM PLANNING TO DO ▸ Add IREP subsequences support (IREP is byte code sequence) ▸ Add runtime class declaration ▸ Move to dynamic memory management system ▸ Try to port lexer and compiler parts ▸ Try to implement REPL ▸ Try to implement network access (need ESP8266 board!)
  • 16.
    LAUNCHING RUBY IN32 KILOBYTES THANKS SECTION ▸ To Mykhailo Odyniuk: this work would be totally impossible without him. He provided me two development boards I used in time of porting ▸ To Shimane ITOC and Kenji Sugihara personally for quick replies and good presentation ▸ To Kazuaki Tanaka for the great work he did implementing alpha and beta versions of mruby/c
  • 17.
    LAUNCHING RUBY IN32 KILOBYTES KAZUAKI TANAKA ▸ The guy to say ‘Arigato!’ ▸ @kaz0505 on Github ▸ Associate professor at Kyushu Institute of Technology ▸ Profile on LinkedIn
  • 18.