How To Make Your Python Packages Really Fast With RUST

Поділитися
Вставка
  • Опубліковано 19 чер 2024
  • Everyone knows that program speed isn't Python's strong point. That's why so many number-crunching data science libraries are written in C.
    BUT C can cause headaches. Segfaults are common and memory leaks are something to be feared. What if there was another option? Well, there is. It's Rust. Rust is a blazingly fast, memory-efficient and memory-safe language that's an absolute joy to work with. And I'm going to teach you how to use it to write your Python packages.
    Check out the code:
    github.com/isaacharrisholt/yo...
    Resources:
    Maturin: www.maturin.rs/
    PyO3: pyo3.rs/
    Rustup: rustup.rs/
    The Rust Book: doc.rust-lang.org/stable/book/
    uuidt: doc.rust-lang.org/stable/book/
    Polars: pola.rs
    pydantic-core: github.com/pydantic/pydantic-...
    __________________________________________
    Check out my other socials!
    🐦 Twitter ▶ / isaacharrisholt
    🖥️ Portfolio ▶ ihh.dev
    📝 Blog ▶ isaacharrisholt.com
    __________________________________________
    Timestamps:
    00:00 - Introduction and explanation
    00:46 - Prerequisites
    00:58 - Script overview
    01:19 - Maturin setup
    02:09 - Writing the Rust
    02:53 - Benchmarking our function
    04:08 - Rust limitations
    04:43 - Common gotchas
    #python #softwareengineer #rust
  • Наука та технологія

