6 ms·
So my dream is that eventually mypy (the optional python static typing module) will be extended so that python becomes a gradually typed language — able to dete
by wallscratch 5y ago
So my dream is that eventually mypy (the optional python static typing module) will be extended so that python becomes a gradually typed language — able to detect if an entire program has valid static typing, and if so, use a much higher-performance python compiler to skip over the python object system to increase code speed by 10x.
This would make python both a viable choice for rapid prototyping, as well as easy to convert to much higher performance code (just by handling some typing conflicts), and potentially making python a mega-language used even more pervasively.
- breuleux 5y agoI don't think typing is the main obstacle to Python performance, otherwise it wouldn't be that much slower than JavaScript. In fact, there are several alternative interpreters that are much faster, but they entail incompatibilities with popular packages. Static typing cannot fix this. Now, my dream would be for Python to actually enforce type annotations at runtime when they exist. Maybe add some kind of syntax to avoid doing it when it's expensive, e.g. `list[nocheck int]`, but for the love of god, enforce it, otherwise I feel like I'm just peppering my code with limp suggestions that only stand if some third party package can find all paths that lead to them, at which point I'd rather use a true statically typed language where the whole system doesn't look and smell like a janky afterthought.
- vlovich123 5y agoYou can’t compare to JavaScript as most JavaScript runtimes have a JIT. You’d have to compare against PyPy or something like that but it hasn’t seen anywhere near the amount of performance attention like any of the browser JavaScript engines. Finally, CPython is very old. That means it made decisions around multithreading it still has to live with to this day whereas the JavaScript implementations have always been single threaded and added multithreading much later (learning from the experiences of other languages making the transition). Dynamic typing is part of the problem but you’re right that it’s not the whole thing.
- codewiz 5y agoAs far as I know, all JavaScript interpreters and JIT VMs are also single-threaded (web workers aren't real threads because they can't directly share memory with the main thread).
- remexre 5y agoexcept via SharedArrayBuffer?
- vlovich123 5y agoWeb workers are real threads in the sense that they map directly to low-level threads (+ shared memory via SharedArrayBuffer which was only temporarily disabled due to Spectre). The bigger thing is that the Python runtime has a GIL because that's kind of how you solved the problem when multiple cores started becoming mainstream in the 90s. The Linux kernel had a similar GIL. It's an easy way to technically support running in a multi-threaded environment while reducing maintenance issues at the cost that you can never use more than 1 core at a time. JS runtimes don't have this problem because the runtime support layer is almost non-existent & there's no multi-threading support at all (indeed, V8 by itself comes with almost no JS APIs - there's a clear delineation between language runtime & "app runtime"). Service workers is extremely thin & works on channels instead of shared memory, further simplifying the design. The JS GC is typically written to support concurrent multi-threaded operation to minimize the "stop the world" phase runtime whereas Python's cycle detector needs to stop the world due to the GIL. These are all small decisions that add up.
- zohch 5y agoBut what other statically typed languages enforce static type constraints at runtime?
- girvo 5y agoWhile it's definitely not quite the same idea, Typescript supports run-time type predicates that inform the type system. https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates https://www.typescriptlang.org/docs/handbook/2/narrowing.htm... function isFish(pet: Fish | Bird): pet is Fish { return (pet as Fish).swim !== undefined; } Which I've found extremely useful
- de_keyboard 5y agoFlow types (for JavaScript) can be complied into run-time type-checks. Properly statically typed languages do not need run-time checks because the compiled code is already proven to be valid by the type-checker.
- zohch 5y ago> Flow types (for JavaScript) can be complied into run-time type-checks. Okay, so there are some, though flow is pretty obscure, and flow-runtime (which is the part which does this AFAICT) is even more obscure. I don't think this is a popular feature. > Properly statically typed languages do not need run-time checks because the compiled code is already proven to be valid by the type-checker. If you want that, just enable strict mode for mypy. And if you want to interface with code that is not stirctly typed, just use runtime type checks, which will inform the static type system: foo: Any = untyped_code() assert isinstance(foo, MyClass) # after this mypy will treat foo as instance of MyClass. Same can be done with other types of checks also. So if you want runtime type checking that informs the type system, you have it. I certainly don't want runtime checks for what the type system has already checked for me.
- IgorPartola 5y agoI was just looking at runtime type checking. Apparently the reason nobody does it is because of the roughly 30,000% slowdown compared to not doing it. I e said it before and I’ll say it again: runtime type checking saves you from the case where you pass None instead of an actual object but not much else. How wrong would your program have to be to pass an instance of Animal instead of an instance of BankAccount? And how quickly would it error out with “giraffe does not have an attribute routing_number”?
- umanwizard 5y ago> my dream would be for Python to actually enforce type annotations at runtime when they exist Does any mainstream language do this? For sure most don't, even statically-typed ones. I'm all for enforcement of types, but why does it need to happen at runtime?
- adsharma 5y agoIf you stick to this subset of python https://github.com/adsharma/py2many/blob/main/doc/langspec.md https://github.com/adsharma/py2many/blob/main/doc/langspec.m... The tool can convert your code to C++/Rust/Go and give you a native binary which like you observe will run faster without the python runtime. There are many open tasks that could use help.
- gcr 5y agoHow does this compare to Cython?
- sodality2 5y agoCython is a compiler, which converts (compiles) Python source code into more performant yet still compiled executables. Py2many is a transpiler, which converts source code of one language to source code of another language (which then needs to be compiled). So far, Py2many seems to support Rust and C++14, and preliminary support for Julia, Kotlin, Nim, Go and Dart.
- adsharma 5y agoYou can also think of Cython as a .pyx -> .c transpiler, but it tries to build a python C extension primarily. Py2many tries to primarily build a standalone C program without the python runtime, although it supports a --extension flag, where it generates a PyO3 extension for rust.
- takeda 5y agoNo, Cython is transpiler as well, it converts the code into C which you can then compile. If you use additional type markings it can generate more performant C code. BTW: I remember a blog article of someone who was using Cython as the primary language instead of Python.
- gpderetta 5y agoThe typical C compiler is a transpiler as well. It convert C code to assembly that is then converted by the assembler (another transpiler) to machine code. As the sibling comment notes, transpiler is not a very useful term.
- wyldfire 5y agoPyPy is able to dynamically codegen significantly faster code. Other projects have found similar success. the most challenging part is complying with the extension system because the interface strongly implies CPython. The use case for mypy is strictly for static type checking AFAIK.
- keithasaurus 5y agoI think https://mypyc.readthedocs.io/en/latest/ https://mypyc.readthedocs.io/en/latest/ is the thing you want. In my experience, it isn’t (yet) 10x performance, but it can make significant improvements.
- mixmastamyk 5y agoCython has been around for years, can be ported gradually, and sometimes the speedup is near 100x.
- G3rn0ti 5y agoStatic typing does not make programs automatically faster. The compiler also needs to know typical patterns and apply optimized assembly code for these cases like e.g. loop unrolling or binary arithmetic. Since typical python programs do not do number crunching much (NumPy delegates that to C routines), I wouldn't expect a great performance boost by a static compiler. Also note that statically compiled languages like Go e.g. still do a lot of run-time dispatching (due to the way interfaces work) which can not be removed by the compiler despite the presence of static types. Furthermore, much of perfomance is lost due to garbage collection that still needs to be done by a compiled python program. This why C programs will stay a thing when it comes to performance sensitive niches since it is fast due to the absence of language features. I'd argue a good JIT compiler will always beat a static compiler, especially for a dynamic language using a complicated syntax such as python.