https://pantsbuild.org/ logo
h

hundreds-father-404

02/03/2021, 9:53 PM
Hi friends! I'm working on a Pycon proposal re: Rust + Python, how it enables us to have performance with accessibility. Will explain 1) how we use it at a high-level, 2) why Rust, and 3) takeaways/insights for other projects It is not a tutorial, I don't want to do a deep dive into the CPython crate vs. PyOxidizer for example Thoughts on take-aways we have with using Python + Rust? Main one is Rust + Python is awesome, but also maybe 1. Be hesitant to add in Rust. Benchmark. 2. Minimize the FFI boundary. 3. Integration tests are crucial. Proposal at https://docs.google.com/document/d/1D0MBEo5Dw97qN91TqoCHwtZf4R8gEFU3RDm46tePRvQ/edit#. Feedback very much appreciated!
Another way of framing #2...have a strong "separation of concerns", know what Python excels at and what Rust excels at My point is that the FFI boundary is awkward, and it's likely better to do full Python or full Rust if you don't have a nice separation. Pants works because we have this well scoped scheduler that's abstracted away
3 might be boring / kinda obvious? And I'm not really sure that's the right takeaway? I more think it could be insightful to talk about how we test things in pantsbuild/pants, but I'm not sure what the actual takeaway is
cc @average-vr-56795 @red-balloon-89377 @early-needle-54791, really curious what y'all think given your wisdom from the earlier days of the engine
a

average-vr-56795

02/03/2021, 9:57 PM
I'll give it some thought 🙂 But my initial reaction to your 3 is: A few integration tests are crucial, but a really well-defined/separated API with lots of unit tests is going to make your life so much easier (which hopefully isn't news to anyone!)
👍 2
h

hundreds-father-404

02/03/2021, 9:58 PM
I agree. Maybe for #3, what I really am after is "You really need to follow testing best practices"? I don't think we have anything new to say about testing, other than that if you have FFI, it's even more important to do what everyone is telling you to do
(FYI this talk is targeted for intermediate.)
a

average-vr-56795

02/03/2021, 9:59 PM
I feel like a large part of that is really pointing at a lack of type analysis across the boundary?
💯 2
🔥 1
h

hundreds-breakfast-49010

02/03/2021, 10:03 PM
from the perspective of Rust, the type of every python object is
PyObject
(ignoring the places we're still using
Value
)
👍 1
I wonder if there are intersting things to say about the rust build problem of creating the
libengine.so
file and getting python to understand it correctly
this is a corner of the codebase I don't understand as well as some other people; and I remember when we were running into issues with this lib being big enough to cause problems with test sandboxing
h

hundreds-father-404

02/03/2021, 10:08 PM
I wonder if there are intersting things to say about the rust build problem of creating the libengine.so file and getting python to understand it correctly
I don't think I'll want to spend a ton of time on this part because using an
.so
file is pretty common via Cython I think in the "How we use it" part, I'd include like 30 seconds explaining we compile an .so executable via Rust, then load it simply by ensuring the resource file is in the right place and importing like normal
a

average-vr-56795

02/03/2021, 10:09 PM
The switch between "avoid linking libpython" and "ok we'll link libpython" is probably worth mentioning (I think Stu is your expert there?)
👍 1
w

witty-crayon-22786

02/03/2021, 10:30 PM
and then also the switch from the “reduced” PEP-384 API to the full API, which required targeting particular versions
https://pantsbuild.slack.com/archives/C0D7TNJHL/p1612389789060100?thread_ts=1612389223.057800&cid=C0D7TNJHL this isn’t actually true… you can include types in the the FFI method signatures, and
cpython
handles the conversion.
via the
FromPythonObject
and
ToPythonObject
traits… which we could implement for more things
`PyObject ~= Value`… but you don’t need to use
PyObject
in your method signatures.
BUT, mypy doesn’t know about those. which is why we also have https://github.com/pantsbuild/pants/blob/95ed385f71feec341f02ef5476bf95ade1a0b4dc/src/python/pants/engine/internals/native_engine.pyi, unfortunately. maybe there is a better way there.
👍 1
…could totally generate that file too.
💯 1
👀 1
one minor correction: the relevant “x vs y” here is
cpython
crate vs
py03
crate… we used
cpython
at the time because
py03
required nightly rust. you don’t need to go any deeper than that though… afaik,
py03
started as a fork of
cpython
, and they’re relatively similar in design still.
b

busy-vase-39202

02/03/2021, 11:13 PM
Hi to folks I don't know yet! I'm the head of developer relations over at Toolchain, where I get to work with Eric, Greg, Stu, Benjy, John, Asher, etc. I'm new to Pants, so the below feedback is general:
Re "unoriginal" points: Key is to decide whether a point is unoriginal in this unusual context + unoriginal to this particular audience. The audience will be Python intermediates, but presumably have little or no Rust background. So a point that seems unoriginal from a Pants 2 contributor's POV may be very valuable for this audience to hear —for possibly the first time, or as reminder/context within an uncommon situation for an intermediate Pythonist. At the other end of the spectrum, things that are axiomatic within Python community can be powerful for calling out how a point of yours reflects community values and community best practices. So, sometimes an "unoriginal" point is worth giving specialness too. Re tests: As a listener, I also appreciate hearing not just that I need good tests but what kind of surprises might be around that corner. Are there things that I wouldn't usually think to test for in Python that need more probing when writing tests for Rust? Are there any things I get to take for granted, e.g. from stdlib, that free me from things that I'd expect to test in Python? Re overall: You've gotten to dig into deep and fascinating corners of both languages. Are there corners of Python or Rust that it's uncommon to explore as much as happened on developing Pants 2? These are fascinating to hear about, and bonus great if hearing about them now can help someone in that audience avoid pain. If you see any interesting opportunities for Pythonistas to piggyback on the code that Pants community has written here, where a 3rd party would be able to a leg up for their own thing unrelated to Pants, these are great to hear about those. Maybe it unlocks someone else's imagination...
👍 2
w