КОМЕНТАРІ • 91

  • @yadukrishnan8577
    @yadukrishnan8577 Рік тому +169

    "C has enough foot guns to cripple a centipede..." 🙂

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  Рік тому +21

      Thank you thank you.
      I was WAY too proud of that one.

    • @somethingelse401
      @somethingelse401 8 місяців тому +1

      That made me laugh way too hard

    • @colinmaharaj
      @colinmaharaj 3 місяці тому

      Im a C dev, and my foot hurts

    • @vncstudio
      @vncstudio 7 днів тому

      LOL. Nim is also great to interface with Python and very fast.

  • @IsaacHarrisHolt
    @IsaacHarrisHolt  8 місяців тому +27

    ERRATA:
    4:24 - You can use a u64 on a 32-bit machine! The compiler will do its magic and sort it all out for you.

    • @electric26
      @electric26 8 місяців тому +4

      Was wondering about that since you can use 128-bit integers on 64-bit machines. TIL, thanks :)

  • @AkilManivannan
    @AkilManivannan 9 місяців тому +14

    There are a lot of videos on this topic but this one earns a like for being focused and to the point. Thanks 🙏

  • @LowRezCat
    @LowRezCat Рік тому +11

    Why are you so underrated. You have genuinely good content, why are you still don't get any views? Keep going, good content!

  • @johanngambolputty5351
    @johanngambolputty5351 9 місяців тому +14

    Damn, after getting more comfortable with the idea of calling opencl code from either python or rust, or c code from python, wrangling c types in all cases, I've been meaning to get to calling rust from python, did not realise it can be this easy (especially for end users too), this might be exactly what I wanted, you've saved me some research :).
    I kinda like c, but I do find myself missing generics, traits and maybe sum types and pattern matching, not super fussed about automatically checked safety, you still end up having to think about what needs to be accessed from where (and apart from rust-gpu, we still don't really have this for GPGPU), but hey, automating some of that with the compiler can't be a bad thing. All in all, looking forward to using rust for more stuff, apart from when I would find a repl more convenient (data analysis and plotting). Knowing what functions can and can't fail and where you might have to think about nulls is also pretty cool.

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  9 місяців тому +2

      I'm glad you found it useful! You'll still be able to call the Rust functions from your Python REPL, thankfully. You'll just have to import the correct lib :)
      Unfortunately Rust doesn't have a REPL, though I'm sure someone's probably made one!

    • @johanngambolputty5351
      @johanngambolputty5351 9 місяців тому +2

      @@IsaacHarrisHolt I think there are a couple, but for just quickly poking around some slices in a matrix, or plots that you want to edit on the fly, good ol' python will do

  • @ofeki4533
    @ofeki4533 9 місяців тому +3

    So informative! thank u!
    I can really tell that this vid was made with love

  • @proudmoroccan8164
    @proudmoroccan8164 9 місяців тому

    Great stuff, thank you for sharing. Greetings from Morocco.

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  9 місяців тому +1

      Awesome! I love Morocco. I want to go back at some point :)

  • @mCoding
    @mCoding 8 місяців тому +1

    Great video! I wonder what packages I'm already using are actually written in Rust.

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  8 місяців тому

      Hey James! Big fan 😅 and thank you!
      I've actually made another video which goes over that, though they're the obvious ones again. Polars, Pydantic, Ruff. There are more and more where authors are choosing to use Rust instead of C/C++ for the critical path. The PyO3 readme has some examples: github.com/PyO3/pyo3

  • @krestofelo8757
    @krestofelo8757 Рік тому

    Hi! I liked the presentation u made and was wondering what software u used for making it.

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  Рік тому +1

      I use Obsidian with the Advanced Slides plugin using the Monokai theme :)

  • @ChrisHalden007
    @ChrisHalden007 9 місяців тому +1

    Great video. Thanks

  • @ErikCampobadal
    @ErikCampobadal 8 місяців тому +1

    I'm curious by your comment on "u64 might not be supported on every machine". What about u128? I am sure this is more than enough for 99.9% of use-cases.

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  8 місяців тому +1

      I was actually incorrect about this! I added a correction in the pinned comment, but essentially you can use u64 or u128 for this too. Of course, there will potentially be memory constraints, especially for larger Fibonacci numbers, but they can be easily overcome using a better algorithm

  • @nevokrien95
    @nevokrien95 9 місяців тому

    With code that needs to take data owned by a different process do some modifications to it and give back control
    (Like python packages cuda ml kernals etc)
    Just us8ng pointers seems so nice and simple

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  9 місяців тому

      I'm not entirely sure how this works in that regard. I haven't looked into whether PyO3 gets values by reference or by value. I'll have a look!

  • @anishbhanushali
    @anishbhanushali 9 місяців тому +1

    aaaand ... subscribed !!

  • @filipengstrom3639
    @filipengstrom3639 8 місяців тому +3

    Amazing video. You deserve more subscribers than mr beast.

  • @hanshosel7431
    @hanshosel7431 5 місяців тому

    Hey great tutoriel sadly the command maturin doesnt seem to work. My maschine cant find a cmdlet, function, script or executable Program named Maturin. Maturin is installed with pip just like you did in the tutoriel. Thanks for your time and help.❤

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  5 місяців тому

      Hey! It's likely that your pip binaries aren't in your PATH. I'd have a quick Google to find out how to do that for your machine and OS.

  • @quintencabo
    @quintencabo 9 місяців тому +2

    C can be fun. It is super simple once you get the hang of it.

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  9 місяців тому +1

      True! C is harder to get started with from a tooling perspective though. Which compiler do I use? What formatter? etc. etc.
      Rust doesn't give you a choice, and all the tooling is in a single CLI app, which makes it really easy to start. Yes, the language is a bit more difficult, but it's much easier to write safer code in Rust than C

    • @user-sb5vt8iy5q
      @user-sb5vt8iy5q 8 місяців тому

      @@IsaacHarrisHoltI think it's better to have a choice, all of rust currently depends on LLVM...

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  8 місяців тому

      That's not inherently a bad thing. My point is that the barrier for entry to getting started writing Python extensions with Rust is lower than that of C, mostly thanks to the amazing tooling built by the Rust community.

    • @snesmocha
      @snesmocha 8 місяців тому

      ⁠@@IsaacHarrisHoltwhy does this need to be such a difficult choice. Windows you use mingw for multi comparability or msvc for performance, Mac you use clang, and Linux you use gcc. And do people really need intelisense so badly? I’ve personally never used intelisense and just use default vim lol

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  8 місяців тому

      I think you've just proven my point 😅 for Rust it's cargo on everything!

  • @osky._.
    @osky._. 8 місяців тому +1

    What is the name of the font used in vscode?

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  8 місяців тому

      I use JetBrains Mono!

    • @osky._.
      @osky._. 3 місяці тому

      @@IsaacHarrisHolt Im late haha thanks

  • @bogaczew
    @bogaczew 9 місяців тому +2

    don't know about it. there are good, tested modules like numpy, to make your counts in python, they are fast.

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  9 місяців тому +5

      They're really fast! Because they're written in C 😉

  • @KrzysztofDerecki
    @KrzysztofDerecki 9 місяців тому

    Why would you recommend Rust for someone with Python background and not Nim?

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  9 місяців тому

      Good question! Mostly because I'm not familiar with it, but also Rust and Python is a very popular combination at the moment and I couldn't find many good videos on the topic, so I decided to fill the gap.

  • @filipengstrom3639
    @filipengstrom3639 8 місяців тому

    I tried to create and return a simple list containing 1 million integers in range with the following:
    #[pyfunction]
    fn create_integer_list(n: usize) -> PyResult {
    let mut result = Vec::new();
    for i in 0..n {
    result.push(i);
    }
    Ok(result)
    }
    when I compile it in RustRover it runs in 8 ms. However, when I create it in maturin as per your instructions, it takes roughly 110 ms to run. Any idea what's causing it? I ran maturin develop --release

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  8 місяців тому

      It's very possibly the interaction with Python. When you say you're compiling it in RustRover, are you simply compiling it as a Rust binary or as a Python library?

    • @filipengstrom3639
      @filipengstrom3639 8 місяців тому +1

      @@IsaacHarrisHolt Sorry I was unclear, I meant that I ran the same code as above in RustRover which worked great, as opposed to when I ran that code through maturin. I have narrowed it down to the PyResult return of my function. It seems like returning a vec and "retrieving" a list in python is really slow. Gonna see if there is a way to return a list of values in a better way

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  8 місяців тому

      That makes sense. The communication with the Python layer will be what takes up most of the time in your case, I would imagine.

  • @FirstLast-vn4kl
    @FirstLast-vn4kl Рік тому

    nice to see a fellow wsl user

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  Рік тому

      Only for videos and work! I use Ubuntu for most of my personal projects, and then WSL on my work laptop and for recording

  • @soniakolasinska3850
    @soniakolasinska3850 3 місяці тому

    Let's not fall into syntax similarity trap. Rust is wonderful language, and I'd use it for other reasons than similarity of syntax. In fact popular modern languages such as Rust, Typescript, Scala, and typed Python share syntax with good old Pascal, which would be the first programming language that you would learn in my days (except basic). Personally, I have rewritten PoCs in Python in C++, and I didn't find it any difficult, actually I could achieve most of those rewrites by copy & paste of Python code, and then run some VI macros to add types, compile and all works! I don't think it is that simple with Rust.

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  3 місяці тому

      You make very good points! I usually only mention the syntax similarity because it slightly reduces people's "fear" of Rust. Lots of folks hear that it's hard and then don't want to try it, so it's just another reason it might not be that scary.
      Naturally, it's not the most important thing when considering which language to use to extend Python. Honestly, I think the most important consideration is whether you even SHOULD be using a different language when there are so many things you can do to optimise Python code first.

  • @CamaradaArdi
    @CamaradaArdi 8 місяців тому +2

    What are you talking about? u64 and u128 are supported in 32 bit machines lol, the compiler fixes that for you. Please add this correction as that is highly misleading.
    That being said, it's very rare to build for a 32 bit architecture in 2023 if you're not in embedded, and there you have a whole lot of other things to worry about.

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  8 місяців тому

      I've added a pinned comment with this information. Thanks for calling me out!

  • @luisalejandroacunalopez3662
    @luisalejandroacunalopez3662 9 місяців тому

    I wish there was anything similar for doing it with c++ and pybind

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  9 місяців тому

      I'd not heard of pybind! It looks like a minimal version of Boost, but it would be nice if there was a build tool for it. Technically maturin and PyO3 are separate tools, so there wouldn't be anything preventing you from creating the equivalent for pybind11.

  • @archniki_
    @archniki_ 9 місяців тому +1

    I try learn Julia but there are only English resources

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  9 місяців тому

      That's a shame! But it also means there's a gap in the market 😉

  • @denizsincar29
    @denizsincar29 Рік тому +2

    wow, i did it!!! With poetry!

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  Рік тому +1

      You got this working with Poetry? Awesome! What was the trick?

    • @denizsincar29
      @denizsincar29 Рік тому

      @@IsaacHarrisHolt in pyproject toml specify build backend as maturin and something else a littlebit. can i have your contact? I have telegram, whatsapp and email. I could provide a bit more detail because now my laptop is closed and i forgot what i actually did.

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  Рік тому

      My email address should be available on my channel's About page

  • @pablodenapoli1667
    @pablodenapoli1667 Місяць тому

    Better use something more similar to Pyrhon and easier like cython or for instance D or nim..

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  Місяць тому +1

      I've not tried Cython, but Nim is good, I agree

  • @joaopauloalbq
    @joaopauloalbq 9 місяців тому +2

    Mojo 👀

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  9 місяців тому

      Perhaps! I made a video on Mojo, but it seems to be a lot further along now. I'll need to take a closer look at some point

  • @loek8638
    @loek8638 Рік тому

    what ide is this?

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  Рік тому +2

      It's PyCharm! I'm using the new UI, dark theme and the red-green colour blindness adjustment turned on, if you're curious about the theme :)

  • @yash1152
    @yash1152 8 місяців тому +1

    C++ has enough leg bombs to cripple a tank

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  8 місяців тому

      This is why we Rust 👀

    • @yash1152
      @yash1152 8 місяців тому +1

      @@IsaacHarrisHolt ohkay, i think i will start with rust then. one question - does it have proper tooling - like the IDE documentations & linting etc?? editors which i have available: vscodium, kate, intellij.

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  8 місяців тому +1

      Yes! Rust's tooling is amazing. It has an LSP called rust-analyzer which you'll be able to use in both VSCodium and Kate, and all the JetBrains IDEs have a Rust plugin available (JetBrains are also working on a Rust IDE).
      I'd recommend looking at the Rust book and watching a few UA-cam videos to get started :)

  • @jordixboy
    @jordixboy 8 місяців тому +1

    C is fun tbh

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  8 місяців тому

      I'm not denying it! I just prefer Rust for Python extensions :)

  • @amortalbeing
    @amortalbeing 9 місяців тому +3

    This is good for rust developers but not C or C++ for obvious reasons.
    anything that enriches python echo system is welcomed by me.

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  9 місяців тому +2

      I don't think it's particularly BAD for C/C++ devs. They can continue to write their language - it just gives people another option!

  • @PaulJaros
    @PaulJaros Рік тому +6

    I'm both a python (on job) and a rust (newbie and hobby) programmer. And before I start my criticism: Yes, pure python code will be slower than rust code most of the time.
    My issue is the abundance of oversimplified benchmarks on the internet like this one. Please stop doing those. New programmers will pick up those examples and will think these are mostly okay. Please, use real-world problems whenever possible and solve it with your best effort on both sides. Don't trust the results and don't jump on conclusions. Verify the results as good as you can and be as transparent as possible when publishing the results.

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  Рік тому +4

      While I do typically agree with your approach, this video is aimed at people who can't really write any Rust, so I needed an example that would easily translate from Python to Rust without causing any confusion.
      For that reason, I avoided anything with any data structures as they're topics that require some understanding of the Rust borrow checker.

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  Рік тому +1

      Ah, that's a good suggestion. I'll keep that in mind for next time, thank you!

    • @seriouce4832
      @seriouce4832 9 місяців тому +3

      @@IsaacHarrisHolt I fully agree with your perspective here. I guess the video was about "how to run rust code in python", not "look how slow python is"...

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  9 місяців тому +2

      Yes! Absolutely. Python is slow compared to Rust for certain, but this was just meant to be an example of WHY you should consider rewriting parts in Rust. The idea was to show potential gains, not necessarily to benchmark the two languages.

    • @DJenriqez
      @DJenriqez 9 місяців тому

      Wtf you are talking about, the tutorial does what video name says,... What real world problems , what oversimplified benchmark ? Benchmark is good, shows simply comparison of those 2 languages. What is Real World problem ?, in real world, like almost a year, I move data from frontend to backend, to database or file storage, read and open and refactor files,....

  • @andreasericsson6989
    @andreasericsson6989 8 місяців тому

    I feel like every language-plugger and C-hater in the world keeps forgetting that their favourite language runs on top of about 5 million lines of well-tested and functioning C code. It's no longer feasible to write any language at all without libbing out to C for most of its basic functionality (like opening files, accessing memory, listening to sockets, connecting to servers etc etc), simply because if you spend 2+ years getting the basics done, your language will get overtaken by something else.

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  8 місяців тому

      That's probably true! But it doesn't mean that everyone has to write C. My argument in this video is that Rust is easier to get started with for Python extensions. That's what I've found from experience, even as someone who's done more C/C++ than Rust in the past.

  • @gustavojoaquin_arch
    @gustavojoaquin_arch 9 місяців тому

    zzzZzz
    Imagine using pythonzzzzz,. instead of pure Rust

    • @IsaacHarrisHolt
      @IsaacHarrisHolt  9 місяців тому

      Unfortunately I'm not genius enough to use pure Rust 😓