Live data from Hacker News

How to rewrite it in Rust

adventures.michaelfbryan.com

21–30 of 43 posts

Re: How to rewrite it in Rust

#21
post #13
post #9

We did a similar thing with a Scala -> Rust rewrite for the http://prisma.io query engine. By rewriting small components and integrating them into the existing project using Javas native interface, our small team of 5 developers were able to pull off this massive rewrite in just under a year. The resulting code base is rearchitected in a few very important ways, but mostly follows the same structure. And because we k…

I promise that this is asked genuinely and isn't some sort of veiled "gotcha!" (it's tough to tell on the internet sometimes); what was the reason for a change from Scala to Rust? I ask because Scala already has a good type system and the JVM typically has good performance nowadays, particularly with something like GraalVM, so I am actually really curious to why you felt a Rust rewrite was a good idea.

Just some reason I might make a switch from Java/C# to Rust:

* you can keep memory use quite a bit lower * you can still sometimes get large constant factors of performance improvements over the JVM in some kinds of problem domains. If this means you can run on 1 server instead of say, 5, you have a much simpler infrastructure. * startup time, especially if you are doing 'serverless' or similar * tail latency - even a good GC language will have occasional long pauses. * data race protection at compile time * easier deployment - no need to install a jvm and keep it updated

Re: How to rewrite it in Rust

#23

> This is where build scripts come in. Our strategy will be for the Rust crate to use a build.rs build script and the cc crate to invoke the equivalent commands to our make invocation Yikes - port the entire build system to cargo before you write a line of Rust. Now draw the reset of the owl! Surely's there's an incremental path for the build as well? Perhaps if you're using CMake?

You can build Rust code as a static library and make the C build system consume that instead. This is the approach that librsvg took.

Moving C build to build.rs is not necessary. It's usually done only because people used to Cargo don't like bringing CMake along. If you were to publish this as a Rust crate, it'd be slightly easier for downstream Rust users to have one less external tool to install.

Re: How to rewrite it in Rust

#24
post #9

We did a similar thing with a Scala -> Rust rewrite for the http://prisma.io query engine. By rewriting small components and integrating them into the existing project using Javas native interface, our small team of 5 developers were able to pull off this massive rewrite in just under a year. The resulting code base is rearchitected in a few very important ways, but mostly follows the same structure. And because we k…

How many lines of code? (old/new)

Re: How to rewrite it in Rust

#25
post #15
post #6

Earlier quoted context omitted.

Twice I've been part of a move from Javascript to Typescript that worked much the same. Both projects were large applications with several developers working on them. Both had been ongoing for a few years before the port started. In both projects we decided to write all new code in Typescript and convert JS to TS when we made any largish change to an existing JS file. In both cases it took around a year for us to hit…

I did something similar for a large-ish C# project and F#. The company I worked for was mostly an F# place, but we had a fairly large legacy codebase written in C#; typically we had the pattern of "if you had downtime or need to fix a bug in the C#, just rewrite the C# code into F#". Annoyingly, at least with the typical msbuild pattern, you have to be using the same language at the project level, but you can have as…

MSBuild assumes you want a 1-to-1 relation between projects and assemblies, so assemblies and projects are interchangeable. There is no hybrid C#/F# compiler than can produce one assembly out of source code from both languages, so you’ll always need at least 2 compilers to be involved, hence the need to have a csproj and an fsproj side by side in your code base.

Regardless, major kudos for your rewrite!

Re: How to rewrite it in Rust

#26

> This is where build scripts come in. Our strategy will be for the Rust crate to use a build.rs build script and the cc crate to invoke the equivalent commands to our make invocation Yikes - port the entire build system to cargo before you write a line of Rust. Now draw the reset of the owl! Surely's there's an incremental path for the build as well? Perhaps if you're using CMake?

There's a `cmake` crate that you could use from `build.rs` the same way, or you could go the other direction and have your existing build system invoke Cargo or rustc.

Re: How to rewrite it in Rust

#27
post #15

Earlier quoted context omitted.

I did something similar for a large-ish C# project and F#. The company I worked for was mostly an F# place, but we had a fairly large legacy codebase written in C#; typically we had the pattern of "if you had downtime or need to fix a bug in the C#, just rewrite the C# code into F#". Annoyingly, at least with the typical msbuild pattern, you have to be using the same language at the project level, but you can have as…

MSBuild assumes you want a 1-to-1 relation between projects and assemblies, so assemblies and projects are interchangeable. There is no hybrid C#/F# compiler than can produce one assembly out of source code from both languages, so you’ll always need at least 2 compilers to be involved, hence the need to have a csproj and an fsproj side by side in your code base. Regardless, major kudos for your rewrite!

Yeah, I knew that actually; it's still annoying :), but after awhile you kind of get used to figuring out how to split up the stuff you're going to rewrite.

It wasn't just me; it was everyone on my team, and probably everyone in the company; I'm pretty glad they did that though; F# ain't perfect, but I like it a lot better than C#.

Re: How to rewrite it in Rust

#28
post #13

Earlier quoted context omitted.

I promise that this is asked genuinely and isn't some sort of veiled "gotcha!" (it's tough to tell on the internet sometimes); what was the reason for a change from Scala to Rust? I ask because Scala already has a good type system and the JVM typically has good performance nowadays, particularly with something like GraalVM, so I am actually really curious to why you felt a Rust rewrite was a good idea.

Just some reason I might make a switch from Java/C# to Rust: * you can keep memory use quite a bit lower * you can still sometimes get large constant factors of performance improvements over the JVM in some kinds of problem domains. If this means you can run on 1 server instead of say, 5, you have a much simpler infrastructure. * startup time, especially if you are doing 'serverless' or similar * tail latency - even…

I agree with all of your points except the "startup time" and "easier deployment"; GraalVM is pretty sweet and produces nice, self-contained executables.

Re: How to rewrite it in Rust

#29
post #28

Earlier quoted context omitted.

Just some reason I might make a switch from Java/C# to Rust: * you can keep memory use quite a bit lower * you can still sometimes get large constant factors of performance improvements over the JVM in some kinds of problem domains. If this means you can run on 1 server instead of say, 5, you have a much simpler infrastructure. * startup time, especially if you are doing 'serverless' or similar * tail latency - even…

I agree with all of your points except the "startup time" and "easier deployment"; GraalVM is pretty sweet and produces nice, self-contained executables.

Prisma engineer here who's been part of the rewrite since the beginning. We tried GraalVM, but the binary size was huge and we anyways needed to write parts, such as the JDBC drivers in Rust, C or C++ due to GraalVM not being able to compile certain JVM code to a native binary.

We distribute the binary with our NodeJS package and hundreds of megabytes of binary size will not work that well for our users.

Re: How to rewrite it in Rust

#30
post #29
post #28

Earlier quoted context omitted.

I agree with all of your points except the "startup time" and "easier deployment"; GraalVM is pretty sweet and produces nice, self-contained executables.

Prisma engineer here who's been part of the rewrite since the beginning. We tried GraalVM, but the binary size was huge and we anyways needed to write parts, such as the JDBC drivers in Rust, C or C++ due to GraalVM not being able to compile certain JVM code to a native binary. We distribute the binary with our NodeJS package and hundreds of megabytes of binary size will not work that well for our users.

Oh! That's a valid reason; I'm actually surprised that GraalVM has issues not supporting JVM features, since I've been using it for Clojure code locally (though admittedly just for fun, nothing serious). Definitely makes sense if you're stuck rewriting things anyway, might as well just do it in a more modern language.

Thanks for the insight!

Post reply on HN