12

A lot of people have expressed excitement at the potential of using inline assembly in smart contracts. After seeing this comment on reddit I'm curious to see some basic (and more advanced, if you like) examples of how inline assembly may be used to save gas or otherwise augment efficiency.

13

As the author of that module, a few things that came up:

  • Inline assembly allows you to read entire words (256 bits) from data types like string and bytes in a single operation. Solidity-stringutils uses that to do very fast string comparisons by doing subtraction on 32-byte chunks of the two strings being compared. Without assembly, you have to do this byte-by-byte.
  • Some operations aren't exposed in Solidity. For instance, the sha3 opcode takes a byte range in memory to hash, while the Solidity function of the same name takes a string. Thus, hashing part of a string would require costly string copying operations. With inline assembly, you can pass in a string and hash just the bit you care about.
  • Some things are flat out impossible without inline assembly. Solidity doesn't support getting return values from external functions that return variable length types like dynamic arrays, bytes or string, but if you know the length to expect, you can call them using inline assembly.
  • still of the opinion that many of the "libraries" that you are creating can be fully implemented into the regular compiler. Strcat for instance. – VoR0220 Apr 19 '16 at 18:17
  • 1
    @VoR0220 I don't think anyone said they couldn't, though I'm not sure why you're putting scare quotes around "libraries". If you look at other comparable languages like C, though, you'll notice these things are mostly the responsibility of a standard library. Keeping unnecessary complexity out of the compiler is usually a good idea. – Nick Johnson Apr 20 '16 at 9:48
  • And whether or not they could be implemented elsewhere is irrelevant if you need the function and it doesn't yet exist. – Nick Johnson Apr 20 '16 at 9:49
  • sorry...the scare quotes were a bad idea :) bad communication. It was late. I think that concatenation of strings for example should work in the compiler as they do already in javascript...makes for an easier scripting language, you know? – VoR0220 Apr 20 '16 at 18:19
  • @VoR0220 That's a tough one. With the current memory allocator, naive string concatenation would take O(n^2) memory and time. Given the gas costs of string ops, you wouldn't be able to do many before you ran out of gas! – Nick Johnson Apr 21 '16 at 8:44
3

I know this is an old topic, but here's a couple of tutorials I put together to cover assembly in solidity:

Functional Assembly - https://www.youtube.com/watch?v=nkGN6GwkMzU

Instructional Assembly - https://www.youtube.com/watch?v=axZJ2NFMH5Q

I go into the benefits, disadvantages, examples and debugging assembly.

2

Well for one you can do things that you cannot do with traditional assembly...a perfect example is the new string utils setup per Nick Johnson. https://github.com/Arachnid/solidity-stringutils

The feature is fairly new, but whatever your imagination takes you to and whatever you deem useful, you can do in inline assembly per the requirements of the EVM. There's even something in the docs about it.

http://solidity.readthedocs.org/en/latest/control-structures.html#inline-assembly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.