Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It is pretty expensive performance vise


Is it? The VM is already already storing the tags on objects to be able to down cast. For example, in Java, the JVM is already storing the bits to remember that your `x` is a `Child` to safely raise an exception for

    Child castToChild(Object x) {
        return (Child)x;
    }
So at least it's not expensive from the standpoint of "uses more memory."

Maybe you're saying that it's more expensive because dynamic dispatch on a type like (A | B) will be slower than static dispatch on a type like A, but most languages lacking ad hoc union types will already have other ways to do dynamic dispatch (parent classes, interfaces, etc.) so it's not like adding ad hoc union types solely incentivizes people to write slower, dynamically dispatched code.

Maybe when you say "performance" you mean that the performance of the type checker is slower? That's definitely true—as ad hoc union types get larger (dozens, hundreds, heaven forbid thousands of variants) the type checker will definitely struggle.

That being said, there have been some impressive optimizations in practice to speed these up (I've spoken with members of the TypeScript team touting some really cool improvements to their union types, and I've seen members of my team submit similar but not quite as good improvements to the union types in Sorbet for Ruby). So I definitely agree that a priori, they're going to be slower to type check, but I think that they can be made fast in common cases, and their benefit in terms of power added to the type system is just a really, really good tradeoff.

So I'm not sure I agree on face value that adding ad hoc union types to a language necessarily come with a performance expense.


My understanding is that it is not the exceptions themselves, it is filling up stack traces which is considered performance taxing thing to do in jvm


Depends on how it is compiled. There’s no reason in principle the two can’t have the same performance, it’s just a difference in ergonomics.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: