wokwi / avr8js

Arduino (8-bit AVR) simulator, written in JavaScript and runs in the browser / Node.js
https://blog.wokwi.com/avr8js-simulate-arduino-in-javascript/
MIT License
463 stars 73 forks source link

Optimize opcode check #17

Closed gfeun closed 4 years ago

gfeun commented 4 years ago

Hello,

First I'd like to thank you for this great project. I am teaching the basics of embedded systems and this could be a great resource for me in the future !

While I was taking a look at the source code I came along this quick optimization in the instruction code. I hope I didn't miss something obvious.

Simply replacing if with else if provides a welcomed performance boost. All these if on every opcode adds up, so adding else if reduces the amount of checks per opcode.

However I liked the clear opcode separation by a line but the prettier doesn't allow this with else if so it is a bit ugly and condensed now.

I am not too familiar with hardware simulators but my guess would be that use of a map of opcodes or switch statements would provide even greater benefits.

With this PR the last instruction still requires evaluation of all the preceding if and else if.

Benchmarks

Before this PR:

Benchmark: cpu-benchmark
  avrInstruction: 66.145 ns(-117%)
  avrInstructionObjMap: 30.535 ns(0%)
  avrInstructionUintArray: 50.937 ns(-67%)

With this PR:

Benchmark: cpu-benchmark
  avrInstruction: 47.811 ns(-58%)
  avrInstructionObjMap: 30.208 ns(0%)
  avrInstructionUintArray: 51.428 ns(-70%)
urish commented 4 years ago

Thanks for taking the time to create this pull-request Glenn! This seems to be a simple yet clever way to improve the performance of the simulator, a sort of quick-win.

Have you tried comparing the speed of actual code (such as the blink example from the demo project)? What speed did you get and what browser/device have you used?

gfeun commented 4 years ago

Yes, i was just editing my PR to add that, you're too fast :smile:

So i observed a ~7% difference using the demo after 5s of simulation time on Firefox 74

Without PR: Simulation time: 00:05.093 (27%)

With PR: Simulation time: 00:05.218 (34%)

gfeun commented 4 years ago

Oh, I wasn't expecting such a massive difference between Firefox and Chromium performance.

Just tested on Chromium 80

Without PR: Simulation time: 00:10.218 (83%)

With PR: Simulation time: 00:10.375 (137%)

urish commented 4 years ago

Good job Glenn! I just released this as part of 0.6.1.

Where do you teach embedded systems?

I am not too familiar with hardware simulators but my guess would be that use of a map of opcodes or switch statements would provide even greater benefits.

Yeah, that's the purpose of the benchmark folder - I have started it in effort to figure out how to further improve performance. If you look at benchmark/index.ts, it checks exactly that - comparing the performance of avrInstruction v.s. using a map / UInt16Array to decode the instructions in one go.

If you are interested in further looking in these directions let me know, we can brainstorm together.

gfeun commented 4 years ago

Thank you for your reactivity !

I teach at a French university called Université de Bretagne Occidentale. It is not my main job, I only intervene as a substitute teacher for practical work on an introductory course. Practical work include teaching assembly basics, C and the use of some µController peripherals (GPIO, ADC, 7-segment display, LED, timers, buzzer). We teach using AVR libraries without Arduino abstractions to give an understanding of the low level interactions with the hardware.

What is of interest to me is that we have custom boards with all our peripherals and an atmega 2560 sitting in the middle. Since school has gone full remote we have to find ways to teach these concepts to students at home. I stumble upon your project and saw the great potential to be able to create a virtual board, or at least part of it (Button and LED would be a good starting point).

So I'm not an expert but I'll be glad to help !

I see that you have some interesting issues awaiting that may be easier for me to begin with than performance optimization :smile: I'll take a few days to familiarize with Typescript and the codebase and will keep you updated.

urish commented 4 years ago

Thanks for the explanation, that's great to hear!

Any help with the open issues will be blessed. Which peripherals do you have on your atmega2560 board?

gfeun commented 4 years ago

Our board look like this (this is our old one but peripherals are the same) image

So we make use of GPIO for leds, buttons, 7-seg and switches ADC for potentiometer and temperature sensor (Upper left) Timers to generate signals for the buzzer Usart for communication with a PC Interupts for keypad

ADC would be a great next step and interesting to implement.

urish commented 4 years ago

👍 for the ADC!

As far as the other hardware, here is some stuff we already have: LEDs are there, 7-seg, push buttons, USART, and I also see that you have an LCD there, which we have too.

So apart from ADC, the missing pieces are the keypad, buzzer , and switches.

If you want to check out the 7-seg, for instance, I created a small programming challenge that uses it, and would love to get your feedback on it:

https://wokwi.com/challenge/seven-forty-two

gfeun commented 4 years ago

Keypad is not necessary, switches shoudn't be hard and I saw that you have an early buzzer implementation in your simon game example. I'll dabble with some components and try to create an initial board webpage.

That's a good challenge and a great website. I couldn't wrap my head around the 7-seg pinout, I'll try again tomorrow.

urish commented 4 years ago

Thanks for the feedback!

What do you think about building this board page on top of the wokwi.com platform?

I have just released a beta editor that actually allows you to tinker with both the code and the hardware / explanations in the page, you can get it to by adding ?beta-editor=yeah to any playground/challenge URL, e.g.:

https://wokwi.com/playground/blink?beta-editor=yeah

This editor allows you to preview your change immediately, but it currently does not support saving the code. Once if have some code you are happy with, you can send a pull-request to the playgrounds repo, and it will go live shortly after being merged.

gfeun commented 4 years ago

I like the possibility to write lessons using markdown while including electronic components using html ! That's super neat. The dynamic reload is great too.

I don't understand however how you link the components to specific pins ? (found, with the label attribute, feels like magic :smile: )

I'd like to know, do you own wokwi ? Is it intended to be a commercial platform in the future ?

For the board I imagined an horizontally split window to fit the real board orientation. But this could be changed, i'll try to build a prototype.

A little feedback, I don't personally feel that the "comment section" is useful for this type of content. Imagining lots of comments here would distract from the lesson i think.

It's a massive ecosystem of tools you built here, that's amazing.

urish commented 4 years ago

Great!

Yes, Wokwi is a project I started together with @AriellaE. The idea is to provide a platform where people can create Arduino, Electronics and IoT courses, both paid and free, and share them with the community.

Good call about the comments section. We actually added it pretty early in the project, and so far it has only been used by a single person to report about an issue. We'll probably remove it or make it optional (so that they playground owner can decide if they want to have comments or not).

Thanks :-)