Writing Python inside Rust
blog.m-ou.se
blog.m-ou.se
https://en.wikipedia.org/wiki/List_of_JVM_languages
I don't see JVM & CLR even with the dozens of language choices on top of the virtual machine runtimes as giving you ways to do "bare metal" programming. I guess one could arguably squint and say Java's JNI or C# P/Invoke and "unsafe{}" gets you some "bare metal" capabilities but that's a stretch.
I go back to the gp's sentence: "Imagine starting a project in an easy language, then migrating pieces to a faster "bare metal" language as needed in a super piecemeal way."
For example, JVM/Java doesn't have value types[1] (yet) and that's required for programmer's control of exact and efficient memory layout for many bare metal domains. It doesn't matter what flavor of JVM language you use because you're ultimately limited by the JVM's capabilities which causes excessive pointers to pointers which is inappropriate for some high-performance code.
In C#, some future tech like CoreRT might open up some more "bare metal" programming possibilities but that's not production yet[2]. I remember you couldn't even develop a Windows Explorer right-click menu shell extensions in C# because it was a bad idea to load up 2 different versions of the NET Framework runtimes (until NET 4.0). That's not even that low a level of programming and yet C++ had no such limitation.
I use C# as much as I can but I still have to do 50% my projects in C++ because JVM/CLR are not "bare metal" enough.
[1] https://en.wikipedia.org/wiki/Criticism_of_Java#Compound_val...
[2] https://github.com/dotnet/corert#user-content-net-core-runti...
Apparently you missed the Windows 8, 8.1 and 10 train regarding your "bare metal" compilation from C# and VB.NET code.
.NET 4.0 was released in 2008 and I have been able to use C++ as .NET language since 2001, so ....
In fact one of my first .NET projects, in 2002, was to integrate C++ RPC library into Managed C++, long replaced replaced by C++/CLI with .NET 2.0 release.
Going back to Java, IBM, Aicas, PTC, Gemalto will happily sell you Java compilers that generate AOT native code for embedded deployment targets, not to mention what is running on my phone, where 95% of the OS APIs are exposed only via Java and where your beloved C and C++ code needs to use JNI to access them.
Not sure what that is referring to.
>C++ as .NET language
We seem to be talking about 2 different things. Using "C++" style syntax (C++/CLI) in a managed language with GC is not what many systems programmers call "bare metal". I thought it was clear from context that my mention of C++ is traditional "real C++" such as gcc/clang/MSVC and not the C++/CLI.
>Going back to Java, IBM, Aicas, PTC, Gemalto will happily sell you Java compilers that generate AOT native code
But that's not the JVM runtime though. The JVM is what you originally wrote and that's the scope of what I was replying to: ("Just like the JVM and CLR I guess. (List_of_JVM_languages ...)")
The _JVM_ doesn't really give you low-level bare-metal programming and it doesn't matter what flavor of JVM-language you choose to run on top of it.
And at the risk of further muddying up the discussion with the tangent subject of Java AOT... do any of those Java compilers give true value type semantics or is still references with pointer chasing? I'm not familiar with those compilers.
>and where your beloved C and C++ code
"beloved"?!? Can we tone down this down a bit? I'm just trying to clarify that JVM & CLR really don't span the entire spectrum of programming all the way down to "bare metal" in the way low-level programmers are typically using that phrase. I thought I was making a neutral and factual statement. I.e. I'm not interested in an emotional flamewar.
But I agree that the tone used wasn't the best.
Also would like to know in what way compiling natively to machine code has to do with having value type semantics on the source language.
We are getting into "splitting hairs" territory but let me attempt to untangle this thread because it seems to be hung up on what "bare metal" means.
Yes, if we're using "bare metal" to only mean real semiconductor chip, WASM is not that. It's an abstract virtual machine. So yes, in that strict sense, WASM is analogous to JVM and CLR.
But.....
I'm charitably interpreting gp's comment (6gvONxR4sf7o) and he's using WASM as his _relative_ (not absolute) perspective of _that_ being "bare metal". Ok, if we play along with that, WASM is not analogous the JVM/CLR because it is lower level[1]. Thus a non-managed language like C++ can more easily target WASM-flavor-of-bare-metal for high performance rather than a managed-C++/CLI targeting .NET CLR.
Yes, it's a subtle difference. WASM is more "bare-metal-ish" than JVM ... relatively speaking. I just don't think JVM languages can really do the same thing as WASM as the Google/Mozilla/Apple/MS specifically engineered Web Assembly to be a target for low-level-bare-metal languages like C/C++. In contrast, Sun & James Gosling deliberately didn't engineer JVM Java byte code to be a compilation target for low-level C/C++.
This means something cpu-intensive like AutoCAD or possibly Adobe Premiere Pro can hypothetically be written to target WASM and will perform better than if those apps were re-written in Java to target a Java web browser plugin. E.g. Java's JVM doesn't have value types and that architecture choice is very unfriendly to storing/manipulating millions of 3d points for a CAD program. In contrast, WASM's architecture opens up a few more "bare-metal-ish" programming domains.
The various choices of JVM languages like Kotlin/Clojure/JRuby/etc actually don't address what WASM is attempting to accomplish.
[1] https://www.quora.com/How-does-Java-bytecode-compare-to-WASM...
https://www.graalvm.org/docs/reference-manual/languages/llvm...
https://www.graalvm.org/docs/reference-manual/languages/wasm...
A JVM and respective JIT compiler all written in Java.
And I still doesn't understand what WASM does for C++ that CLR doesn't do, given that I can write straight C89 or C++ with C++/CLI, just like using gcc or clang doesn't force me to use their language extensions.
Most people on JVM land, use a mix of Java, Kotlin, Scala, Clojure, Groovy, JRuby.
Whereas on CLR land it is C#, F#, VB.NET and C++/CLI (for low level stuff).
Naturally those that want to kill Java, or rather not touch Windows, aren't aware of this.
Web assembly is the browser C FFI, not some high level platform like Java or .Net. Your examples aren't comparable.
Also failure to understand that C ABI does not exist, rather it is the OS ABI from OS written in C, and that other OS, not written in C, don't have such thing as C ABI across all languages.
Examples of such OSes, IBM i, z/OS, Unisys ClearPath, UCSD, Unisys ClearPath, Classic Mac OS, UCSD, Native Oberon, Mesa/Cedar, Windows (plenty of stuff is .NET/COM/UWP nowadays), Android, ChromeOS, Garmin OS, and the Web.
So no, it isn't the browser C FFI, all major ones aren't even written in C for the past 20 years.
The majority of browsers now support Web Assembly and about half the global population has a web browser and access to the internet - and now, access to an actual universal bytecode based execution runtime by nature of being part of the web browser standards instead of an OS feature or framework installation or (god forbid) Oracle TOS.
The C FFI part was an analogy. The whole point of Web Assembly is that it can't call out to just any library on the OS.
By the way, JVM on the browser, Flash CrossBridge and PNaCL were there first in what concerns "universal bytecode based execution runtime by nature of being part of the web browser".
But I think your argument holds for CLR, were I've seen the 4 languages you mention being used altogether in the same code base.
The choice of the language would become a matter of presenting the compiled code and writing replacements for new code.
The runtime and the type system can't be replaced this way though.
Languages with good type systems and tooling support a workflow where you mostly rely on Intellisense hints and docs, and never read the code itself.
That, but taken to its logical conclusion: if you never read the source code, you don't need to store the source code.
The missing part is the ability to define algorithms as modifications of existing algorithms.
x = y // 2 # floor division
But then I decided to look at the docs:https://doc.rust-lang.org/proc_macro/struct.Span.html
And I noticed source_text, which "preserves the original source code, including spaces and comments"!!!
Why not just use this from the start then?? Seems like the easy way out, no?
(Disclaimer: I don't know Rust, can't even write hello world.)
> Is it just about convenience so you don't need to bother with putting your C++ code in a different file?
Yes, mostly. I find that having the code in place makes a big difference. I do not like useless levels of indirection and context switches while coding. This way is much better then having to edit three files (the .cpp, the ffi module, and the caller) each time I want to do a call into C++ while making sure they are in sync.
> However, in rust, there’s no way to differentiate .a.b with .a .b
Now I know that the above is incorrect. I would have never thought of spans so I thank you again
[0] https://conradludgate.com/posts/yew-css/#what-are-the-downsi...
if True:
x()
y()
if True:
x()
y()It's a brilliant hack and we are on Hacker News after all.
To try random ideas is part of learning and when you have something to share, just do that. There will always be like minded someone to pick up.
This article is just a fun hack using Rust macros.
[1] https://github.com/dgrunwald/rust-cpython
[2] http://dgrunwald.github.io/rust-cpython/doc/cpython/macro.py...
His primary use for it is building distributable binaries for Mercurial, which is written primarily in Python.
I was also amazed how much is implemented in libtorch itself as opposed to the Python wrapper, which makes much of the Torch functionality available to other languages.
[1] https://github.com/stickeritis/sticker2 https://github.com/stickeritis/sticker-transformers
Lua is often used tightly-coupled to C, and it doesn't have the significant-whitespace issues that Python shows here. Even so, I've only ever seen it used in separate `.lua` files, never embedded directly within `.c` files.
How would I go about doing that? I could put the Python code behind a little http API and call it that way, but that’s a bunch of overhead and extra stuff to maintain just to analyse some text. If I embed said Python tools in my Rust code then I can call those tools with significantly less overhead and complexity.
If you’re suggesting spawning things over shell/cmd line I’m of the opinion that this a generally bad idea.
Really, where does this fear of spawning things come from?
So the spawning of a short-lived subprocess approach has massive overhead, only suitable for multi-second workloads.
The simplest approach is via a pipe to the process' stdin/stdout I guess. Of course you have to (de)serialize your data, but you would have to to the same if you go the HTTP route, which seems far more complex. Furthermore the suggested solution probably has a nice wrapper in the language's stdlib (e.g. `check_output()` in Python land).
[0]: https://neosmart.net/blog/2020/self-compiling-rust-code/
But the macro hacks are impressive!
Cool article btw!
It seems like it's a light gray background with a slightly (not much) darker gray text. The contrast and thin font weight is terrible.
And the pink is practically vibrating on the page.
At least the blue is okay.