Typing


Monday, December 17th, 2007 at 11:45 pm, cs

Those of you who regularly poll tech sites will certainly have heard of ruby in the last two years. The fanboyism of the average ruby zealot is not ignorable and it’s impossible to escape from it.
Anyway, I won’t write about ruby and its users today (though maybe they should broaden their horizons and try something real).

However, a year ago during the big RoR hype (remember 37 signals?) plenty of web developers jumped into the ruby water. I have the impression lots of them did never use a web framework before and were that amazed about the productivity boost they experienced, they just couldn’t stop writing about their great discovery. This affected the typical discussion and made things even worse.

ruby ownez teh noobs OMG

Fortunately, nowadays that is followed by the classical

ZOMG RUBY IS TEH SLOW

and most of the ruby developers got uncovered anyway by Steve Yegge’s

Ruby fans seemed like ferret owners. They could go on and on about how much they adored their beady-eyed albino stretch-limo rats, and how cute they were, but we all knew they were just looking for attention.

Anyway. Where was I going with this? Oh yes, if you are bored and search for entertainment among ruby flame wars on the internet you will certainly read about problematic DB design, scalability and performance issues. As for the performance issues, ruby’s type system is blamed for the performance issues it has. But what is a type system?

Type system

When a program gets written, the developer normally has to deal with data. This may be anything from a simple integer to more complex data structures. Those values are stored as bytes in the memory. As you can imagine, it’s not very funny to deal with a bunch of bytes so people started to define “types” which represent specific byte arrays. Hence, instead of an array of four eight bytes, a variable with type “integer” can be used. After that everybody could pass his new and fancy types to functions but it was still a bit of a hassle.

Later somebody came up with the idea of adding functions to those types and called them “Objects”.

However, when you move around your variables on the stack, a mechanism is required which ensures that the type of your variables matches the type expected (We have a function whose only parameter is an integer. It must be forbidden that a string gets passed. Otherwise strange things would happen when the first few bytes of the string passed get interpreted as an integer). The thing which makes sure that this doesn’t happen is a Type Checking System.

There are several ways to implement such a system. The verification may occur when the program gets compiled or on demand when the program is running.

Static typing

The compiler makes sure that the type-contracts don’t get violated. This means type errors get detected very early and the compiler can also perform further optimizations. The errors detected depend on how powerful the type system is though.
In a statically typed language, refactoring tools are easier to implement and more accurate. The contract between caller and callee is clearer but this normally leads to more verbose expressions. Limitations in the typing system exist and need to explicitly overridden by the developers with casts or similar constructs. Static typing is used in Java or Haskell.

Dynamic typing

The type verification is done during run-time. This leads to some runtime overhead, allows more flexibility for the developer though.

var i = 3;
var j = "33";
var k = i + j;

Although a string is used for “j”, it’s obvious enough for the runtime what we are trying to do. More code can be written in less time like this but there is no compiler which makes sure that the statement can be executed later. Dynamic typing is used in languages like Python and Lisp.

Duck typing

Duck typing is a subset of dynamic typing. Rather than requiring a special type of a variable, just the actually used aspects are demanded. The type itself doesn’t matter. When the execution environment discovers during run-time that the required method is not implemented in the passed variable, the application stops with a run-time error.

Ruby

Ruby uses duck typing. The problem this language has is that those duck type checks need to be performed on every function call (and it’s really a problem. Check out the release notes of new versions and you will see they just talk about performance. Or watch the community get thrilled when there is a release of a fast implementation)
Ruby needs to scan and analyze the object, find the function which should be called, generate the according cpu instructions and execute it. In a statically typed language, all those steps can be precomputed by the compiler. And guess what, Ruby has to redo those validations on every function call! Unfortunately it’s just pretty common for developers to call functions all the time. Now just imagine how slow recursion is.

Thus the anti-ruby guys have certainly a point with their performance argument. It *is* slow.

However, it should also be considered that not everybody builds a website which attracts half the population on earth and shorter development cycles are worth more than an upgrade of your server.

Tags: , ,

