7 ms·
Crystal and Ruby do not even remotely have the same semantics. They look similar but the similarity is extremely superficial. The extra semantics that Ruby has
by chrisseaton 6y ago
Crystal and Ruby do not even remotely have the same semantics. They look similar but the similarity is extremely superficial. The extra semantics that Ruby has have a massive impact on performance, for even the most optimising implementations. This is not a reasonable comparison if you do not consider the 'slick as Ruby' part. No runtime metaprogramming! The entire Ruby ecosystem is built on runtime metaprogramming!
(Crystal is a fine language and compiler - but it's nothing to do with Ruby.)
- choxi 6y agoIt has macros at least: https://crystal-lang.org/reference/syntax_and_semantics/macros.html https://crystal-lang.org/reference/syntax_and_semantics/macr...
- watzon 6y agoCare to explain more? Having been using Ruby for about 8 years and Crystal for about 4, they actually have an extremely similar syntax and are also semantically very close. To the point where many Ruby scripts are completely valid Crystal, or at the very least require only a few changes. I do think that people trying to compare Crystal to Ruby kind of miss the point though. Ruby as an interpreted language, even optimized with JIT compilation, will never match the performance you can get out of a true compiled language. By the same token, Crystal as a compiled language will never be as quick to develop with since you have to wait for your code to compile after each change.
- nickbauman 6y agoYeah how fast is that compiler? If it's just another compiled language (rather than the kind of wicked fast compiled language like Go), my enthusiasm will be dampened...
- nine_k 6y agoIf it has reasonable incremental compilation, it can take a few seconds to compile. With good code structure, I see large Java projects compile small changes in seconds, even though compiling Java used to be a hog. You don't often rebuild from scratch during development, do you?
- abeppu 6y agoI often switch between feature branches, when working on more than one project in a repo with multiple related modules. If there's a change near the top of that dependency graph, I'm forced to not exactly rebuild from scratch, but still to rebuild quite a lot.
- nine_k 6y agoRight now I'm at the very same situation. Yes, it is frustrating. This is why things close to the top of the dependency graph should be small, well-tested, and rarely need changes. But when you still need to troubleshoot them, there's no way around recompiling a lot of stuff if you want these static guarantees :(
- nickbauman 6y agoBad language design, IMO. The language shouldn't make the writers in it worry about how to organize the code to speed up the compiler.
- nine_k 6y agoIn my case it's not even a language proper; I was using JavaScript which gives you near-zero static guarantees. I was fixing an issue in one common library; properly testing changes required rebuilding and restarting a number of containers. Unit tests only tell you so much; you need proper integration tests to see how certain things interact. If I were used a statically typechecked language (e.g. TypeScript), I could have eliminated 50%, or maybe 75% of the testing, because the compiler would check things for me before runtime. It would be drastically faster to localize and fix the bug even if the compilation increased build times 10x.
- nickbauman 6y agoOften I do because that's what CI does. This is pretty normal. But the point is having a different view of what compilation means in the developer's workflow as a language designer. Having the engineer have to think about how to organize the code for the compiler is bad design unless that organization is built into the compiler. The compiler should reject programs that are not organized for optimal compilation. And the organization required at least does not impede understanding of the code (best if it improves it). This is Go's design imprimatur and it's critically important to the success of Go. FWIW, I see large Java projects compile small changes take minutes to compile, even using hot-reload tools. Figwheel in Clojure is not like this, however: they're doing something right there.
- chrisseaton 6y ago> Care to explain more? Having been using Ruby for about 8 years and Crystal for about 4, they actually have an extremely similar syntax and are also semantically very close. To the point where many Ruby scripts are completely valid Crystal, or at the very least require only a few changes. It doesn't have Kernel#eval. It doesn't have Kernel#send. It doesn't have Kernel#binding. It doesn't has Proc#binding. It doesn't have Kernel#instance_variable_get/set. It doesn't have Binding#local_variable_get/set. It doesn't have BasicObject#method_missing. It doesn't have BasicObject#instance_eval. I could go on. All these methods have extreme far reaching non-local implications on the semantic model and practical performance, and specifically defeat many conventional optimisations. > To the point where many Ruby scripts are completely valid Crystal, or at the very least require only a few changes. You can't even load most of the Ruby standard library without these methods! And it doesn't matter if you use them or not. They're still there and they impact semantics and performance because the fact that you can use them affects performance. You can't even speculate against most of them as they're so non-local. Rails and the rest of the mainstream Ruby ecosystem fundamentally depend on them. > and are also semantically very close Sorry I super disagree with this. They look similar. Dig into it just below the surface? Start to model it formally? Not at all. Method dispatch, which is everything in Ruby, isn't even close. (Again, Crystal's great as its own thing, it's just not similar to Ruby's semantics. If you don't need Ruby's semantics or you can replicate them at compile time then maybe it's perfect for you.)
- dwheeler 6y agoAgreed. Crystal looks like it has many positive characteristics, but having similar syntax has nothing to do with having similar semantics. Without constructs like missing_method, you cannot run practically any of the Ruby ecosystem libraries, including everything involving Rails. Java and C also share a similar syntax, but that does not make that you can easily swap one for the other.
- remmargorp64 6y agoTo be fair, method_missing has caused more nightmares and problems with debugging than probably any other feature in Ruby. I actively avoid using it, and even the Rails team has massively dialed back on its use in their libraries over the years...
- eggsnbacon1 6y agoMetaprogramming doesn't need to have a performance impact. VM languages like Java, C#, and JS allow you do define and modify code at runtime JS you can redefine anything, Java support is pretty good, C# better support is coming with generators
- rantwasp 6y agoyou obviously did not do metaprogramming in Ruby. Try it and you will never look at the "metaprogramming" capabilities of other languages in the same way. no offence to JS, but JS and a proper programming language are not even the same species.
- YorickPeterse 6y agoRuby's meta-programming capabilities is why optimising Ruby is so darn difficult. It's extremely powerful, but also complicates a lot of things.
- eggsnbacon1 6y agoI reason the difference is mostly when metaprogramming can happen. In Java, redefining or adding code is very explicit, it can't just happen whenever. Same with C#. And there are lots of rules. A class can't modify itself, and there's limits to what changes you can make. And to make changes you must have control over the "classloader" that loaded that code. JS on the other hand can do most of what ruby does, to my knowledge. Objects are key value pairs so you're free to mess with them in virtually any way you please. You can also mess with the inheritance by altering JS prototype chains. I don't think metaprogramming itself has much to do with the speed of Ruby, with my admittedly limited knowledge of this stuff
- nine_k 6y agoRuby has to have runtime metaprogramming because it has no other time but run time. Compile-time metaprogramming is way safer and more performant, but for it you need a compiler.
- norswap 6y agoBut Crystal doesn't have those either.
- hamandcheese 6y agoYes it does? https://crystal-lang.org/reference/syntax_and_semantics/macros.html https://crystal-lang.org/reference/syntax_and_semantics/macr...
- shay_ker 6y agoI agree that the comparison is unfair - but I think the larger point is that there are many simple bits of Ruby that can copy/paste to Crystal with an immediate performance boost. In fact, it'd be interesting to slowly re-write a Ruby codebase into Crystal. Of course, harder than it sounds, lots of specifics to figure out.
- suzuki 6y agoYou are absolutely right. I have written Scheme interpreters in both languages. Compare https://github.com/nukata/little-scheme-in-ruby/blob/v0.3.0/scm.rb#L35-L55 https://github.com/nukata/little-scheme-in-ruby/blob/v0.3.0/... # Cons cell class Cell include Enumerable attr_reader :car attr_accessor :cdr def initialize(car, cdr) @car = car @cdr = cdr end # Yield car, cadr, caddr and so on, à la for-each in Scheme. def each j = self begin yield j.car j = j.cdr end while Cell === j j.nil? or raise ImproperListException, j end end # Cell and https://github.com/nukata/little-scheme-in-crystal/blob/v0.2.0/scm.cr#L127-L147 https://github.com/nukata/little-scheme-in-crystal/blob/v0.2... # Cons cell class Cell < Obj include Enumerable(Val) getter car : Val # Head part of the cell property cdr : Val # Tail part of the cell def initialize(@car : Val, @cdr : Val) end # Yield car, cadr, caddr and so on, à la for-each in Scheme. def each j = self loop { yield j.as(Cell).car j = j.as(Cell).cdr break unless Cell === j } raise ImproperListException.new(j) unless j.nil? end end # Cell and they will make the point clear. Ruby and Crystal are different languages, but you can translate your code from Ruby to Crystal line by line fairly easily. For the performance boost, see https://github.com/nukata/little-scheme/tree/v1.3.0#performance https://github.com/nukata/little-scheme/tree/v1.3.0#performa... which shows times to solve 6-Queens on a meta-circular Scheme as follows: * Crystal 0.34.0: crystal build --release scm.cr: 2.15 sec. * Crystal 0.34.0: crystal scm.cr: 9.88 sec. * Ruby 2.3.7: ruby scm.rb: 84.80 sec. Compiled (and complex enough) Crystal code runs 39 times faster than the equivalent Ruby code in this case.
- badrequest 6y agoThank you for the relevant example!
- deleted 6y ago[deleted]