Navigate Select ESC Close

An Optimization That Is Impossible In Rust

2024-09-07 Science & Technology
127.2k
2.7k
578
ThePrimeTime
ThePrimeTime
1.1m subscribers

Unlock all features

FREE: Get instant access to 10 AI summaries, chats, or transcripts per day.

Description

Recorded live on twitch, GET IN ### Article https://tunglevo.com/note/an-optimization-thats-impossible-in-rust/ By: Le Tung Vo | https://twitter.com/ltungv ### My Stream https://twitch.tv/ThePrimeagen ### Best Way To Support Me Become a backend engineer. Its my favorite site https://boot.dev/?promo=PRIMEYT This is also the best way to support me is to support yourself becoming a better backend engineer. MY MAIN YT CHANNEL: Has well edited engineering videos https://youtube.com/ThePrimeagen Discord https://discord.gg/ThePrimeagen Have something for me to read or react to?: https://www.reddit.com/r/ThePrimeagen Kinesis Advantage 360: https://bit.ly/Prime-Kinesis Get production ready SQLite with Turso: https://turso.tech/deeznuts

Top Comments (10)

@brinkoftext 2024-09-07

There are at least two Rust libraries with this exact optimization, for strings and vectors respectively. Also Rust lets you embed assembly directly in the code, in case you need one of the few niche things not supported by unsafe blocks. Some things may not lead to "clean" code but anyone who says something isn't possible in Rust probably never checked.

289 25 replies
@Baltasarmk 2024-09-07

Primogen: I do not comment on politics Also Primogen: German strings!

168 12 replies
@rumplstiltztinkerstein 2024-09-07

Hey Prime. A PhantomData is simply a type marker for the compiler. Let's say we have a trait that has functions that work with a certain generic type D. We want to apply that trait to a new Struct we created. But that struct doesn't have the type D. Like this: pub struct Something<D: SomeTraitYouNeed>{ ... } The struct has the generic marker D, but it doesn't have any of its attributes that follow the requirements of D. This will confuse the compiler because it will not be sure of how to apply the trait rules. In that case we create a PhantomData that implements those rules: pub struct Something<D: SomeTraitYouNeed> { PhantomData<D>, .... } PhantomData is just to fulfill the rules of the compiler, it will not exist in the compiled source code. Edit: Here is an example of a use for this. I once created a Worker struct for running custom functions in multi-threaded environments. It requires a trait for the parameter of the function, and another for the function it is going to run. It stores the parameters of the function, so that is okay. It doesn't store any attribute that implement the trait of the function it is going to execute. Due to that, I needed to implement a PhantomData for the function it is going to execute. Edit: For those unfamiliar with Rust. Traits are interfaces. Or, in simpler terms, a "class" with only functions. A generic is a "type that implement ("inherit") a certain trait ("Interface").

129 38 replies
@monad_tcp 2024-09-07

27:46 that's why its german, ITS OVERENGINEERED

15
@whotherelack-yi5uw 2024-09-08

The whole article is just kind of wrestling with the semantics of the language lol

13 1 replies
@Aras14 2024-09-08

In this case PhantomData is there mostly because of variance, the struct is now covariant to T because it is naturally covariant to PhantomData<T>. To explain variance: There exist three types if variance. Covariant<T>: 'a if T: 'a // a restriction in T leads to a restriction in Covariant<T> Invariant<T> if T: 'a // a restriction in T does nothing to Invariant<T> 'a: Contravariant<T> if T: 'a // a restriction in T loosens restrictions in Contravariant<T> Examples on how to make those types: struct Covariant<T>(T); struct Invariant<T>(*T); struct Contravariant<T>(fn()->T);

10
@afinewhitehorse 2024-09-07

Reminds me of strings is Pascal

6
@CjqNslXUcM 2024-09-07

What? Polars the python library is written Rust. They are not two separate things.

6
@yearswriter 2024-09-07

I really like those type of videos.

2
@JFRA24 2024-09-07

gotta optimize that Rust weapon handling

1

Unlock the Data Inside
Turn Videos into Knowledge

  • Get FREE 10/day: transcripts, summaries, chats
  • Chat with videos, export text & PDF
  • $1 free API credit for RAG, chatbots & research

Free forever plan • All features unlocked

App screenshot