1

When using operator overloading in Dart, are the operating functions resolved at compile time, or at runtime, or something else?

In which cases will the vm or dart2js be able to bypass something like this:

function add(left, right) {
    if (isPrimitive(left) && isPrimitive(right)) {
        return left + right;
    } else if (left.hasOverload("+")) {
        return left.plus(right);
    } else if (left.hasReverseOverload("+")) {
        return right.plus(left);
    } else {
        throw new Error("Cannot add objects");
    }
}

Where in dart, maybe a + b at compile time would be changed to add(a,b). This would be runtime checking, and for obvious reasons I have concerns about the performance of this. So basically, when in Dart if at all is runtime checking used?

Seki
  • 11,135
  • 7
  • 46
  • 70
Gabriel Ratener
  • 595
  • 5
  • 20
  • I'm not sure this can easily distinguished in Dart because Dart compiles at runtime. As far as I know Dart optimizes such things if they are called often enough and it has enough knowledge. Dart drops type annotations at runtime which makes this even harder to tell. – Günter Zöchbauer May 11 '15 at 20:27
  • I guess I should rephrase the question, cause dart may do something in the middle. – Gabriel Ratener May 11 '15 at 20:29

1 Answers1

2

Operators in Dart are no different from other methods on objects. The language specification allows methods to be resolved at runtime, and most of them will be.

However, a smart compiler may be able to recognize the function being called at compile-time, if it is exceedingly clear from the code which kind of values are flowing where. There is no hard-and-fast rule for when that happens, because it depends on how good an analysis the compiler is used, which can change from day to day (generally for the better). There's a big difference between the dart2js compiler, which can do all sorts of analyses and optimizations, and the VM JIT compiler which has to be very fast. In the latter case, optimization will also happen at runtime, when the optimizing recompilation knows the types actually reaching a function.

So, a Dart compiler can definitely do optimizations like the one you show. Whether and when it does so isn't possible to say in general.

All in all, don't worry until you have actually identified that a + call is a bottleneck.

lrn
  • 64,680
  • 7
  • 105
  • 121
  • 1
    What's true of Dart is true of any compiler: "what you get out, whether it be byte-code or object-code or source-code," is promised to be *functionally equivalent* to what you put in, but not identical. "Operator overloading" is definitely something that you can expect Dart to do a lot of fiddling-around with, since the target language doesn't support that notion directly. As far as performance goes, best advice is: *"Don't guess, don't try to predict. Profile."* Try to avoid writing things that might be hot-spots, but to find out if they are: Profile the (generated) code. – Mike Robinson May 14 '15 at 11:17