14 ms·
Ruby 3.2 preview 1 with support for WASM compilation
- swlkr 5y agoOne notable thing is the ruby apps in a single .wasm file. This may make ruby CLI apps easier, as well as eventually replacing things like docker or shipping your ruby code to a server.
- cpuguy83 5y agoYou don't just execute a .wasm file, it requires a runtime which will JIT compile the code into machine code and handle the (wasi) system interfaces (e.g. read, write, stat, etc).
- specialp 5y agoYes this is true with all interpreted languages. But if you consider the use-case the OP was contrasting with (Docker) that not only has the Ruby runtime, but an entire Linux OS as well.
- anamexis 5y agoWouldn't a more natural point of comparison be installing the ruby interpreter?
- zem 5y agono, because you also need to install all your dependencies
- anamexis 5y agoWell, vendor them and tar it up. You could make a ruby script in a few lines that would load the dependencies and then execute the app all as one file.
- regularfry 5y agoThat is... uh... not a few lines.
- deleted 5y ago[deleted]
- anamexis 5y agoI'm fairly confident you could write a ruby script that would untar a folder and then execute the entry point in 2-3 lines. Then the trick is just making this script and the tar binary be in one single, executable file. You could do this trivially by encoding the binary in base64, but I'm fairly sure there would be a way to have it as straight binary in well, in a way that the ruby interpreter wouldn't complain about.
- regularfry 5y agoYou're skipping the fun part. What's in that tarball? In order to gather the dependencies into the tarball in the first place, they need to be installed. And not all your dependencies are handled by `bundler`. In any interesting project you're going to be depending on system libraries that Rubygems has no way to signal to the OS that it needs. You need to `apt install <foo>` for interesting values of `foo`. That means you've got a few choices: either you document what those OS packages are and rely on the operator knowing that they need to pay attention, or you try to automagically install them when you unpack your tarball, or you include everything you need from the build system in the artefact. This means running the application and finding all the files it touches via `dlopen` and hoping you exercised all the interesting code paths. Option 1: you're back to installing dependencies. Option 2: you're wishing you'd just used `dpkg` to build yourself a .deb, and you're still installing dependencies. Option 3: now you need to figure out how to swap out OS libraries after the ruby interpreter has started, and need to get all the configuration right to point into your untarred filesystem, and you're wishing you'd just used Docker.
- anamexis 5y ago
- FooBarWidget 5y agoIf you require a runtime then it's not better than JRuby with a jar.
- exdsq 5y agoWhy would it replace docker? You still need the dependencies of the CLI app Edit: Ah I guess it’s just the WASM vm if it includes everything
- qbasic_forever 5y agoI was thinking the same thing, isn't ruby particularly hard to package as it doesn't support static compilation? It would be nice to just sidestep all of that with a hermetic little WASM distribution.
- psychoslave 5y agoNot sure it covers what you expect, but there are tools out there to convert a ruby script into a single binary executable, like https://github.com/loureirorg/rb2exe https://github.com/loureirorg/rb2exe
- schemescape 5y agoIt looks like this embeds the Ruby runtime, a virtual file system for the files, and uses WASI to interact with the OS. The portability sounds great, but I’d like to see how performance is impacted. Edit: also curious about file size.
- freedomben 5y agoTimeouts for Regexp is quite interesting. The engineering purity in me saddens at such a thought, but indeed it seems highly practical. The syntax feels a little rough although I have no ideas how to make it better: Regexp.timeout = 1.0 ... /^a*b?a*$/ =~ "a" * 50000 + "x" I think I would favor the: long_time_re = Regexp.new("^a*b?a*$", timeout: 1.0) version instead but I use the `=~` almost entirely, so that would still be a big style change. Probably end up setting a global timeout per app and then overriding for individual checks as needed?
- speedgoose 5y agoA timeout for regexps makes so much sense. And it would end all these denial of service security reports.
- deleted 5y ago[deleted]
- Thaxll 5y agoIt does not make sense to me, best solution is to have an implementation like re2 that does not have those problems. Adding a timeout is a bit strange, first because you don't know in advance how long it's going to take for large search. The timeout is a failsafe against something that should be fixed in the first place.
- messe 5y agoI think a limit on stack/recursion/backtracking depth would be tad a bit more elegant than a timeout and would keep your code behaviour the same between different machines.
- MaxLeiter 5y agois this not the halting problem? You can guarantee a certain depth isnt reached but you cant guarantee a recursion will unwind or anything like that
- deleted 5y ago[deleted]
- k__ 5y agoIs it comparable to Opal?
- AprilArcus 5y agoNot really. Opal is a source-to-source compiler that compiles Ruby to JavaScript. Ruby 3.2 compiles the whole Ruby VM and runtime to wasm, which then runs Ruby inside a real Ruby VM nested within the JS VM. A good analogy is that Opal is like PureScript, whereas Ruby 3.2 is like GHCJS.
- poisonta 5y agoI hope it performs better than Opal.
- eatonphil 5y agoThis looks awesome! I've already played around with pyodide and coldbrew doing the same thing for CPython. I use it for an in-memory playground [0] of an open-source desktop app I build [1]. I've been waiting for Ruby, Julia, and R support to add them in too. That said, I am not seeing a link in here about how to actually use this code. Is there a good tutorial/example somewhere? [0] app.datastation.multiprocess.io [1] github.com/multiprocessio/datastation
- eatonphil 5y agoFound it myself [0]! :) I think. <html> <script src="https://cdn.jsdelivr.net/npm/ruby-head-wasm-wasi@latest/dist/browser.umd.js"></script> <script> const { DefaultRubyVM } = window["ruby-wasm-wasi"]; const main = async () => { // Fetch and instntiate WebAssembly binary const response = await fetch( "https://cdn.jsdelivr.net/npm/ruby-head-wasm-wasi@latest/dist/ruby.wasm" ); const buffer = await response.arrayBuffer(); const module = await WebAssembly.compile(buffer); const { vm } = await DefaultRubyVM(module); vm.printVersion(); vm.eval(` require "js" luckiness = ["Lucky", "Unlucky"].sample JS::eval("document.body.innerText = '#{luckiness}'") `); }; main(); </script> <body></body> </html> [0] https://github.com/ruby/ruby.wasm/tree/main/packages/npm-packages/ruby-head-wasm-wasi https://github.com/ruby/ruby.wasm/tree/main/packages/npm-pac...
- alberth 5y agoDoes this imply that Rails apps could run as WASM server apps and receive a huge performance boost?
- shafyy 5y agoWhy do you think this would give them a performance boost?
- anm89 5y agoThis is by far my favorite tech talk of all time. It goes into why WASM can run faster than native code in many contexts, the reason being that it get's around the overhead of OS security rings https://www.destroyallsoftware.com/talks/the-birth-and-death-of-javascript https://www.destroyallsoftware.com/talks/the-birth-and-death...
- eatonphil 5y agoThat talk is from 2014 and Wikipedia says wasm was announced in 2017?
- AprilArcus 5y agoit post-dated and took inspiration from Mozilla's asm.js, which was highly influential on wasm.
- capableweb 5y agoasm.js was first mentioned in 2013. asm.js was eventually superseded by wasm and is pretty much the beginning of wasm as we know it. Didn't watch the talk, but could asm.js be the thing the presenter was talking about?
- time_to_smile 5y agoWhile the parent is does seem to be treading into Poe's law territory, it's not entirely correct to dismiss that talk's relationship to wasm based on the dates your quoting. Bernhardt in the talk explicitly mentions asm.js which is the precursor to wasm (it's even mentioned in the wikipedia article you skimmed a bit too quickly). asm.js was released Feburary 2013. I'm surprised HN has such a short memory, but the impetus for that talk was a clearly disturbing trend at the time implying that everything should be done in javascript. Node.js was gaining rapid popularity, people were discussing javascript as the new C for using as the language to write example code in, and while things like asm.js were exciting, they seemed to point towards the hilariously nightmarish future Bernhardt is discussing there.
- marcus_cemes 5y agoI've been very attracted to learn Ruby a couple of times, being exhausted of the JS ecosystem. Everybody who's used it seems to fall in love with it, but I can't get over just how slow it is... It takes a fresh installation of Discourse over 10 minutes to start-up again on a small underpowered VM and uses 10x as much RAM as an alternative platform such as Flarum.
- freedomben 5y agoI'm one of those people that fell in love with Ruby, and yeah the speed is the biggest downside. That said, a lot of the bulk is often Rails. I usually use Sinatra now and it's pretty light. On the smallest VM it usually starts quickly and runs fine for quite a while. One even survived an HN Hug. There are also some big improvements coming with Ruby 3 (if you aren't already upgraded to that) and more to come. But you definitely "pay" a fee in CPU/memory for the privilege of using Ruby. In most cases, it's way worth it IMHO. I've also been loving Elixir lately. It's got much the same feeling of beauty that Ruby does, and it's much lighter and lightning fast. I often measure response times in microseconds rather than milliseconds!
- inopinatus 5y agoMy developer experience is that the long initial start time (of Rails in particular) is more than offset by my productivity.
- adriand 5y agoIt’s funny, I’ve been writing Ruby code since Rails 1 and over the past few months I’ve been learning and using Node and Typescript and I find it incredibly productive compared to Rails. I get that Rails comes with a lot of great stuff out of the box, but the benefits of static typing for actually writing a lot of code are immense.
- ta988 5y agoI've been doing ruby for the last year or so coming from statically typed languages and yes... 95% of our bugs in prod would not have happened if we had a good typing system.
- syrusakbary 5y agoThis is super exciting! They also created an awesome playground to try Ruby online [1]... all powered by Wasmer/WASI [2]! [1] https://try.ruby-lang.org/playground/ https://try.ruby-lang.org/playground/ [2] https://wasmer.io https://wasmer.io
- nomilk 5y agoRegarding WASM, what are the benefits to users? Is it that html/js/css loads 2-10 times faster? Also, what's the impact on a typical rails/ruby dev? Do they have to learn anything new to enjoy improvements WASM brings, or will all the changes be 'under the hood' (i.e. in ruby and/or rails)?
- knu_ttib 5y agoIn the browser, applications can run compute expensive tasks faster with WASM. For example, decryption of compressed 3D models (https://google.github.io/draco/ https://google.github.io/draco/). AFAIK Figma uses WASM for some expensive calculations. If there is already C/C++ code, it is especially useful. However, as Javascript is already really fast, for most use cases the overhead doesn´t pay off. WASM in the browser did not take off as expected by many. WASM on the server is interesting for companies providing compute services like edge computing or traditional FAAS like AWS Lambda. As WASM has very good security features (sandboxed) it is much cheaper to provide a secure edge compute service for WASM only than one that supports Java, .NET, Node, Python, Go, ... However, neither the enduser nor the developer are the winner here. For most languages WASM is slower. For developers it is obviously easier to provide just your .NET code to the cloud provider than to compile it to WASM. Most (if not all) cloud providers also support NodeJS, so if you would need to develop an edge function I'd highly suggest just writing it in Typescript. So, WASM can help cloud providers make even more money or for very demanding web applications. Otherwise, a lot of hype ...
- nomilk 5y agoSounds useful for applications that have heavy computations client side, and for those which need better security when using Lambdas (or similar), but otherwise not particularly significant for users/devs that don't have those problems.
- heavyset_go 5y agoAnyone know how large the compiled WASM is, for say, a hello world example?
- Meai 5y agoseems to be 3.4 MB
- melony 5y agoThat's awfully small for a full Ruby interpreter in WebAssembly, is the code precompiled?
- heavyset_go 5y agoThanks
- zamalek 5y agoWhat would really excite me is adding WASM support for native extensions. Installing gems in our CI for arm64/M1 takes about 40 times longer than the actual build.
- lloeki 5y agodoes it involve a gem that has no aarch64-linux/arm64-darwin binary gem published thus falls back to compilation from the ruby gem platform? or is it because you're using docker buildx which emulates aarch64 on x86_64 through qemu?
- zamalek 5y agoThe former.
- boundlessdreamz 5y agoCI does not cache the gems?
- pvsukale3 5y agoHere is IRB running in the browser by author of this feature https://irb-wasm.vercel.app/ https://irb-wasm.vercel.app/ No installation required :)
- zwp 5y agoCute! But I get an error if I try "require 'resolv'", is this expected? <internal:/usr/local/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:85:in `require': cannot load such file -- socket (LoadError)
- djur 5y agoWASI doesn't support sockets yet, and in any case there's no browser API available to implement socket-level networking. Practical use of this would require bridging over to JS to implement HTTP requests and the like,
- norman784 5y agoWhat I can see here with WASM is that in the near future Ruby community could build their frontend frameworks so you can develop using 98% ruby in your app. There were attempts in the past to use ruby in the frontend, by compiling it to JS (opalrb), so I looking forward to see how the ruby community exploits WASM. While I don't know how good it will be it still interesting the possibilities that WASM bring to the table, no more forced to use one language for the browser, use whatever language you like!
- psychoslave 5y agoWhat do you mean by attempts? https://opalrb.com/ https://opalrb.com/ seems to be a rather active and production-ready project, isn’t it? Maybe it’s not the most popular project out there, but I don’t see a reason to flag it as a mere attempt.