(e)WASM Code Golfing - Below Par with Nim by Jacek Sieka (Devcon5)
Devcon·Fri, Oct 9, 2020, 12:00 AM
Visit the https://archive.devcon.org/ to gain access to the entire library of Devcon talks with the ease of filtering, playlists, personalized suggestions, decentralized access on IPFS and more. https://archive.devcon.org/archive/watch/5/ewasm-code-golfing-below-par-with-nim On the horizon of Ethereum2, WebAssembly being proposed for phase 2 execution. Just like with the EVM, we'll be paying for gas and storage, and thus it's important that you're able to use contract space efficiently.In this presentation, we'll go over the WASM pipeline from program code to assembly bytecode, looking step by step at:* where storage goes* tricks and techniques to optimize for size, speed or both* how the compiler looks at and reasons about your code* what tooling is available to help you!* how the choice of programming language might affect the efficiency of your codeFor our examples, we'll be using NimPlay - an easy to use smart contract development environment currently being researched at Status. Speaker(s): Jacek Sieka Skill level: intermediate Track: Developer Infrastructure Keywords: technical Follow us: https://twitter.com/efdevcon, https://twitter.com/ethereum Learn more about devcon: https://www.devcon.org/ Learn more about ethereum: https://ethereum.org/ Devcon is the Ethereum conference for developers, researchers, thinkers, and makers. Devcon 5 was held in Osaka, Japan on Oct 8 - 11, 2019. Devcon is organized and presented by the Ethereum Foundation, with the support of our sponsors. To find out more, please visit https://ethereum.foundation/
Transcript
[Applause] welcome everyone my name is Isaac I work at status at the research they're the dominant key right now one of the bigger projects were working on is they're interesting everybody try to see you messages there's one thing you wouldn't remember about you is that I changed I would actually go into the reason why the girl I mentioned that they're gonna follow along looking at is on lines in notes are rid of the spicy so many there's a couple of people I wanted to mention like where this is coming from we were sort of sitting around and thinking whether we should start just like you punk project in name and you know as everybody every programmer know that the very first thing that you should do in your so that's sitting down and like optimizing the out of it those of you that have a manager you call this a feasibility study I know it isn't working it like we can basically and that's the basis of what we'll be talking about today Yuri friend of mine on the minim team sort of started this idea to do video tracks in this brand-new language for men sort of experimenting with expense to nice little language it has very good programming tricks they have a very nice environment for a programmer and at compile time translate a lot of things very efficient constructs for the computer to execute right the other person I wanted to mention is all the younger it wasn't he so we post like we posted is the elevator and then he started like if communicating in the back and for things that is farther than he had oh like that so together we get to where we will be going to make this thing finally we have the project actually - a smart contract environment we're experimenting with it to see what it would look like parent smart contracts becoming a template for not something you should totally check out online or there's a presentation going on or a workshop with Denmark's team from all circumstance they're sort of presenting a little bit about how you can deploy your contract some Universal mysterious mystical stuff all right so where are we we're talking what he wasn't here he wasn't one developed but as an alternative for it there one execution and may get there into execution and you know that goes a little bit back and forth whether that would be used or not for the purpose of this presentation we'll be using a very old version of us and one that's targeting is there in one any end of the minds like you know how to be in supporting what contract should be able to do how to access the air in one state a publicist and what sort of thought of as a gateway to tutoring water and so what of the stuff you'll see here if you're familiar with it Sarah one you probably recognize like the commands of the functions how it sort of looks at its enormous data and so on it's not a very difficult area to written that there are tumors to right now especially exports that cause problems and then maybe something like this will be available under theory in the form of an execution environment like that to be in the same but it doesn't really matter like wasn't one of the reasons why it wasn't as interesting is because it's this type generic VM that's being explored for a lot of different blockchain so whatever blockchain you happen to be using that is targeting was in this discussion that you're optimizing will be more or less the same and I mean why would you even go here well on the theory on on an abduction right storage is at previous so if you deploy like the large contract that's bad for everybody it's bad for your own base plane in terms of cost but for users to cos like they'll be paying lots of gas for the execution was obviously better system is just par as he hasn't worked well how does Weizen work first of all you have an environment in which your code is executing them in sandbox then you have your contact code on the one silence when you start if you write and then the environment provides functionality from the outside world right that might be if you're running this that if you're running wasn't in the browser for example that's gonna be access to the JavaScript world on whatever the JavaScript code instantiating there wasn't it has given you to play with that might give the Dom that make you like UI campus like this if you're doing many more modern stuff to standard in Facebook buzzing which gives you access to files it's basically the interface it's a little bit like what what you would expect on your current library or your sub library right that's the interface around there so you as my particular defines these functions which are basically for example comes straight from the via right storage they asked for Islamic install so it has like the function for everything in EVM that is not present in wasn't native and you could access to this from from inside here within sandbox or conduits [Music] the contract will be optimizing today's like this very small and simple WRC 20 was a challenge that the poster a year ago it's very like the token contracted transfer object you can see that their balance function and your transfer function right this is basically an example of this new play contract environment that we're talking about but this is a pretty alpha 3 degree version there's no experimental witness and that's not with it if you look at viper it's a little bit similar so you have like functions then Lazarus and eternal you like violence you can do comparisons and chance but when we give it to the compiler and we'll go through this pipeline in multiple stages right so we start with your parson pointing right the first thing that happens and then ladder and the GNU Compiler expensive in that peashooter very music macros are their basic it compiled and there's a bunch of code executes that explains it in different slightly more primitive constructs in the language right and you access this library that we were talking about the caldera coffee and so on then it goes into the compiler ast like that's my Heartland right the language will do a little bit of processing tried to fight her cases for optimization of a message that would be a and a little B and in turn has its own intermediate representation and optimizations wasn't that wasn't as this instruction set that is executed by the Basel environment later on and there will be one more translation into the native instruction set of your father and every single stage with when you're optimizing something there is like opportunity to save either time or space or whatever it is you're optimizing for today we'll be talking mostly about space this is just because I want to scope this presentation yeah and you usually like it wasn't in the last books like Dean thought generally we'll see wasn't or I mean uses WRC when it going to be like II wasn't corporately yeah so you go right there one thing I think you can execute discovered in the browser or you can execute like you could write this by hand side yeah like he wasn't what's no he was yes he wasn't defined the visit to the library and you know semantics outside wasn't so just to give you an idea of what it what this looks like right the first stage of the pipeline we take your coat and we sort of covered these low-level calls right and why would you use the higher level thing Odin they're like the experience that we as the maintain their environments that we take all these organizations that we figure out on the story tragically to this conservation rights but they don't want to bother this is the premise of why you're using a high-level language instead of stuff so you can see here the transfer function previous think was just like one line carry its expanded into a alright we're going to check if we have enough Paul later we're gonna get the color we're going to get the recipient now we're going to copy back to values the Depression positive alright the nymph compiler then combines to say hello we have another businesses kind of generic ir for lots of languages the reason why um does this waves because they can then generic optimize and there's like medium level of abstraction and it's looking even worse right here it's but there's not even really having rights yeah big memory you're offsetting pointers into these structures for nail polish disappear recognize them from their computer science class right I was like starting like here we have no specific knowledge to understand what's going on and finally we have this that we have these this another system wasn't [Music] LVN register-based wasn't snack based sled two ways of looking at the same problem this mismatch is one of the reasons why some people think is better not to go through again and you create - because I'm listening every translation step you lose a little bit of efficiency and that's true when you're going from the high level constructs like the medium better ones and every time you take a step back this one tiny little bit will be exploiting some of these things that we know our losses and you try to take them away right by being smart about progression our starting point is rapid four four four and a half kilobytes if you take like that smart contract code you end up at four kilobytes and there is a tool called twitting which gives you like where the size is suspect immensely useful so you can see that like I just the fact that we're saving is bond function names we have that to do transfer takes up a lot of space in a monitor the first thing you want to do is like make sure that you're calling your tools correctly that you want that tools that I strip away all the stuff and try to do some basic optimization just by using the compiler and I'm calling it we can bring them to clear out theater but obviously like when you're looking at the boat you can't really tell what's going on anymore but the compiler doesn't work early the execution environment doesn't really care about my special messages means no very common so the second thing is that like when we started doing this that we were thinking like alright let's do an elevated 128-bit integers and this is like distance of really not understanding us the challenge was for 64-bit integers and we wrote this with an 8 bit integers because that's what using material for storing way values well them in particular has issues with this there's no active collaboration for example so when you want to add to 128-bit numbers together you have to do piece by piece and then you have a little branch to see if you overload and then you need to add that to your number and it becomes like a fairly big chunk of rock so it was all right fine let's skip 220 bit stuff it's moved to 64-bit value struct there we go 100 bytes of site I thought it would be more I really thought it would be more because at these ad with carries and so on the you tend to take up a lot of space if you lived with lives but it turns out that we meant the optimizer does the optimizer do well the optimizer looks at your code and price to make it better right and one of the functions that graduates is begun in English implemented in a bit from like you know by following the bits which you don't have access to an instruction does this for you this is what you need to do in the basic operations and juicy stuff but Alabama is so smart that it recognizes this pattern and transition to a peace walk intrinsic before which is like what the swapper bytes one more thing that you don't have available in wasn't but when we take the step from wasn't from LLVM to IR Olivia does not know about this really so it thinks that this be Spock call is very cheap and in light sit on x86 that makes perfect sense it's not an instruction thing and you really don't want to be making a function call to access this one instruction was it not so all right let's start up in lighting for this particular function in something look we use 50 months something like this just by giving the optimizer this hint we know something better than the optimized [Music] moving on we have the language itself right NIM is as kind of like the same language when you initialize things depends to like to zero written to zeros like all values knowing this we haven't really told him that this call data property function would overwrite the data so what NIM does here in this instance is that it first writes 32 zeros to this array of reusing and then we override it with the day other puppy so in order to know the check to do these things you can go back and look at the assembly it was about but as he was going on in the earth in there are lots of zeros and I'm Grover adding those zeros with data right what we do without zero initialization we write 20 bytes of address and then is zero the rest simple optimization right 200 price you have to know your library but when you're using a library like a man to really understand what the different functions do in this series naively we thought we'd be safe with Christ max passage over we check the code they decide do we have 24 fights or not of call later that they really present an update for us to run this function of day as they want us to all right let's check in Roberta it turns out the deposit are copy if you just call it and give it like alright I want to last 24 bytes if there's not 24 bytes available it will reverse it will do this work for you which means basically lives you can just remove this and the program of function exactly the same the optimizer well the optimizer works by looking at your opponent right to figure out the best way to schedule it but it doesn't really understand all of your functions so what do we know well we know that if we're doing the transfer we're removing the value from the sender and we're giving it to the receiver in the original code we were first loading all the data I mean the transfers and saving of instead we recognize that like we don't need all this data available at the same time we can note the sender's value subtract it save it and then we can load the receivers value add it and save it and this means that we don't have to use memories for both these operations so what's left here well what's left here when we've done all these optimizations like we have your contract code and we have this big engine optimization and then we never do a craft that you can't really get rid of this is the way that we tell our contract about the environment part of the space so if you listen to Paul's presentation you would know that we can also compress your contract using save but the quick summary here is that like no every step of your of your pipeline and you can use the knowledge across the board all right all right there we go [Music] [Applause]
Automatic transcript — names and jargon may be misspelled.