news.volyx.in

A single line of code made a 24-core server slower than a laptop (pkolaczk.github.io)

617 points by Ygg2 · 1722 days ago · 195 comments on HN

Article summary

The article describes a performance issue in a Rust program where a 24-core server ran slower than a laptop due to a single line of code. The issue was caused by the use of atomic reference counting in the Arc library, which led to cache invalidation across cores. The author investigated the issue using profiling and flamegraphs, and found that the problem was due to the shared reference counters. The fix involved avoiding the sharing of reference counters by using simpler references and Rust lifetimes.

Main themes

  • performance optimization
  • atomic reference counting
  • garbage collection
  • Rust ownership system
  • concurrency
  • memory management
  • parallelism
  • language design trade-offs

What commenters say

  • The use of atomic reference counting can lead to performance issues due to cache invalidation across cores.
  • Garbage collection can avoid these issues by not using reference counting, but it may introduce other performance overheads.
  • Manual memory management can be error-prone and time-consuming, and garbage collection can be a better option for many use cases.
  • Rust's ownership system can help with concurrency, but it can also make the code more complex and harder to write.
  • Some argue that Rust's decision to throw its hands up at non-lexical destruction guarantees is a limitation, while others see it as a necessary trade-off for safety.
  • The issue of memory reclamation is hard to solve in the face of parallelism, and different languages and approaches have different trade-offs.
  • Using a concurrent garbage collector can help avoid performance issues, but it may not be suitable for all use cases.
  • Immutable data structures can help avoid performance issues related to shared mutable state, but they may not be suitable for all use cases.