text
stringlengths
0
1.62k
[1875.06 --> 1880.56] to write or read at the same time and that's also totally static a compiler figures out whether you're
[1880.56 --> 1886.40] doing that or not and yells at you if you're doing the wrong thing so this probably this probably lends
[1886.40 --> 1892.22] itself pretty well into the concurrency story steve could you talk about that yeah so uh rust actually
[1892.22 --> 1897.36] has a bunch of really interesting and unique concurrency uh things just about it in general so
[1897.36 --> 1902.96] the first one is that um the question that's on everybody's mind with regards to concurrency
[1902.96 --> 1906.92] today is like what's your threading story do you have channels and that kind of thing
[1906.92 --> 1913.46] so what i mentioned is that uh originally well i shouldn't uh i'll do it that way fine it's like
[1913.46 --> 1916.82] you can always pick the way to tell the story right so i'll give you a little bit of history
[1916.82 --> 1923.56] rust used to have both one-to-one and end-to-m threading built in and the problem with that is
[1923.56 --> 1927.94] that the abstraction layer that let you choose like you could basically say in your rust program
[1927.94 --> 1931.86] this rust program will use one-to-one threading or this one will use end-to-m threading and it was an
[1931.86 --> 1936.50] abstraction so you just you would just pick so then that overhead meant that green threads were not
[1936.50 --> 1941.12] actually like significantly more lightweight than regular threads and since rust is a systems
[1941.12 --> 1945.50] programming language you need to have access to systems threads but like end-to-m threading is a
[1945.50 --> 1952.62] runtime kind of issue so we made the decision to switch to just one-to-one threading so by in rust right as
[1952.62 --> 1957.76] of right now by default it's just got one-to-one threading built in now there's a whole bunch of like
[1957.76 --> 1962.34] discussion you get into around that for example like on linux threads spawn a lot faster than you may
[1962.34 --> 1967.00] have expected in the past and so like it's not that end-to-m is inherently superior or inferior
[1967.00 --> 1970.76] to one-to-one but we just got one-to-one right now you can actually write end-to-m threading as a
[1970.76 --> 1975.58] library because rust is a low enough level programming language that io is a library concern not really a
[1975.58 --> 1982.16] language concern so um there's several people including uh one of the people at um tilde who's
[1982.16 --> 1989.18] like writing alternate io libraries that give you other concurrency models etc um but what's important
[1989.18 --> 1994.16] about rust concurrency is is that we have certain types built into the type system that have certain
[1994.16 --> 1999.52] concurrency properties and the standard library uses those to ensure correctness which means that
[1999.52 --> 2004.60] if you write an alternate io library you can also gain the same level of safety with your concurrency
[2004.60 --> 2010.14] that we do built into the language so for example rust has a channel abstraction that's entirely written
[2010.14 --> 2014.48] in library code and you can use channels if you'd like those channels are great but if for some reason
[2014.48 --> 2019.48] you don't like the way that we implemented channels so like our channels are uh multi-producer single
[2019.48 --> 2023.98] consumer channels if you wanted multi-producer multi-consumer channels you would need to write
[2023.98 --> 2028.80] your own but because the channel is a library type and not built into the language you could get the same
[2028.80 --> 2034.68] safety guarantees around them uh that we have which is really really cool and the latest example of
[2034.68 --> 2039.86] something that we did uh is you can actually i've been meaning to write a blog post about this i don't have a
[2039.86 --> 2044.34] good link for more explanation but at some point i'll have something for you you can actually uh do
[2044.34 --> 2050.36] mutable concurrency over stack allocated data and prove that it's safe and not have uh race conditions
[2050.36 --> 2055.52] in it which is well data races um which is super impressive and really hard to explain without code
[2055.52 --> 2060.86] so i'll just like drop that as a thing uh i have like we have like very strong very good safety
[2060.86 --> 2064.66] guarantees around concurrency that are really fantastic i'm sure you've got more to say yeah
[2064.66 --> 2070.70] ultimately the the ownership story is basically exactly what you want for for data right so i typically
[2070.70 --> 2074.32] i mean everyone knows that shared mutable state is the root of all evil when it comes to
[2074.32 --> 2080.24] concurrency and a lot of languages try to solve that by restricting your ability to have
[2080.24 --> 2086.62] shared state or mutable state and ross basically says shared mutable state is indeed bad but shared
[2086.62 --> 2092.44] state is fast and also often very intuitive so what we're going to do is we're going to prevent we're
[2092.44 --> 2096.76] going to use the ownership system to stop you from sharing and mutating at the same time right so
[2096.76 --> 2100.78] the ownership system with the same ownership system that you already learned for doing single
[2100.78 --> 2108.04] threaded programs is also perfect for multi-threaded programs uh so if as long you can uh as steve
[2108.04 --> 2114.14] sort of alluded to you can write a program that uh you know does a fork join model and as long as
[2114.14 --> 2121.12] all 10 of those things that are uh all 10 of the things that are forked that are forking uh only read
[2121.12 --> 2126.28] the data that's totally safe and the rust ownership system knows how to uh think about that um and if you
[2126.28 --> 2131.36] want to uh have a fork join system where you have 10 things that are each mutating something as long
[2131.36 --> 2136.70] as you don't give them the same thing to mutate that's also fine right and so basically what rust
[2136.70 --> 2143.08] said what rust sort of the innovation of rust is that rust has this really uh robust ownership story
[2143.08 --> 2147.54] um ownership and borrowing and ownership and borrowing is pretty awesome for reasoning about
[2147.54 --> 2151.88] things it's awesome for performance it's awesome for letting you allocate things in the right place
[2151.88 --> 2155.84] either the heap or the stack whatever's appropriate but it's also really awesome for letting you do
[2155.84 --> 2159.94] things on a lot of different threads and not have to worry that those that those different threads are
[2159.94 --> 2164.22] going to be stomping all over each other because things have things only own the things that they
[2164.22 --> 2168.56] they should own right and rust already guarantees that you only have a unique owner one unique owner
[2168.56 --> 2173.84] per thing so that's that's basically perfect and the awesome thing is that this ownership system is not
[2173.84 --> 2179.24] um it's not a dynamic thing so like in javascript for example there's also an ownership system
[2179.24 --> 2184.28] and you can pass things to another thread uh and the other thread can do something with it and pass
[2184.28 --> 2188.68] it back um but in javascript every single time you pass something around you have to do all these
[2188.68 --> 2194.28] dynamic checks and that means that there's a lot of a lot of extra overhead to enforcing a pretty good
[2194.28 --> 2200.08] a pretty good rule right um and in rust because of the fact that the ownership system is entirely static
[2200.08 --> 2205.72] the actual cost is no different than doing shared memory concurrency in c or c plus plus but you have
[2205.72 --> 2211.62] guarantees about what can happen because of because of the underlying model so i think the tldr is just
[2211.62 --> 2216.10] when you start learning rust like the ownership system feels pretty daunting but it turns out that
[2216.10 --> 2220.32] it's effectively one concept that you have to learn and then it unlocks all these superpowers that let
[2220.32 --> 2225.82] you write really fast and complicated code safely it's also really just generally like you know
[2225.82 --> 2231.38] everyone's terrified of writing concurrent code because it's very difficult uh and rust makes many
[2231.38 --> 2236.34] concurrency errors be compile time errors which is just mind-blowing the first couple times that you
[2236.34 --> 2242.96] see it for sure well let's take a break here we'll hear from another sponsor and then we get back i'm
[2242.96 --> 2247.66] gonna give you guys a chance to think during the sponsor break uh what's your favorite feature besides
[2247.66 --> 2251.98] ownership and all that it implies we'll have each of you a chance to answer that question when we get
[2251.98 --> 2260.28] over 400 000 developers have deployed digital oceans cloud digital ocean is simple cloud hosting built
[2260.28 --> 2266.02] for developers in 55 seconds you'll have full root access to a cloud server and it just doesn't get
[2266.02 --> 2270.96] any easier than that pricing plans start out affordably at five dollars a month for half a
[2270.96 --> 2278.02] gig of ram 20 gigs of ssd drive space one cpu and one terabyte of transfer all digital ocean servers run
[2278.02 --> 2284.92] on blazing fast ssds with tier one bandwidth and come with private networking use the promo code
[2284.92 --> 2292.52] changelog april to get a 10 hosting credit when you sign up again changelog april 10 bucks when you sign
[2292.52 --> 2300.38] up new accounts only head to digitalocean.com to get started and now back to the show all right we are
[2300.38 --> 2306.00] back steve we've talked about ownership we've talked about how that kind of spreads its way through the
[2306.00 --> 2311.72] whole system and gives you lots of wins um the memory safety stuff the security stuff surely there's
[2311.72 --> 2316.64] other facets to rust what's another feature that is exciting to you yeah there's tons of cool stuff
[2316.64 --> 2321.64] that's the most unique ones that tends to be the one we talk about most often my personal favorite pet
[2321.64 --> 2326.70] feature that is other languages but the rust has a really interesting take on is closures so you
[2326.70 --> 2332.30] alluded to this a little bit earlier but uh rust actually has because the ownership is still involved
[2332.30 --> 2337.96] in closures but like the point is is that because of that system rust closure implementation feels just
[2337.96 --> 2342.72] like ruby's closures so for example like let's just talk about a classic example and see you have a
[2342.72 --> 2347.24] for loop with an array and you want to add one thing to every element of the array right so normally
[2347.24 --> 2352.74] you're doing this like low level like i equals zero i plus plus you know all that kind of shenanigans
[2352.74 --> 2358.20] to deal with this loop managing overhead because you don't want to pay the cost of a full closure and a
[2358.20 --> 2362.98] function call and all that kind of stuff that's indirect um but due to the lvm's optimizations
[2362.98 --> 2367.54] and the way you've implemented closures in rust you wouldn't write a for loop like you wouldn't see
[2367.54 --> 2372.20] you write a for loop like you would well not a for loop but you can write a for loop in ruby but
[2372.20 --> 2376.94] you could also use an iterator as the most important part so the closure is an iterator system ends up
[2376.94 --> 2382.62] giving it a super high level feel but thanks to the implementation details we're actually able to
[2382.62 --> 2387.54] in an optimized build compiled of the same assembly language that you would get out of a for loop
[2387.54 --> 2392.14] um if you were doing the low level stuff so it like it gives you this really high level feel
[2392.14 --> 2397.20] while still giving you low level performance and so yeah to me closures are like a super cool way
[2397.20 --> 2402.92] and the way that they're implemented is is amazing and without it does that without having the problem