11 Responses so far

  1. luca Says:

    I didn’t know what that typing stuff was all about. Nice post.

    Ah ruby! As if we had enough CPU :D I don’t get the hype anyway, there are other cool frameworks which shorten development cycles.

    I have the impression lots of them did never use a web framework before

    I just know ex-python (django) users. RoR is nothing for the self-made phpist.

    (though maybe they should broaden their horizons and try something real).

    :D

  2. Reto Bachmann Says:

    I don’t get the hype anyway

    Ah come on, let ruby.
    If you like the ruby language itself and the framework it’s using, install and use it for your webapps.
    If not, stick to your python/erlang/java* framework.

  3. Tobias Says:

    What I dont understand is why does everybody talks about yet another framework.
    Why not just using tomcat and reusing the code base which has (often) already been implemented in java?
    It’s java is also way more faster than those crazy dynamic interpreted stuff. You can even switch the operating system of your server, which I see as a huge advanage because of the flexibility you gain.

    @steve
    Im sure you could solve your ‘ 3 + “33″ ‘ also in a static typed language.

  4. Chrigi Says:

    Ah java is way too limited. More than 2 devs and after a year you have an unmanageable code base. (http://steve-yegge.blogspot.com/2007/12/codes-worst-enemy.html)

    The cool thing of a python/ruby/erlang/foo frameworks is that bright people can do bright things with it whereas in java bright people are forced into a scheme which was designed for dummies.

    You can even switch the operating system of your server..

    Saying java is good because it works on all platforms is like saying anal sex is good because it works on all genders.

  5. giu Says:

    Saying java is good because it works on all platforms is like saying anal sex is good because it works on all genders.

    hahaha

  6. steve Says:

    @luca
    Anyway better than defining its own xml-scripting-language :p

    @Tobias
    Yep, you are right.
    I should have given a more complex example, e.g. a function which calls a few functions of one of its arguments. Without any interface defs, inheritance stuff, etc. that would work.
    And nope, that is not possible in a static typed language.. you need some type definiton that those “undefined” method calls compile.

    (ye, you could build you reflection based method calling-imp.. but… come on :) )

    @chrigi

    Ah java is way too limited

    Funny how after all the hype it had everything and everybody turns against it. The last year I read from lots of bloggers that they gave up with java and went on to the dynamic camp or went back to c++.

  7. Mirko Stocker Says:

    Hi :)
    I’d like to add a few things:
    - There’s not only the distinction between static and dynamic typing but also between weak and strong type systems. Your example actually shows a weak type system where the string is coerced into an integer (I think that’s what php does). You can’t do the same thing in Ruby, you’ll get a TypeError exception.
    - About method-dispatching: If you call a method, or more precisely, send an object a message and the method isn’t defined, your application doesn’t stop with a runtime error but a special method is called, like method_missing (Ruby) or doesNotUnderstand (Smalltalk). And that’s exactly what makes dynamic typing so powerful.
    - “all those steps can be precomputed by the compiler” Sorry, but that’s just not true. If you have virtual methods in C++, you don’t know the binding of the method until runtime (vtable and that stuff..). And the same is true for Java as well, the specific type of a polymorphic object is generally not known before runtime.

  8. steve Says:

    Hi back :)

    Well, first thank you very much for your extensive reply

    (thx also to giusi for guiding over the hsr folks)

    @a
    yep

    @b
    Thats what I meant with run time error. “Application stops” -> the “normal” flow gets interrupted by some fallback stuff (exceptions or as you mentioned special methods which let the dev handle such cases )

    @c
    :) Yeye I know, I know.. that was too general. Compiling ‘everything’ down to instructions is not possible where CHA/CHG/RTTI is used anyway without further ‘hints’. (But I think you can ‘downcast’ virtual calls to inline them)

    Anyway, you see I skip many details as you will also notice in older posts. Thats because I know knew all my readers and just wanted to get some basic ideas across without explaining too much.

    You are a new type of reader… I will pay attention on such details and not generalize that often in future ;)

    thx again for the input.

  9. Mirko Stocker Says:

    Heh, I thought that you might be simplifying things, but I wasn’t sure :)

    Apropos “new type of reader”, can I add you to planet-hsr.ch?

  10. giu Says:

    (thx also to giusi for guiding over the hsr folks)

    you can call me an indirection pattern haha!!

  11. steve Says:

    Of course but just not now.

    I’m very new in this blogging thing and was just experimenting a bit. I like to do some restructuring first: maybe moving to a new host, do further configuration, change the things I don’t like in wordpress etc etc.

    Anyway, I will drop you an email when I think it’s ready and I’m happy how my blog looks like (maybe in one or two months).
    I would be honored to represent a humble entry in your list between Roland F. and Thomas M :)

Leave a Reply