3 ms·
I think is forgotten here that one of the benefits of nominal typing is that the compiler can know that data layout at run time so performance benefits. There
by PaulHoule 2y ago
I think is forgotten here that one of the benefits of nominal typing is that the compiler can know that data layout at run time so performance benefits.
There has been so much ink spilled on the question of what kind of type systems help programmers be productive but there is not such controversy on the performance side.
- agentultra 2y agoDo you mean at compile time? I’m mostly familiar with Haskell which does type erasure. I think the means that there is no type checking at run time. I think dependently typed languages would the benefit of knowing structure at compile time enough to detect things like dead branches and impossible cases which can optimize by removing code. I’m not sure that all types are erased in such languages though.
- taeric 2y agoI assume they mean the simple fact that nominal types can be used in optimizations. Is why float versus double or int can be a big difference. For user types, explicitly, it is less of a benefit. But can still show up.
- PaulHoule 2y agoIn the case of simple types, say a 64-bit int if the compiler knows what the types are ahead of time it can store the ints in registers and add them with one assembly language instruction. Same if it is floating point. If you want to add two things in Python they could be totally different types so at runtime the program is likely to have pointers in those registers and it is going to have to look at the objects and figure out what it has to do to add them, possibly calling the __add__ method on the object. In the obvious implementation you are having to fetch the int out of memory when you could have just got it out of the register. Now many languages play tricks with pointers, we are not filling out a 64-bit address space any time soon, so we could make 'pointers' with certain bit patterns host integers and other numbers inside. Still it is a lot harder to add two of those than it is to just add two values we know are integers. With user types it is pretty much the same, particularly in single inheritance languages like Java where it is dead obvious that you can access field A of type X by adding a fixed offset to the object pointer. In a language like Python or Javascript you are looking everything up in a hashtable, even if it is a fast hashtable. (You don't consistently win using slots.) A really advanced compiler/runtime could undo some of this stuff, for instance it might be clear that in a particular case you are adding x+y and those are both going to be 64-bit ints and you can specialize it. You could do this at build time or even at runtime see that function has been called in a particular context 10,000 and you get the chance to inline that function into the function that calls it and then inline that function and then recompile it with the fastest code but it is tricky to get right. If an x that isn't an int finds its way in there you need to despecialize the function without breaking anything. PyPy shows that Python could be greatly improved over what it is, I think CPython is going to approach it in speed gradually.
- lmm 2y agoPremature optimisation is the root of all evil. If you get the semantics right then good performance will generally follow, if you make the wrong thing then it doesn't matter how fast it is.
- taeric 2y agoPerformant code in the wild largely disagrees? This is the difference between a numpy array versus a regular python list. It is stark. You are correct that you can often engineer the performance later. But layout concerns and nominal types for performance are fairly non controversial.
- thesz 2y agoPython does not do optimizations at all. In contrast, you can find Fortran compilers that can substitute your inefficient implementation of some numerical algorithm with different much more performant one.
- PaulHoule 2y agoPython has a peephole optimizer: https://akaptur.com/blog/2014/08/02/the-cpython-peephole-optimizer-and-you/ https://akaptur.com/blog/2014/08/02/the-cpython-peephole-opt...
- maccard 2y agoI think what OP meant was "Python's optimisations are not very fast", which (IMO) is a fair statement. Python is many, many things, but it's best case scenarios (without rewriting in C, at which point you're writing C not python) are slower than the naive implementations in many other languages.
- thesz 2y agoPython optimizations are not deep and/or broad. I did not mean "very fast" as in "compilation speed." Python does not attempt to apply anything more complex than that peephole optimizer, whatever that means. Judging from my experience with Python, that peephole optimizer cannot lift operations on "dictionary with default value" to a regular dictionary operations using conditionals. I had to manually rewrite my program to use conditionals to recover it's speed.
- k_g_b_ 2y agoI think you're confusing nominal with static and structural with dynamic. This might be because there's few instances of more powerful static structural types implemented in common programming languages - OCaml's polymorphic variants, Typescript (partially dynamic). Very common structural types available in many statically typed programming languages are tuples. The compiler can still know and optimize the data layout of any static structural type and tuples certainly are optimized in e.g. Rust. However, the flexibility of other structural types like polymorphic variants or set-theoretic types like unions mean that the data layout also needs to be more flexible and that comes with some overhead e.g. vs a nominal sum type (like in ML, Rust enums, etc) or struct/record. Missing data layout optimizations comes mostly due to necessary uniform representation of dynamic types - the same as for nominal types - e.g. through runtime polymorphism language features as OCaml has by default, virtual methods or Rust's trait objects. Whether a structural type system allows such dynamic features (e.g. OCaml's row polymorphism) or not is a design question - I think there's still plenty of use for structural types in a language with only compile time polymorphism (monomorphization). See also deredede's comment https://news.ycombinator.com/item?id=42191956 https://news.ycombinator.com/item?id=42191956