←back to thread

Introducing tmux-rs

(richardscollin.github.io)
857 points Jtsummers | 1 comments | | HN request time: 0s | source
Show context
philosophty ◴[] No.44458716[source]
"Despite the generated code working, it was basically unmaintainable and 3x larger than the original C."

Which makes C2Rust seem pretty useless?

"I’ve recently reached a big milestone: the code base is now 100% (unsafe) Rust. I’d like to share the process of porting the original codebase from ~67,000 lines of C code to ~81,000 lines of Rust (excluding comments and empty lines)."

And yet somehow a hand-ported (and still unsafe) rewrite of a C program in Rust is still almost 20% larger?

If I recall, the Go gc compiler was automatically converted from 80K lines of C to 80K lines of Go. A hand-ported version would have been much smaller.

replies(1): >>44458975 #
kevincox ◴[] No.44458975[source]
> Which makes C2Rust seem pretty useless?

It does what took him 6 months in seconds. Of course it isn't perfect, failing to keep the name of constants being an obvious flaw. But presumably with a few improvements you could then spend some of that 6 months cleaning up the code and still save time. Sounds like C2Rust is almost there, but not quite yet.

> And yet somehow a hand-ported (and still unsafe) rewrite of a C program in Rust is still almost 20% larger?

Size is not a very useful metric. But the port is still half-done. He has got it working in Rust, so now he is ready to do the "hard" part of the port and actually rewrite the "basically C" code into idiomatic Rust. That is where you expect to get safety improvements and hopefully more readable code.

replies(1): >>44459452 #
philosophty ◴[] No.44459452[source]
"It does what took him 6 months in seconds."

It generated unusuable garbage code in seconds, which is nothing like what he wrote by hand in six months.

"Size is not a very useful metric."

Size is a very useful metric. Counting tokens is more accurate estimate of "size" but lines of code is a good first approximation.

The entire purpose of high level languages is to make it possible to do more with less code. Size isn't all that matters but it's very important.

Rust code is not only more verbose than C it's also much more irregular and complex. That 20% increase in lines of code is probably more like 50% increase in code complexity, and this is without safety.

Just compare tokens in the post's example:

  // cmd-kill-session.c
  RB_FOREACH(wl, winlinks, &s->windows) {
    wl->window->flags &= ~WINDOW_ALERTFLAGS;
    wl->flags &= ~WINLINK_ALERTFLAGS;
  }

  // cmd_kill_session.rs
  for wl in rb_foreach(&raw mut 
  (*s).windows).map(NonNull::as_ptr) {
      (*(*wl).window).flags &= !WINDOW_ALERTFLAGS;
      (*wl).flags &= !WINLINK_ALERTFLAGS;
  }
replies(1): >>44460935 #
1. hamandcheese ◴[] No.44460935{3}[source]
C-flavored-rust is more verbose than C, sure, but that doesn't tell you much about idiomatic Rust.