News for hackers. Stuff that matters.
Re:Implications of Rewriting a Browser Component in Rust by mrath on Thursday February 28, 2019 @10:09AM (#19271791)
I primarily use Java for my job. Security and memory related features of Rust are not an advantage compared to Java. But I like rust because it feels modern and produces efficient standalone binaries. Most of my hobby projects are in Rust now. But I would not rewrite any of my work projects in Rust even though they require ultimate performance. That would be a maintenance burden.

It is great to see people rewriting in Rust where it makes sense.

Re:Implications of Rewriting a Browser Component in Rust by bluejekyll on Thursday February 28, 2019 @11:11AM (#19272356)
For me, also primarily a Java person before Rust, it’s datarace safety that captured me.

Java’s executors improve the world, but I can’t count the number of concurrency bugs that I had in Java that are not possible in safe Rust. This isn’t just my code, btw, but large teams where it’s hard to disseminate good practices when building threadsafe code. If it had been in Rust, those issues wouldn’t have happened.

Other things that I appreciate about Rust over Java is the error handling combined with RAII, doing away with nasty bugs around try’s lacking proper finally statements for closing file handles, etc.

Java has its warts, not everything is just about memory safety.

Re:Implications of Rewriting a Browser Component in Rust by mrath on Thursday February 28, 2019 @04:34PM (#19275443)
about RAII there is quite a bit of support in recent Java versions not quite as good as Rust but there is support.

Data race is one other thing, I do have data race issues but that is very rare. Some of the static analysis tools even catch these anti patterns.

Re:Implications of Rewriting a Browser Component in Rust by agumonkey on Thursday February 28, 2019 @06:25PM (#19276430)
Curious about memory usage too
Re:Implications of Rewriting a Browser Component in Rust by bluejekyll on Friday March 01, 2019 @10:07AM (#19281250)
I think the best way to look at it is that memory usage becomes predictable and GC pause free. An application of similar scale and implementation between Java and Rust won't necessarily use less memory when in Rust.
Re:Implications of Rewriting a Browser Component in Rust by agumonkey on Friday March 01, 2019 @10:32AM (#19281494)
Naively I thought Java data model was inherently more demanding than Rust but .. I never read about Rust memory layouts.
Re:Implications of Rewriting a Browser Component in Rust by ianlevesque on Thursday February 28, 2019 @11:52AM (#19272732)
I use Java constantly in my job and recently tried rewriting a math & memory heavy component in rust to see what performance gains there might be. Surprisingly (to me) the naive rust version was ~15% slower than Java. There’s probably room for more rust optimization but it was interesting that “efficient standalone binaries” doesn’t automatically mean faster too when competing with HotSpot.
Re:Implications of Rewriting a Browser Component in Rust by carlmr on Thursday February 28, 2019 @11:58AM (#19272781)
I find where rust usually shines the most is if you do text processing. String allocations take time. In rust you can often avoid them and use things like cow to only allocate when you change something. That way often my text processing heavy scripts go twice the speed of a C++ version, and they're easier to write with Cargo, too.

Compared to highly optimized Java and C# I could often get a quite naive rust implementation to be 10x faster.

Naive rust means I didn't spend much time optimizing but I do use appropriate algorithms and to avoid unnecessary allocations.

Re:Implications of Rewriting a Browser Component in Rust by pjmlp on Thursday February 28, 2019 @11:58AM (#19272779)
Google had a fast math library for Android, which got outperformed by ART JIT compiler and was eventually deprecated.

https://developer.android.com/reference/android/util/FloatMa...

Re:Implications of Rewriting a Browser Component in Rust by QuercusMax on Thursday February 28, 2019 @12:03PM (#19272837)
My experience working on writing image processing code in Java is that rewriting the slowest bits in C++ only gave about a 10% performance boost, and this was over a decade ago. Moving stuff to the GPU was vastly more effective than doing faster CPU work.
Re:Implications of Rewriting a Browser Component in Rust by alex_duf on Thursday February 28, 2019 @12:34PM (#19273142)
When comparing speed on the JVM with speed with native languages, the only positive side you get from native binaries is cold start nowadays.

For a webapp this doesn't matter, but as we're moving towards more cloud functions it start to make a lot of sense.

That needs to be ponderated by the fact any real life application will have to access the network at bootstrap to load configuration and therefore your bottleneck will most likely be I/O.

Re:Implications of Rewriting a Browser Component in Rust by chrisweekly on Thursday February 28, 2019 @12:48PM (#19273299)
> "ponderated"

Huh! I have a fairly strong vocabulary and thought this might have been a made-up word -- but apparently it means "to weigh down or give substance to", which aligns with your intended point here. TIL

Re:Implications of Rewriting a Browser Component in Rust by alex_duf on Friday March 01, 2019 @05:26AM (#19279317)
Sorry that's directly brought from French.

English is my second language. I did a quick google search just to make sure it was correct but it's hard to gauge if a word that happen to be in the dictionary is also widely understood.

Re:Implications of Rewriting a Browser Component in Rust by vbezhenar on Thursday February 28, 2019 @12:47PM (#19273277)
The main performance gain would be memory consumption.
Re:Implications of Rewriting a Browser Component in Rust by ahartmetz on Friday March 01, 2019 @01:02AM (#19278304)
I remember reading an article about Java performance many years ago. It had an example for Java being faster than C++ and it was maths on arrays / matrices. I guess that kind of code least trips up HotSpot optimizations, so it wins due to availability of runtime information and a really good implementation of certain parts.

Though I doubt that Java usually wins against the Eigen C++ library. Eigen uses some template tricks to fold operations together (which can avoid intermediate storage and unlocks more types of restructuring optimizations) and uses SIMD extensively.

Re:Implications of Rewriting a Browser Component in Rust by pjmlp on Friday March 01, 2019 @02:34AM (#19278619)
Probably not, however Hotspot got some Intel help regarding auto-vectorization, including AVX support.
Re:Implications of Rewriting a Browser Component in Rust by mrath on Thursday February 28, 2019 @04:41PM (#19275492)
I would except best performing Java code to be slower than best performing Rust code. But comparing general cases is not going to be simple.
Re:Implications of Rewriting a Browser Component in Rust by orf on Thursday February 28, 2019 @10:18AM (#19271872)
Nullpointer exceptions are a huge thing in Java.
Re:Implications of Rewriting a Browser Component in Rust by latencyloser on Thursday February 28, 2019 @10:59AM (#19272250)
I write Java daily, I haven't seen one in a production environment in... at least recent memory. Most common NPEs I've seen can be resolved by doing two things: 1) for returning a single, nullable value, wrap it in Optional. 2) Never return null collections, always default to an empty collection instead.

More nuanced ones can often be caught by using all args constructors and requiring the constructor values with Objects.requiresNonNull() or similar. Using spring? Don't use field or setter injection, always constructor so the above applies as well. Making the state of your objects largely immutable means their state is more consistent and what is null and when becomes a lot less surprising.

Lastly, write good tests. If you're exhausting the behavior of your system with tests, these things are much less likely to surprise you later.

NPEs are definitely a problem with Java, but they're a very avoidable one as well.

Edit: I don't understand the downvotes. The parent said they're a huge deal and I'm disagreeing because I think they're relatively simple to avoid?

Re:Implications of Rewriting a Browser Component in Rust by bluejekyll on Thursday February 28, 2019 @11:32AM (#19272557)
> I think they're relatively simple to avoid?

Most issues in software fall into this category, though. The issue with Java’s null is that it’s not type safe. The language is generally a strongly typed language, except in the case of null. And until Value Types land, there is no way to express nullability to the compiler.

The arguments you make aren’t all that different than the undefined behavior arguments with C. “Just follow these simple rules and you’ll avoid all issues”, a compiler should do that for you IMO.

I can’t wait for Value Types in Java, at that point we can have real Option types.

Re:Implications of Rewriting a Browser Component in Rust by chucksmash on Thursday February 28, 2019 @12:45PM (#19273260)
Right.

No downvote, but GP post conflates "how code SHOULD be written" with "how existing code was written" in my opinion. Same with one of the earlier comments about "idiomatic C++ does bounds checking." I've seen plenty of NPEs in real, working Java.

If Mozilla isn't managing to write idiomatic C++, I don't hold out much hope for myself either.

> “Just follow these simple rules and you’ll avoid all issues”, a compiler should do that for you IMO.

This was one thing I really liked about Kotlin vs Java. It's nice that my IDE can do the work of generating getters/setters on a POJO for me, but it's not nice that the language requires me to fill my source code with that noise. To a first approximation, any task rote enough the computer could do it for me, it should not ask me to do.

See also: static languages that don't offer type inference.

Re:Implications of Rewriting a Browser Component in Rust by andrekandre on Thursday February 28, 2019 @04:12PM (#19275255)
this is just an honest question, and i’m genuinely curious, but why couldn’t Optional<T> be done as an immutable reference type... why does it need value types to be implemented first?
Re:Implications of Rewriting a Browser Component in Rust by bluejekyll on Thursday February 28, 2019 @04:29PM (#19275409)
do you mean like a final field in an class?
Re:Implications of Rewriting a Browser Component in Rust by andrekandre on Thursday February 28, 2019 @04:59PM (#19275671)
i was thinking something like that, yes
Re:Implications of Rewriting a Browser Component in Rust by bluejekyll on Thursday February 28, 2019 @10:24PM (#19277683)
It’s still not a “guarantee” that it’s not null, but you can be somewhat more confident that it isn’t if you assign it in all constructors of the type.

For params and return values you still can’t know for sure, and in all these cases it -could- be null. But yes, making all fields and variables that are Optional would help as a coding practice, except of course where you need to reassign the value in the Optional, as there is no “set” on the type.

Re:Implications of Rewriting a Browser Component in Rust by woah on Thursday February 28, 2019 @11:13AM (#19272372)
Rust basically just enforces the things you mention
Re:Implications of Rewriting a Browser Component in Rust by pjmlp on Thursday February 28, 2019 @11:59AM (#19272798)
With the cost of catching up to 25 years of tooling and libraries.
Re:Implications of Rewriting a Browser Component in Rust by bluejekyll on Thursday February 28, 2019 @12:27PM (#19273062)
We’re getting there. I dislike this argument, I’m sure the same was made in favor of C when Java came out.
Re:Implications of Rewriting a Browser Component in Rust by pjmlp on Thursday February 28, 2019 @12:42PM (#19273236)
I agree with you, it is just a call to reality to those "lets rewrite everything in language X", without consideration that there is more to it than just grammar and semantics.

Also there are plenty of domains where Java or C++ aren't even taken into consideration, in spite of decades of effort.

Just to cite Microsoft's recent security recommendation out of Blue Hat conference, regarding their own software, "use a mix of C#, Rust and constrained C++".

Re:Implications of Rewriting a Browser Component in Rust by firebird84 on Thursday February 28, 2019 @11:27AM (#19272503)
Can also use checkerframework to statically analyze your code for NPEs. It effectively augments the type system to require you to handle nulls where appropriate. It doesn't recognize I/O boundaries for you, you have to make sure those are annotated properly, but after that with sensible defaults, you can be more confident of the NPE-free-ness of your code.
Re:Implications of Rewriting a Browser Component in Rust by dotancohen on Thursday February 28, 2019 @11:25AM (#19272477)
I suppose that you are being downvoted because, though your arguments are correct, this thread is not about "how to avoid NPEs in Java" and is therefore OT.

That said, "write good tests" is impossible. You can write as many tests as you want, you can make sure those loops loop or don't, and those ifs if or don't, but you cannot write "good" tests. Tests will always miss some corner case, some input, some unexpected locale variable.

Re:Implications of Rewriting a Browser Component in Rust by keypusher on Friday March 01, 2019 @05:09AM (#19279232)
NPEs literally The Billion Dollar Mistake, and you don't think they are a big deal?

https://www.infoq.com/presentations/Null-References-The-Bill...

Re:Implications of Rewriting a Browser Component in Rust by discreteevent on Friday March 01, 2019 @08:10AM (#19280200)
In my experience null pointers and unassigned variables were a big problem in C an C++. But in java? They were never something that really caused a major issue. Most of them turn up in tests and are really easy to fix because you get an exception stack. Very few turn up in production and often it is because something else failed at runtime to cause it. If the null pointer wasn't there you would have had to deal with that other failure anyway.
Re:Implications of Rewriting a Browser Component in Rust by Eridrus on Thursday February 28, 2019 @10:21AM (#19271899)
I haven't written any Rust in a few years, but last I knew, it was very possible for a Rust program to crash. The guarantee is that it won't corrupt memory while doing so.
Re:Implications of Rewriting a Browser Component in Rust by gpm on Thursday February 28, 2019 @10:32AM (#19271985)
That's true, but 'null pointer exceptions' (which I suppose means expecting a Option<&T> to contain a &T when it is instead empty) are much rarer since nullability is made explicit.

Rust goals in life towards bugs are basically

- Isolate memory bugs to unsafe code which you rarely write.

- Make as many classes as bugs reasonably possible rarer by encoding as much as is reasonable in the type system and encouraging the programmer to think about all possible cases.

The first gets all the attention because it is the one you can make guarantees about, but the second is really just as important.

Re:Implications of Rewriting a Browser Component in Rust by jjnoakes on Thursday February 28, 2019 @10:36AM (#19272029)
It is possible, but the causes and frequencies are quite different.

In Rust runtime panics are certainly possible (array out of bounds, out of memory, etc), but the ones analogous to Java's NullPointerException have to be explicit opted into (unwrapping optionals) and do not happen implicitly.

Rust lets you handle optionals in nicer ways which lets you be sure you covered the "null" cases at compile time with no runtime panics possible, if you like.

Re:Implications of Rewriting a Browser Component in Rust by mrath on Thursday February 28, 2019 @10:41AM (#19272069)
I think that is somewhat related to the article. There a class of bugs not possible in Rust, they are also not possible in my GCed langs. unwrapping an `Option` is similar to NPE. but that code does not feel idiomatic Rust. IMO there is higher chance of making NPE mistake in Java than unwrapping None in Rust for similar logic.
Re:Implications of Rewriting a Browser Component in Rust by vaylian on Thursday February 28, 2019 @10:34AM (#19272002)
There are different reasons why a program can crash. Nullpointer exceptions are one of them. Out-of-memory situations are another. Rust protects you against the former, but not the latter. But at least in my experience I only encountered crashes when I disabled safety checks by calling .unwrap().
Re:Implications of Rewriting a Browser Component in Rust by jononor on Thursday February 28, 2019 @10:52AM (#19272175)
unwrap() does not disable safety checks, it just means you ignore/disregard error handling. It will panic and abort the program, but not cause a safety issue.

/pedantic

Re:Implications of Rewriting a Browser Component in Rust by amalcon on Thursday February 28, 2019 @11:36AM (#19272585)
This is actually an interesting distinction. The way that Rust defines "safety" is a little jargon-ish. It's probably more useful than the (highly impractical) colloquial meaning, but still different. In a rust context, "safety" means that the program is protected from a very specific set of things.

One might expect that set to contain things like crashing (panic! and friends) and leaking memory (forget). It does not, and is not intended to.

Re:Implications of Rewriting a Browser Component in Rust by atoav on Thursday February 28, 2019 @01:39PM (#19273829)
I think safety is about risk managment. Having a seatbelt on in a car is safer than not having one. This is despite the fact that most regular people really don’t desire crashing their car. By putting on a seatbelt you pay a small prize to weaken the impact of a existing risk (crashing your car).

Rust’s idea of safety is similar: it tries to reduce the impact of common risks that happen during programming. It won’t save you from logical errors — just like a seatbelt won’t save you when you decide to maneuver your vehicle into a flaming pit of lava. The only thing that would save you in that situation is not maneuvering into flaming lava pits.

That beeing said rust has a very straightforward implementation for Unit Tests and Integration tests which could help you dealing with remaining risks.

Of course you can always live on the edge, ignore seatbelts and do things like calling unwrap() on results that might not retun Ok(value) at all times. But that will bite you sooner than later.

What Rust is really good at IMO, is to make risks clear at any givrn moment in time. The routes your program can take are very well represented, especially because of the ownership model. Once you understood it you can make very strong assumptions about which part of the software is manipulating which data. These strong assumptions also help mitigating risk. It is harder in Rust to do things just wrong than in most other languages and it is still fast

Re:Implications of Rewriting a Browser Component in Rust by dullgiulio on Thursday February 28, 2019 @12:41PM (#19273221)
But the parent's point remains: a Java NPE and a panic because of unwrap are really equivalent. Not really unsafe in the C sense, but equally bad as unhandled crashes.
Re:Implications of Rewriting a Browser Component in Rust by staticassertion on Thursday February 28, 2019 @06:07PM (#19276277)
There's one really major distinction - you can `grep` for `unwrap`. You could grep for null, but that won't tell you if an NPE elsewhere is possible, and it won't hit every single case (like 3rd party libs returning null).

Making bugs grep'able is huge.

Re:Implications of Rewriting a Browser Component in Rust by jononor on Thursday February 28, 2019 @06:12PM (#19276324)
Greppable also means it is trivially lintable. And the standard linter Clippy defaults since two years to disallow unwrap()
Re:Implications of Rewriting a Browser Component in Rust by Yoric on Thursday February 28, 2019 @03:43PM (#19274987)
Assuming that nobody catches the NPE by accident or error (according to my experience in Java, it's a pretty big assumption), yes, they pretty much are.

The big difference is that, in Java, you typically get your NPE because you didn't know/didn't check that your pointer could be `null` – in other words, the default behavior of the language is to NPE. In Rust, the default behavior of the language is to inform the developer that they need to check. They may decide to explicitly assert that the pointer is not null, causing a crash if it is, but that's a conscious choice.

For instance, in my Rust code, pretty much every occurrence of `unwrap()` or `expect()` contains a comment explaining which invariant guarantees that the call will succeed. I don't think I have ever seen any comments associated to a member access in Java.

Re:Implications of Rewriting a Browser Component in Rust by Yoric on Thursday February 28, 2019 @11:25AM (#19272486)
Not exactly "disabled safety check": `unwrap()` is an assertion check (that may sometimes be optimized away by the compiler). You're still checking safety, but you're doing it in a much more terminal manner.
Re:Implications of Rewriting a Browser Component in Rust by FooBadDev on Thursday February 28, 2019 @11:15AM (#19272392)
Wouldn't using Koltin or Scala, which benefit from the engineering effort of JVM and arguably its ecosystem solve this problem ? (Since NULL is not idiomatic in either language)
Re:Implications of Rewriting a Browser Component in Rust by on_and_off on Thursday February 28, 2019 @11:25AM (#19272480)
Yes and no : I have been using kotlin for a while for Android. Currently our codebase is 75% kotlin.

Issues arose at the interface between java and kotlin. Unless there are @Nullable @NonNull annotations (and they need to be truthful), the kotlin compiler cannot know the nullability of something coming from a java method.

It can be pretty pernicious : if you use some java written libraries like Moshi (json parsing), it can also lead to crashes : IIRC if you declare a moshi generated property to be non null but it is absent from the json, it will generate an object with null, creating a crash.

Still, null is now an anecdotal issue in Kotlin. The Android framework team is working on annotating all their APIs with the corresponding nullability annotations and more and more JVM libs are also working on handling it gracefully.

It was never a huge issue to begin with even in java, just pretty cumbersome to have to add annotations everywhere and have to add some policies like 'no null collections, only empty' in a codebase to have sane handling.

Re:Implications of Rewriting a Browser Component in Rust by 14113 on Thursday February 28, 2019 @11:21AM (#19272441)
It's not idiomatic, but it's still possible. I've got NullPointerExceptions while writing fairly complex, modern Scala, so it's about as possible as with Java in my experience.