Logo TIB AV-Portal Logo TIB AV-Portal

Can Rust make Python shine?

Video in TIB AV-Portal: Can Rust make Python shine?

Formal Metadata

Can Rust make Python shine?
Title of Series
Part Number
Number of Parts
CC Attribution - NonCommercial - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this license.
Release Date
Production Place
Bilbao, Euskadi, Spain

Content Metadata

Subject Area
Dmitry Trofimov - Can Rust make Python shine? Rust is a new programming language from Mozilla. It is fast, safe and beautiful. It is also a very good option when needing performance. In this talk we're going to look at Rust and see what it offers and how we can leverage it as Python developers. And we'll do it with a case study: a statistical profiler for Python.
Keywords EuroPython Conference EP 2015 EuroPython 2015
constantly programming language generate processes Computer animation Lecture/Conference code Development Java machine environment Snake
Meta server Spezielle orthogonale Gruppe addition studies time type inference help divisor browser iked prime number events number product versions profiles pattern data types programming language algorithm multiple Hoare logic Development moment volume applications sign Prime Computer animation case naturally Torque Abstract libraries
inclusion implementation multiple Computer animation Ranges sets gute model implementation number
CIs functional programming randomization breadth log time functions Arm programs theoretical number Prime programme Arrays memory console arrow structure implementation errors Arc tasks conditions Windows Chi-Quadrat-Verteilung area man comparison addition programming language algorithm multiple Elektronischer Datenaustausch files code bits inclusion Pointer events Computer animation versions speech Right game RAS Results
man randomization Pointer Computer animation Java memory implementation errors programs
point man Slides Elektronischer Datenaustausch log files workstation code open subsets programs variance Pointer events Computer animation real vector loss kernel slices console integer structure Windows clone
man functional programming Context Elektronischer Datenaustausch time files code drop counting Arm programs compiler Pointer Computer animation memory box errors compilers Windows
CIs functional programming Beats time internet compilers scratch testing Arc Windows area man comparison Elektronischer Datenaustausch sin real interfaces files code scans residual redundancy Hause Computer animation functions communication interfaces
frame functional programming statistics modules overhead time capture traces programs computational period Types profiles string model systems diamond man overhead real traction applications portable system call frame sample Computer animation different operating functions string genetics tasks Abstract libraries
man statistics multithreaded states singularities capture sample share Ext events several frame Emulation loop events Computer animation objects loop Arc Fingerabdrücke data types
multithreaded states time states Java sample Ext primitives programs Emulation compiler Mutual Exclusion events Computer animation objects different Right iterations testing Fingerabdrücke conditions data types
current frame functional programming Computer animation information time moment lines frame number
current man functional programming table maximal code gute total statistics frame van Symbolic Computer animation objects structure Gamma Gravitationsgesetz
point functional programming Types Pointer Computer animation mapping Void's Commodore Void's expression
man Symbolic Computer animation Blocks lines code structure pub
point man Computer animation different encoding string information systems code lines pub total clone
Computer animation case string interpretations time string life Conversation libraries
modules singularities unit law sources lines Computer animation case string interpretations string Representation Abstract libraries
man Elektronischer Datenaustausch time files code rates lines functions applications total Computer animation profiles naturally life modularity Windows
link Computer animation 3rd
install Types scratch sort extent compilers libraries systems
you OK let's start Our 1st everything that is myself so my name is that each of the more my work for generations and to me and develop
might be but today I want to be speaking about by charming this talk if you want to discuss anything about which chunk of code to bring with opportunity and will be there and you know all the conference there is also the question of the machine uh mostly of a development job and widen the job has been my primary development language for many years and by company eyes more than language that use it's like a subject of constant instigation like a snake that examined which that scarcity has reached the end of what is inside of the country but it's a good
book profiling and I also am curious about new programming languages and to have a year ago statistically with us to beat and I knew nothing about it in that moment steel I don't know much about it now but I hope that I will be able to introduce it to those who are not aware of it at all by the way could call you have heard about this pretty much and how many of you have tried it already develops 0 that's cool but for those who have tried it it will be a whole reveals interesting because I'm sure some corner chronic cases this so what is known about trust yes and answering 1 more question is why why do they roster uh why why
did they started to use my time learning it I found it interesting and also I wanted to do a profile for Python and to make it work for us so OK still a lot of our stresses unless you look projects and and they actually by using the it already for the New OED browser engine called server the 1st started in the southern then as I thought that was you employee look at the example and there were some while was released on May 15 this year so notice is 1 . 1 and before version 1 of of the things we're changing we worry if that were kind based in the rest braking compatibility and and now it's still change standard libraries not polished yet and ecosystem around is just starting to emerge but now it's uh has couldn't do it and this allows to develop production applications and trust so what is the rust what do they know about it when I started to learn it what is it against my and they don't believe it is faster prevents nearly all the faults guarantees that safety close the metal has 0 cost abstraction but mention and type inference and so there's something very cool I felt like we were interested in learning and the 1st established deletions underfunded you became clear for them than a false sense of justification at end of that is already outdated the language that had just changed and then a Congress book the volume was interested I recommend this book this study important it's online in real freedom and I don't think that the torque can help you to teach the language that's like today I want explain the basics of Frost's at all I don't have a goal should language but you're thing in every OK so let's start with a small of what real problems and as this is an advantage rests with the computational problems like competing brands so that the problem is to compute prime numbers between 2 and and and prime number is a number that has no given devices set itself and 1 select 2 3 5 and so on and so will solve all the help of the algorithm called UP conceived the other from a simple work was with this way we 1st take all the numbers from 2 to to n uh and to then iteratively throw away those who have devices so something like that to start date 2 and then with the way or all events and then proceed to the the number of 3 you would take is a prime all the multiples of 3 and then the 5 and a lot of 5 of the runway and so on and so on so here we see
a Python implementation of the fence you know and it's quite beautiful isn't it here we initialize our non-price as
the empty set and then iterate through a range and should be for the current number is not in that said we increment the counter and update all the model multiples you put it the settlement primes and then return it from the OK so
let's run it this is all functions and also we have here main function which takes a command line argument is M and then it accused of function and brings out OK so it's something like OK so we have 4 can and then and for 100 we have something that seems to be correct OK let's the mirror the speech the time of this problem we will comment out them output because it's all always slows down the diffusion and contrary and for 1 million like something like this 1 yeah it's in the pizza more than half a 2nd it's pretty fast but so what is all called the task is what we do care about the what our task is to implement of the algorithm are as efficient as possible so 1 of the obvious solution uh solutions is to use the programming language that everyone knows and sees or a fast and efficient and were really worried programs so we can see for example the by the so let's in the members of the romances so the program now is a bit but it's pretty much the same so we have our defense function and fortunately there is no us said and and the hybrid seed so we use an array of primes and with where all of items marked as 1 of his means that as prime and the when 0 it's not prime and then we iterate and is prime incremental counter then update of the multiples of 0 and then you can tolerate into their results structure and unfortunately there is no of tuples in in the USSR so we need to have these results structure which holds counter and a whole long array so in here we have executed and bring the counter and to then you want to print all the primes when we 1st put them into their another area which is the length of for the counter so iterated games with them and then we bring them so looks quite quite similar so we need to our comparison and we for 10 OK it works correct now run it for 100 and we have a segmentation fault and modern also aware that the error yeah and all that it's difficult to but let's see what did they do it so I managed to to to read the count of so probably the arrow somewhere here that's examples last the so we have all primes arrays and do which is the length of the total primes and then iterate through all our the 1st rate and if the number is prime we increment the counter and put it here so probably you would put things that we can we go out of French here but it's impossible because we start from 0 and we incrementally with the same condition where we implemented here well this is true so we we shouldn't go out of the and here we just bring to the data out of the race what what is the problem and picture that's that kind of kind of problems that that could be called like a beautiful journey in c because you have no idea was going on and I'll tell you what's the problem the problem is not here the problem is here because when we there don't our pricerange we so that we're array but actually what we do in C that is that we return the pointed to this so disappointed tolerates moderated itself and our aim was allocated in the the beginning of the function on on the step and actually it was valid only the scope of the function and when we after we're returning the pointer to this and we go out of the scope this theory it's it's just know more it has to be it expired tend to to me that so by and we still have a pointer tool to eat and that is a problem of very common for C programmers to that's called and then you want pointer that points to know where some random some random memory and that's why we get you would have this addition fold so all task
was to get our rights as fast as possible solution was in in the MUC seasonal solution I know
that there proper programs implemented in C and probably there are people who are convenient C and who will know how to use the recently and probably and that they don't make such errors but Sansón still tells me that sometimes they do and I personally after years of Java and Python just can't imagine how to leave in the world where you can suddenly become uh pointer to a random data in memory so let's carry on to
rust finally let's implement the same 2 1 what will what
do not he's just to re-implement our for over but in the US and we'll see how the uh cross-compiler and the station so here we have
ROC program and here we have quite the same Rust program if you don't understand little centers details it doesn't matter because I just want you to do understand what a basic concept for example what we do here we have the same structure and actually it denotes the same as in C we have counter that is integer and this is the was a pointer the pointer to array of in there and there is no point pointers roster but these denotes like a slice in Russell it doesn't hold the data doesn't hold the data it just points to some data external for that structure so it's actually the same as in the and that here we allocate hold vector 0 initialize it and to integrate and increment the counter we then we do the same as in the and here return of vector as a slide OK so let's come Mr.
think yes and we have compilation errors and press compiler tells
us that primes does not leave long enough so what it tells us that exactly what we have here that payment you cannot and while that I won't allow you because you just want to return the a pointer to the memory that will expire after we leave the context of this function and uh actually this this seems to be restricted much what is better to get this error just In time before is run a program all to divide some mysterious segmentation fault just in the after you deployed your program 2 to users for example I think this is much better but let's let's say around it let's make it work we want
this exact copy because it doesn't make sense instead of that we just 3 implement it from scratch in more than do much trust because a rust has set uh and the test tuples like fight and so we have much shorter solution and it resembles us by them and to 48 let's run it properties and for 1 million something like so yes it's 20 milliseconds so it's like 25 for 30 times faster than Python and the
concept that's that's helped arrest compiler tools to be able to deduce the area that we had scold electrons if you are interested about the beat read residue so include our comparisons python is 25 times slower than rust and to seek doesn't work just much so fast and but that is exactly what they told us in the beginning nothing new and redundant or linked up we can rise to make my questions yes but if you searching the Internet about communication and Ruston Python you quickly find some tutorials about foreign function interface Williams you will given find
examples like these uh these examples are quite clear and simple and their
work is to try so this allows you to call Ross from your bicycle but it's not
enough but what if I wanted to surprise and transform rest what if I encumbered by the string object to rest 3 what if I want to return a with somebody from rust what what if I want to make crust library computable as a model wife and so actually that is what is needed in the real applications now for example the profile by the way who of you have ever use the profile of Python that's cool but for those of you who who still what profile use profile is a program that measures the frequency and duration function calls and so normally less overhead has the better so let's make a Python profile and rice and to see how it goes that you what that was my initial idea idea but when I started with the rest of try to make for example a simple dire need by some there are 2 main genetic profile and stress performance and simplified it's also called of statistics before and statistical profile they periodically and capture frames for program and knowledge has less overhead and recent profile which traces calls in the program let's see how to implement statistical profile last but here we want to go step-by-step incremental the problem because we don't have so much time we'll just focus on 2 important aspects and maybe will learn something along the way and that is that's periodically and friends so how Toronto spirit there is no in no time and trust in the library uh but there is a wonderful library called me on metal ion knew is a lightweight library providing effectively and different operational system abstractions like diamonds and to we
just create a event loop and set up a event in need and then we run a new thread and we pass that Our event handler and what is interesting here is an event handler all hand will
capture frames and save
them to statistics map that is a simpler object that great books and as all-time roads and separate thread that means that our sample is a resource that is shared between different threats so is a shared mutable resources which is believed to be very dangerous as everybody knows that share charitable state of the art of all
but not in last forever and use you as a safe shared mutable state which sounds like a light but it's true what we
do we just and all sample into mutants the mutual exclusion primitives useful for protected protected and shared data when you create a new text you transfer ownership of the data into the new immediately you objects as and then any access to the data through the mutex will walk threats of waiting for the lock became available thus making that that are accessible only through them so the mutants by 1 thread at a time and to pass the reference to have threat right replica with the air he their see provides reference counting through atomic iterations and this is also the and having done all that rust violent guarantees us but we won't have any race conditions never it's just impossible and not having done that well you can't excess that object from different arrest compiler won't allow you to do that it won't allow you even to pass these mutable data to another test so it will be up see single-threaded usually so the the compiler guarantees you that you your program will and to understand this
better I read about ownership so capturing the current
frame but for simplicity and will capture only current execution line as we are not interested in all 3 of the moment there are 3 pieces of information that filename function name and line number that will collect at every detail of our time in Python there's
a function In modern so that is called current frame under the hood use function by throat current frames looking into the supply Fernando we'll find out that the the structure that we need school
that should underscore frame so
we have this wonderful for structure that points to some to some quite cold object that you also need and uh what we need now we we need to convert convert it something how from from CE structure the rest structure available to you and Ross and that could be something
hard because of some she types not rule will be useful to map to arrest there is no no no street making no direct mapping because they had just it's important you do much trust so there are special rust thanks for that to fulfill gap for example see avoidance and of void and asterisk Newton's a special type of that reflects the C pointers and normally the reason now and roster told but to check this point so we have special method is now so knowing that we've right important remember that when 1 if anything which which is important so when you're born in the function or using appointed arrest can guarantee safety anymore all
such expressions should be with and say and I think that
that is what they mean when they say that the US prevents nearly all 6 false because that is the the way that the work nearly it means that it doesn't falls and on the book when you work supported and so knowing this
known how to use in the block so we just create all structures and trust and it
seems that we are very close to there is evidence of what we need but how to convert by
considering the terrestrial funny but that was nearly the hardest problem I faced because that attributes it was difficult and at some
point came up with something like this so this is just to convert this by the strength of the US think last line and it indeed works sometimes he didn't handle some differences between bites thing single-sink scenario was already at the latest alludes started to to implement that but then I came across a
library called trusted by from by the new 1 in my life became much more easier that is what a beautiful libraries and I had recommended actually it appeared that that the most things that they need to communicate across and by the time I was there I only needs to to some
details from my specific case and also it's very good example of a for example a string conversion using this library world looks like that and also all on each handles all the
cases this unit called string representation and also provides very very important abstractions like special law of corresponding to the global and by
but yes and this is this is how you can expose your native for library for the Python modern or using the uh using the rust Microsoft it's amazing thing varied sources it's very freedom so soon just this line there are a lot of more on under the hood and they are the interest so
and and that's it was much followed by um and much where details but the end of the now
as we have a couple things let's see how our profound works the given name so
conditioned on so a profile and this is the sense of why that we have freedom and the 1st time how did this not interested and need to well I think so that it will happen at some it's impossible to to make life a demo without fail and yet so that we can mentality of bringing 5 the nature yeah let's start with millions OK and she was passed let's make it OK it it's very simple now it's very basic but what what it tells us that the 85 per cent of all time when and in the like a and 14 per cent and the light 7 actually nearly 1 % was for output of this line so line 8 this 1 so what what what we are doing here is we update and also in array and you know what we're doing here 15 14 per cent when implementing has the it looks the believable so it looks for where logical and yes and you still care about performance nearby for applications but you don't want to do the deep into the Native Court I recommend you
to listen to the top of my colleague between those of that will take place on the 20 3rd of july and those if you will
show you how to write performance by the court without using any see of course to thank you for your attention questions what
is for why is it possible to distribute the last called so in by them values which can be installed his people I came across a little library that allows you easily to for radio system that find the slave that it will compile neurons court if you have frost compiled and stuff maybe even dongles it I don't know I never tried it I wanted to try but had no time before and you just types of some sort of 5 the little installed and it builds your roster of from scratch like the suspect by allows you to install C extensions so various library also for us all it this is the only 1 1 the biggest thing