witty-crayon-22786

02/03/2021, 11:21 PM
yea, very good points. and you have to expect that ~90% of your audience will never have used Rust, so worth touching on the “can’t get it wrong” stuff that Rust brings for all code, not just FFI. maybe also worth contrasting to how you could get it wrong in C (“forgot to release the GIL”, “segfaulted because deleted object”, etc)
💯 2
a

average-vr-56795

02/03/2021, 11:46 PM
One thing I'd possibly think about bringing up (or not, it's maybe kind of niche, but also, kind of Pantsy) is the dependency story... Rust has a really nice single package manager, and dependencies are generally really easy to pick up, and that's designed into the language - the difference between 0, 1, or 10 dependencies is trivial. Python has a somewhat jankier dependency story. Publishing is weird, there are wheels and eggs, native deps aren't always clear or easy to work with, and the difference between 0 and 1 dependency is huge. (Pants tries to make the Python world feel a bit more like the Rust world here, yay Pants, good opportunity for a plug)
h

hundreds-father-404

02/04/2021, 7:02 AM
First draft! Thanks for the feedback everyone 🙂 https://docs.google.com/document/d/1D0MBEo5Dw97qN91TqoCHwtZf4R8gEFU3RDm46tePRvQ/edit# Feedback welcomed
f

flat-zoo-31952

02/04/2021, 7:21 PM
I guess this is a bit late to be suggesting a new topic altogether...but is there any attempt to address a question like "why use rust to extend python rather than C/C++?". Do you have any experience developing python extensions with those things?
👍 1
❤️ 1
w

witty-crayon-22786

02/04/2021, 7:22 PM
i had just added a comment to that effect, heh. coke
it’s an important point.
Rust is as fast as C or C++, but much safer. it might have a higher learning curve, but in terms of new contributors actually successfully landing new code, the hypothesis is that it is easier.
1
🎯 1
@hundreds-father-404: and that reminds me: here is the original motivating document. https://docs.google.com/document/d/1C64MreDeVoZAl3HrqtWUVE-qnj3MyWW0NQ52xejjaWw/edit
f

flat-zoo-31952

02/04/2021, 7:25 PM
i mean this is specific nit of mine too, because I have done a bit of C++ extension dev and I hate it
w

witty-crayon-22786

02/04/2021, 7:28 PM
yea, i don’t know what the state of art for C++ extension development is, but the
cpython
crate is really, really solid.
f

flat-zoo-31952

02/04/2021, 7:29 PM
the big argument on the science/research side of python for C/C++ is the larger community around it, and the established practice providing C++ SDKs for hardware, be it GPUs for number crunching, or just drivers for things you get from vendors
w

witty-crayon-22786

02/04/2021, 7:29 PM
yep, good point.
f

flat-zoo-31952

02/04/2021, 7:29 PM
i haven't used rust enough to know its story for interacting with those things
w

witty-crayon-22786

02/04/2021, 7:31 PM
it has a ways to go before you can do it natively, admittedly. you’d have to generate rust bindings for the C/C++ library in some cases, and then use rust to interact with python.
f

flat-zoo-31952

02/04/2021, 7:31 PM
i get that you're supposed to be able to write wrappers at the FFI boundaries
w

witty-crayon-22786

02/04/2021, 7:32 PM
but as an anecdote: Pants’ rust code embeds a few different C libraries, and has embedded C++ code in the past. and the rust packaging ecosystem mostly hides that from you successfully.
🤔 1
f

flat-zoo-31952

02/04/2021, 7:32 PM
well a lot of C++ libs have such bad APIs that the work necessary to build an idiomatic rust wrapper might actually make it easier to work with than working with it natively in C++
💯 1
idk, just anything you can do to get people to stop using C++ haha
👍 2
💯 2
i don't think the hardcore C++ people are reachable, but i think teams that might be considering doing extension work for the first time might be an easier sell
h

hundreds-father-404

02/04/2021, 8:10 PM
"why use rust to extend python rather than C/C++?"
cokeCarina and I were talking about this at the exact time you sent that suggestion, @flat-zoo-31952! We realized that is really what I wanted to make this talk about, and it was getting a bit muddied into making Pants the subject of the talk, rather than only something that guides the talk I'll be rewriting this before the deadline next week
b

busy-vase-39202

02/04/2021, 8:59 PM
h

hundreds-breakfast-49010

02/04/2021, 9:04 PM
I don't think Rust even has a higher learning curve than C++
👍 1
🙏🏻 1
C++ has been around longer, so there's a larger pool of people who are familiar with it
and in general I think the software industry should be trying to move away from C/C++
b

busy-vase-39202

02/04/2021, 9:05 PM
Stu answers the big question of "Why would I, a Python dev, want to hear -- let alone pursue -- this?" It also shifts away from subtext that we need Rust because Python is lacking. The core topic is that Rust can brings the wins for Python that most people look to C/C++ for. You're presenting an alternative, as not only legit but with less friction and more benefit.
👍 1