Skip to content
PodSubs
ShowsPeopleGuestsJobsNewsEvents
More
BlogPodCred
Menu
ShowsPeopleGuestsJobsNewsEventsBlogPodCredNetworksSign in
Join nowSign in
  • Shows
  • People
  • Jobs
  • News
  • More
    • Guests
    • Events
    • Blog
    • PodCred
    • Networks
    Join nowSign in

ADSP: Algorithms + Data Structures = Programs

ADSP: Algorithms + Data Structures = Programs artwork

Episode 182: C++ Variadic Templates, Swift and More with Doug Gregor

May 17, 202438 minAudio

In this episode, Conor and Bryce chat with Doug Gregor from Apple about C++11 Variadic Templates, C++11 std::tuple, C++17 std::variant, Swift and more! Link to Episode 182 on Website Discuss this episode, leave a comment, or ask a question (on GitHub) Twitter ADSP: The Podcast Conor Hoekstra Bryce Adelstein Lelbach About the Guest: Douglas Gregor is is a Distinguished Engineer at Apple working on the Swift programming language, compiler, and related libraries and tools. He is code owner emeritus of the Clang compiler (part of the LLVM project), a former member of the ISO C++ committee, and a co-author on the second edition of C++ Templates: The Complete Guide. He holds a Ph.D. in computer science from Rensselaer Polytechnic Institute. Show Notes Date Recorded: 2024-04-29 Date Released: 2024-05-17 C++11 Variadic Templates / Parameter Packs / Expansion C++26 Pack Indexing C++11 std::tuple C++17 std::variant C++11 Digit Separators Swift Programming Language HPX (High Performance ParalleX) Intro Song Info Miss You by Sarah Jansen https://soundcloud.com/sarahjansenmusic Creative Commons - Attribution 3.0 Unported - CC BY 3.0 Free Download / Stream: http://bit.ly/l-miss-you Music promoted by Audio Library https://youtu.be/iYYxnasvfx8

Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.

Transcript

From the show. Tap a time to play from there.

BryceSo when when Chris started this off, um what was his original vision and intent? Was it to create a general purpose systems programming language? I I see a I see a nod.

Doug GregorI am nodding, yes. The overall goal was general purpose systems programming language.

BryceWould you say that's what Swift has become?

Doug GregorYes. So I think that is what Swift has become, but that's not necessarily what the perception of Swift is.

ConorWelcome to ADSP the podcast, episode 182, recorded on April 29th, 2024. My name is Connor, and today with my co-host Bryce, we continue our interview with Doug Greger, and in this episode, chat about C Variadic templates, std tuple, stdvariant, the Swift programming language, and more.

BryceBut uh actually, this is perhaps a good a good transition point because I don't want to talk solely with Doug about concepts. I actually want to bring up something that uh that you said to me um uh last year at PLDI, and I'm not sure if you'll remember this, but we were talking about um uh a feature that uh you worked on that did end up going into C11, which was Variadic Templates. And I believe you said something uh to uh something akin to uh variadic templates was like a perfect feature.

BryceThere were no no defects, like it's just a perfect pure thing, uh and I'm very happy with it. And I um felt a little bit differently uh because while variadic while variadic templates are a wonderful feature, um, and anybody who uh uh does not feel that way should look at how tuple was implemented pre-variadic templates. You can go find it in the old boost libraries or uh the thrust tuple, a thing that I have had to deal with for many more years than I would like.

BryceUm but uh my my objection, my issue with variadic templates is like this head-tail pattern that you can't like index into them. Although, interestingly, last committee meeting we voted in parameter pack indexing. Um but uh explain yourself, sir, why why why are variadic templates such a great feature?

Doug GregorI should have known it wasn't safe to talk to you about variadic templates. So the reason I was so happy with Variadic templates, I still like the feature, is it's a pretty tiny extension. And for a tiny extension, it enables a whole lot of really interesting abstractions. Right? So, you know, at its core, you have one I I guess you have two new things. Like it only introduces two things into the the C language.

ConorWhat did you just do? You you put the peace sign up for the listener. You know, Doug Doug was uh had a peace sign for two. Put his two fingers up, and then a bunch of balloons appeared, you know, floating up around his head. I'm not sure if there's some birthday sign or something like that. Maybe they thought you would.

BryceYou know what? It's not gonna happen on our systems, uh Connor, because we are both connecting from non-Apple systems, and I'm pretty sure that this is an Apple thing. Yeah.

ConorLook at that. That's amazing that it's an Apple feature in a Microsoft product.

BryceWell, it it's my understanding of how it works is that it's um it's like a pat like it's an injection. So so the it's something that Apple layers on top of the camera feed and then feeds through to the apps. So it's transparent. Um it's pretty impressive.

Doug GregorBut the net effect is that we got a party with balloons while we're talking about variatic templates. So I'm pretty happy. This is true. This is true. Okay. The the two things that Variatic templates added are parameter packs, you know, introduced by the ellipsis that abstract over any number of parameters. Same idea whether it's you know template arguments or function parameters, and pack expansion, which lets you take a pack and expand it out into multiple arguments. As a feature, it's really tiny.

Doug GregorAnd it wasn't terribly hard to implement, so the initial implementation took about a week or two in GCC, uh, including porting over uh uh tuple, function, and bind libraries, and getting them completely working. And so it's it's fairly easy to teach, it's a fairly small extension, and yet it let us get rid of this horrible limitation on the number of template parameters you could have in a way that I was fairly proud of. Pack indexing is nice, I agree.

BryceUh this one so why why didn't we get pack indexing in the first version? Why was it why was it um uh like this like head tail sort of pattern to unpack them?

Doug GregorSo we didn't have pack indexing in the first version, mostly because we couldn't come up with a syntax. Which sounds really silly, but you know, we tried several different things, and you couldn't really subscript, that wouldn't work. And I think we tried subscript with the leading dot, like x dot subscript, you know, open square bracket something, and that was hitting problems with the preprocessor. And essentially, we sort of got tired of looking for a syntax for a feature that wasn't strictly necessary, right? Because you can implement this directly.

Doug GregorWhy couldn't you subscript? Well, because x subscript with with an i, that is completely reasonable if your your pack x had a bunch of values in it that were all like vectors or something or arrays.

BryceRight, right. The syntax that we settled on that we have in C26 is um dot dot dot uh subscript.

Doug GregorOkay, sure. Yep.

BryceThat's that will work.

Doug GregorUh that that may have uh provoked antibodies had we tried to add it to the proposal at that point uh in in the C OX timeframe.

BryceTo be fair, it it would have been almost impossible to have foreseen at that time how widely used parameter packs would have been and how um uh uh how important the uh indexing would end up being. Um I think the same can be said of constexpr. When uh uh you know constexpr is like the C case study in incremental evolution where we shipped a very minimal restricted form of constexpr in C11, and it's slowly grown over the past decade with every release us allowing more things in constexpr, making more things constexpr, to point out that you can have constexpr vectors and strings.

BryceUm and that constexpr today is a very different beast than it was back then, and it's used, you know, it's a whole new form of metaprogramming that has arisen around constexpr, um replacing type-based metaprogramming in ways that I don't think the people who originally worked on Constexpr could have ever really imagined. And as you say, Virietic uh uh templates, a you know, very important and signature feature of C11, at the time it may not have seemed like that because it was this very easy and simple thing to do.

BryceUm you said it was pretty straightforward to put into the language, it was pretty straightforward to implement. Um, and the uh the full impact of it may not have been known or realized at the time.

Doug GregorNo, and our goals when we did it were very much around these libraries. So stdd function was was mine. I was working with Yako Yarve, who did stdtuple, and we were both really frustrated by this dumb limit that we had. You could only have 10 or 20 or whatever the implementation-defined limit was, values that worked. And so we wanted this feature to make our libraries better. And that that was kind of the scope we imagined that you know only library designers doing this kind of thing would end up using variadic templates.

Doug GregorSo keeping it sort of smaller was a benefit.

BryceSo sopple actually is an interesting one because I I I, for a long time, have been a proponent of the um uh uh exposed things as uh as uh struct and class um abstractions. Um the the the way the way I like to explain this to people is like C arrays and C are weird because they don't really behave like other things. There's a whole bunch of caveats when you have an array object, you can't return them from functions. Um they have all these like when you pass them in as parameters to functions, there's all these pointer decay things that happen.

BryceUm and like std array is better because it's a normal thing. It is a, you know, it's a struct. So therefore it follows all the basic rules of what a struct is, and that's a good thing. We like things that sort of like behave like the other things, and that's one of the reasons why in sort of modern C design, we like to expose things as uh even things that are lot language features, we like to expose them through the library. Even things like you know, reflection, like the metadata type, is exposed as a library type.

BryceUm uh and things like you know, tuple and uh uh variant get exposed as library types, because then they behave like all other uh class instruct types. Um with that said, um uh just so many hours and years of people's time have been spent on you know making tuple work or making variant work. In tuple, aside from std.git being a little bit ugly, tuple's not so bad.

BryceUm variant, I think, is less pleasant to work with. Though the real place where tuple's painful is more on the implementation side of things. And I just wonder, would we have been better off with the language uh tuple? Was that something that was considered at the time?

Doug GregorI don't remember it being seriously considered at the time. Now, Tuple has a pretty long history, so it was a boost library. Yeah. And, you know, it and function were the first two papers that were voted into the C technical report on library extensions, which is like the first thing the committee did after C 98.

BryceYeah, the TR.

Doug GregorThe TR eventually became into COX. And I there wasn't a whole lot of appetite for language extensions at that time. So I don't know that people would have really considered it as a language feature. And part of the thing is what you said there's, you know, tuple as a library works pretty well. If we get a nice pattern matching facility, then it'll work pretty well, probably quite quite well in in C. Uh Variant is a completely different story, though.

Doug GregorI do not uh love using variant. I tend to hand code my own uh discriminated union types. This is one of those things where uh in Swift we got it right. And when I get to write the Swift form of you know what I think variant should have been, it is wonderful. And then I have to port it to C for some reason, and it's you know, to fit in the C compiler, whatever I have to express that idea, it is really painful. Because discriminated unions is actually a fundamental data type.

Doug GregorLike it should be a fundamental data type. We have structs, right? What do structs do? They let you put a bunch of things together. What does a discriminated union do? It lets you express a choice between a couple different things. And variant makes that choice really painful. You have you have to basically embed each of your choices in another type that you create just for its name. Like there's no way to name these things. Every operation on a variant is either this like unsafe thing where you want to go and like check, do you have this and then use it, and then you have big ifs, or you have to write a giant like type switch visitor thing.

Doug GregorIt it can be so much nicer.

BryceYeah. Yep, I I completely agree. I think at the very least we should have done variant as a language thing. But if we were going to do variant as a language thing, uh it probably didn't make sense to have a language variant, not a language tuple. So maybe we should have just done both as language things in C17. So, so before I I do want to chat with you about Swift, um, but uh before this one last C topic, which if I and this is like bar stories that I was told many years ago, and so I'm not sure if I'm gonna remember the details or if this is something that you're gonna want to talk about.

BryceOh no. Do you recall um playing a role in the the history of digit separators in C?

Doug GregorDigit separators in C. There was digit separators in C. I literally cannot remember. I remember a lot of discussions over what it could be, and there was a serious problem because of user-defined literals. So you couldn't have nice things, you couldn't have the obvious underscore syntax.

BryceSo what I what I what I've been told. So the way the committee used to work was there used to be a plenary on Friday, and that was the fun plenary, where we would uh and the the the way that the closing plenaries worked has changed like three or four times. So I'm just gonna describe one of them, although it's actually sort of describes uh uh there were three different evolutions of this way of doing plenary, but I'll just describe all of them. Um it used to be that we had two plenaries, one on Friday, and that was the fun one where discussion was allowed, and then one on Saturday morning where we weren't supposed to have any discussion.

BryceIt was supposed to be just procedural and we'd vote, and uh there would only really be discussion if new information had been uncovered um uh overnight. Um and uh and these days we don't even we just have one plenary, we don't even like have technical discussion during the plenary because we're supposed to like front load all of it. But we used to do it this way, and those Friday plenaries could go long. And the story that I had heard was that like it was very rare that that something the information changed overnight.

BryceBut what I seem to recall is that in one case involving digit separators on Friday there was the plenary, and they we were gonna vote out digit separators, and um and then everybody went off to the bar, and then some group of people found some new issue, and then some some folks went around and told everybody about this issue, and then between the close of plenary and Saturday morning, the room had changed and the vote on digit separators failed.

BryceAnd some stakeholders had decided to fly out on Saturday morning because they were so confident that everything was good, and then there were some surprises. Do you recall any of this?

Doug GregorThis sounds vaguely familiar. I mean, an interesting problem with this approach, of course, is that you only learn in the Friday plenary what some of the other working groups have been up to.

BryceRight. Right. We've gotten better about that.

Doug GregorIf you're a compiler implementer, it may very well be that you were in another room when some of these things were decided and might be a problem. So I I have a vague recollection of this happening. I do not even remember why digit separators were so problematic.

BryceOh, I I I I will find the the um the meeting minutes and uh if I can and send them around in the show notes. But uh but yeah, I just I had a vague recollection that you were uh you were one of the people involved. I won't say the culprit, because that would be that would be unfair. That sounds deeply unfair. I will say that I won't say the culprit. So the implication exists. I I think we should talk about uh Swift. Um because you were you were there at the beginning, um and uh and Chris won't come on our podcast.

BryceSo how else are we gonna learn about how Swift started?

Doug GregorI see. So I'm your second choice.

ConorOkay, I'll remember that. Thanks, Bryce. Before we ask any questions, I'm curious, Bryce. Uh we're gonna we're gonna test Bryce's knowledge. Just, you know, simple question. What version do you think Swift the latest Swift is on?

BryceJust uh Oh, I'm gonna guess the last time I knew what Swift version was was around 14 or 15 because I asked Chris uh something about when an async feature was gonna go in, and he said that's not planned until like 15 or something. And that must have been uh five or six years ago, because I would have been working at NVIDIA. So let's assume it's five years, so let's assume that Swift revs like twice a year at least.

BryceUm so it's gotta be at least in the 20s, um, but maybe the early 30s. So I'm gonna say 24 or 25.

ConorWow. The purpose of this question uh uh is everything I hoped it would be, folks. Uh Doug, would you like to tell Bryce what version Swift is currently on?

Doug GregorSo the most recently shipped Swift version is Swift 5.10.

BryceOh, okay. Well then what do I know?

Doug GregorThe big push right now is for Swift 6, uh which is actually a big deal. So we treat the the major version as the only time that we can sort of make a real change that is source incompatible with the prior versions. So we did it much more often in the in the early days as Swift was still growing and and learning its space. Swift 5 has been Swift 5 since uh early 2019.

Doug GregorSo actually basically half of its life has been uh Swift 5. That's where that's the point at which we declared ABI stability.

BryceSo so then I must I must have been thinking of uh Chris must have told me four and five, and four and five dot something, and I must have in my head mixed that up to 14 and 15. Okay.

Doug GregorThat's my theory. It could have been Swift 4 would have been around 2017, I guess. I may be off by one.

BryceBut but all joking aside, I'm actually very excited to have Doug here on the podcast because I I do not get starstruck by many people. I I think I am well known in the community for this. I have famously on this podcast said that I don't experience imposter syndrome. I feel very at home and very confident. But there's like a very limited number of people that I was exposed to early in my career who I like looked up to and sort of get that little starstruckness with Doug's definitely one of them.

BryceUm I would probably say Dave is another one. Sean Parents actually not just because I spent so much time at bars in C now with Sean, it's like he's just become a friend. Um and yeah, I but but Doug's one of those people who is like, we have Doug Greger on the podcast. Wow. So you are in no way a uh uh a second choice. Um but uh Chris, if you want to come on the podcast, you're you're always walking, buddy.

BryceSo okay, so you're at Apple, you're you've been tasked with uh working on Clang. And then at some point Swift happens. Like what's what happens in between you working on Clang and Swift?

Doug GregorSure. So uh we worked on Clang. We made super fast progress early on. So I joined in late 2008. By 2010, we had a C compiler that could compile Boost. Um so things moved really quick. And uh we were rolling it out, it was became the preferred compiler on um like Apple platforms by 2010, 2011, and Sunset GCC.

Doug GregorUm this time, like why? Why why build a new compiler? We needed a compiler that was more flexible. GCC was kind of hard to work with. I mean, I I implemented a lot in GCC. So I did variac templates, I did concept GCC, I think I also did R-value references and so on, as well as some types of some surgery. It was just a hard compiler to work with. It's it was an old C code base. I think they've moved to C since then. But we felt that starting from scratch, knowing C as of like the C11 standard was you know mostly done at that point.

Doug GregorWe figured we could do a much better implementation that you know leveraged LVM the back end. And leverage the philosophy of the library-based design that LLVM had, and build a compiler that was just both better for users, but also more extensible to build better tooling on, make it easier to implement new language features, and so on. And so it went very well. We did that. We started adding some features.

Doug GregorI mean, this is at the point where if someone came up with a feature in the C committee, myself or later uh Richard Smith, could go ahead and try to hack it up in Clang. It's very possible that your digit separator story was this exactly, that we went and tried to hack it up in Clang and found some ambiguity or something, even though I don't remember it. And so, but as this was happening, uh we were also adding more support to Clang for other things, like um for Objective C, which is the other language Apple supports in the C family.

Doug GregorWe added automatic reference counting to try to improve the memory safety of the language. But uh we really realized that C and just the C family of languages had some fundamental limitations that we could never fix. Some of those are around memory safety. I don't necessarily want to go down that rabbit hole, but you know, our view was that these are unsolvable problems in in the language as we have it, or that solving them would change the language so much that it wouldn't still be the same.

Doug GregorAnd the other problem is that you can't really remove anything from C. And so you're stuck with a lot of decisions that we would not have made early on. And so um we decided to it was time to go and build a new modern native language based on all we had learned about you know language design and compiler design. And so Chris Latner totally started it in secret, and then after a bit of hacking, uh he looped in several of us to go and join him.

Doug GregorSo I'm committer number two to the repository for Swift and have been working on it ever since, including you know, the generic system uh was initially my design. I've also worked on the concurrency system, the macro system, and most other aspects of the language uh over the years.

BryceSo when when Chris started this off, um what was his original vision and intent? Was it to create a general purpose systems programming language? I I see a I see a nod.

Doug GregorI am nodding, yes. The overall goal was general purpose systems programming language.

BryceWould you say that's what Swift has become?

Doug GregorYes. So I think that is what Swift has become, but that's not necessarily what the perception of Swift is.

BryceYes, I would I would I would tend to agree. I think people tend to think of Swift as that language that you write your Apple, your iOS apps in, right? Yes. Um But so okay, so maybe you can you can tell us a little bit more about Swift and uh Swift's design and specifically what makes it a general purpose language and not just that app language.

Doug GregorSure. Uh so I mean, what does systems programming language actually mean? Is is right, because you can define it a lot of different ways, and so it's tough. So the things that we look at are um as a language, it needs to have abstractions so that you can build good libraries, because any system of any size is architected as a set of libraries. Uh it needs to have a compilation model that um admits efficient code. So, in our view, this meant it had to compile to native code.

Doug GregorWe we couldn't accept requiring a JIT behind the scenes. And the design has to be such that you can scale your program. So I've had a hard time articulating exactly what this means. But, you know, if your language depends on having to see every bit of source code everywhere and compiling it all together, you aren't going to be able to scale up to a larger system. You have to be able to ship separate software components with stable interfaces that can evolve over time.

Doug GregorAnd so our view is those are the main ingredients. And the the last piece that we cared about a lot was is related to memory safety, but it's not memory safety. So it was really, we wanted to make it such that it was easy to get to correct code. So the compiler and its type system should support you in getting your code right. Things like undefined behavior shouldn't happen.

Doug GregorAnd the type system should encourage good patterns that help you write correct code and deal with erroneous cases and so on, in a way that you know it's obvious when you look at the code what it's doing and that that's what it actually is doing under the hood. And so that that's kind of the overall dream of Swift. Um and you can see this play out in uh how the language is actually designed. So uh things you're probably familiar with, for example, in Swift, we push value semantics everywhere.

Doug GregorWe love value semantics. And value semantics have this wonderful property that if you have a value and you make a copy of it, the original value and its copy are completely independent. Nothing you can do to one will change the other. This is so good for local reasoning, because you can just reason about the code that's on your screen. You don't have to think about, well, someone else might have an alias to my data structure over here that's gonna create some spooky action at a distance and affect my code. And when you have that basis of value semantics, which like runs throughout Swift, then you can make immutability that really works and say, okay, well, most of the time you're not changing things.

Doug GregorSo let's make it easy to do immutability in the language. Right, and make that sort of the default view that things should be immutable, unless you really, really want to make it mutable, and then those mutations will be local.

BryceWhen did uh when did automatic reference counting uh become the uh the memory management story of choice for Swift? Or is that just does that just come from the Objective-C background, or was it a principled uh choice?

Doug GregorAaron Ross Powell No, it was actually principled choice. Um so it was certainly true that uh Objective-C had a convention-based manual reference counting scheme that we then codified in the compile in Clang to make it automatic. Uh, when we started Swift, we looked at the options. I mean, the main options there were uh you know traditional garbage collection or something like you know automatic reference counting. And they're interesting and have different trade-offs.

Doug GregorI think the easy answer probably would have been, uh, let's do a traditional garbage collector. But there's a couple things that worked against that. Uh one, we'd had sort of a poor experience with trying to do garbage collection for Objective C. There was an attempt, it didn't really work out very well. But there's a reason for that, which is when you're interoperating with a C stack, like you have some of your software written in C and some of it written in another language. You can't trust C to behave the way you want it to, from the perspective of garbage collector, right?

Doug GregorAnd you see how hard this is in any language that has garbage collection and also has a native interface to C. And with Swift, we knew that one of our key features had to be really good interoperability with C so that we could start like incrementally moving the world over. Having a garbage collector would have made that really hard. That's one of the points. The other point that I think is really uh important is that the great thing about reference counting is you can locally optimize it away when you have more information.

Doug GregorIt becomes a compiler optimization problem. Like you set up your calling conventions so that you know where the reference counts are supposed to be maintained. But if you have some performance critical code where you absolutely cannot handle the cost of reference counting, you can force the optimizer to look harder, add more annotations to the language, whatever it takes to eliminate the reference counting just in that place without affecting anything else in the system. So it has this really nice property that you can do local optimizations to improve performance without throwing out the model.

Doug GregorFinally, you don't need a runtime, except for like we have retain and release. That's it. Right? Couple of instructions, it's an atomic, basically. And so you need very little runtime support to make this model work. And so that lets you scale your language down to run it in environments where you would never consider having a garbage collector running. Yeah. Right. And so that gave us the option to actually say, well, you know what, this is actually reasonable to use down in a kernel or down in firmware somewhere where you don't you could never do a GC and you would have to essentially switch the model.

Doug GregorWe don't have to change the model. We just have to make sure it's optimized well in those places.

BryceAnd it also gives you determinism, right? You know, you you don't you yeah.

Doug GregorThat is the other thing I should have mentioned. It gives you determinism of you know at the end of your scope or when you stop using a value that the reference count is going to drop and it will go away. So you you can reason about it better than you can with a garbage collector.

BryceI I've always been what's one of the reasons I've always been a fan of Swift has been ARC. Um, because I I for many years worked on the HPX runtime, which is uh uh an HBC parallel programming runtime. And uh HPX is uh uh relies entirely upon reference counting and it has this um distributed uh reference counting mechanism um where it's based on uh you know you get a number of reference count tokens, and then as the thing moves, you can split them, and so you don't have to have that much reference counting traffic, and that's what I spent a lot of my early work on.

BryceAnd um, you know, one of the things that I came to appreciate in that time was in particular when you're working on a concurrent system, you you need to have some form of managed lifetimes because you don't you you cannot solely rely upon you know new and delete, and you don't have this scoping. So you can't just rely on some manic unique pointer. You you need some way of uh managing the lifetimes of objects.

BryceAnd I really believe that reference counting is the best option. Um it's deterministic, I think it gives you the best performance. Um it's straightforward to reason about. The only thing you really have to concern yourself with is the potential risk of cycles. Um but uh in my years of dealing with reference counting systems, uh it's never been a major, a major uh issue. Um I've run into it a couple times. But uh it it to me and it's it's uh it's I think there's I think ref I think automatic reference counting is something that almost makes Swift unique because I think most of the languages that are in the space of uh a language like Swift either has no automatic mem memory management story or they do a GC.

BryceUm reference counting, I think, has just not been very popular over the years. And um uh it's one it's one of the things I really like about Swift, I have to say.

Doug GregorYeah, it's really interesting because you know there's been an enormous amount of effort putting into making GCs like way better than they were before, right? I I ten years ago I probably would have said, uh, you know, no GC pauses from when when you use reference counting, but GCs have gotten so much better that you know that that can be an issue, but it's pretty rare. The main place it affects users is with reference cycles. And you know, we do see it, right? We do see users that end up with a reference cycle.

Doug GregorAnd we were very deliberate in when we picked reference counting, we said there shall not be a cycle collector. We consider the presence of a cycle to be a programmer error. The memory will leak, you will find it with your tools, and we'll provide the tools that make it obvious where that is, as well as providing the language tools where you can have a weak reference, for example.

BryceYeah, I was just about to ask. So is there is there in Swift uh some sort of weak reference mechanism?

Doug GregorYeah, right. So the so the way it works is um you have, you know, if you just have a you know an op a variable that refers to an object, right, the value of class type, then that is a strong reference. It'll keep it alive. Uh you can describe that variable as a weak variable, spelled like that. Uh the type of that variable is always an optional of that class type. The reason is that optional will implicitly become nil if the object ever goes away.

Doug GregorBecause a weak pointer does not keep it alive. It has to tell you. Now, optionals are actually deeply ingrained into the Swift language, and there's a lot of uh syntactic sugar to make them easy to use. Part of the reason we can do this thing with weak is that there are no null pointers in Swift. This was a massively controversial decision uh when we when we first did it when Swift was introduced. But basically, if you have a value of type person where person is a class, there's always an object there and is kept alive.

Doug GregorIf you want a null pointer, that's fine, but it shall now be of type person question mark, meaning an optional person. And to access it, you always have to go and look like is it nil or is it not nil? And we provide nice syntax to make it easy to go in and do that um consistently uh to make your code more robust. And it's it's one of those places where it aids correctness. Like it the first time you, if you're coming from C, you're like, I know how to deal with null pointers, I know what I'm doing.

Doug GregorWhy are you making me do all this ceremony? You kind of get angry with us. We had that. Uh but after a while, you realize you stop thinking about null ever. And the only time it ever comes up is when you have an optional and the type system is showing you what's going on, and the type checker is helping you deal with it in a reasonable way. And you realize like you no longer have this background, I'm worried about this stuff. Because the language has got a better model that reinforces correct code.

Doug GregorAnd you know, this is something we could totally have done weak pointers with the GC behind. Like that's pretty common in there, but it fits so nicely together using the reference counting model with all of these benefits of you know, determinism and optimizability, and you know, small runtime overhead for that feature.

BryceBut I I think the the determinism and the performance advantages to me allow languages with reference counting to thrive in spaces that they otherwise would not be acceptable in. Um, and which is which is one of the appeals to me.

ConorBe sure to check these show notes either in your podcast app or at adsphepodcast.com for links to anything we mentioned in today's episode, as well as a link to a GitHub discussion where you can leave thoughts, comments, and questions. Thanks for listening. We hope you enjoyed and have a great day.

BryceHi, quality. That is the tagline of our podcast.

ConorIt's not the tagline, our tagline is kingdoms with sprinkles of information.

Open the transcript file

Hosts and Guests

Guests

  • Doug Gregor Guest Inferred
  • Douglas Gregor Guest Inferred

Credit the people on this episode

Be the first to rate this episode

More episodes

All episodes
  1. Episode 313: C++26 Reflection vs CRTP, AI, Kids & More with Zach Laine

    Nov 20, 202647 minAudio

    In this episode, Conor and Bryce chat with Zach Laine about C++26 Reflection vs CRTP, AI, kids & more. Link to Episode 313 on Website Discuss this episode, leave a comment, or ask a question (on GitHub) Socials ADSP:...

    • Apple Podcasts

    Credit the people on this episode

  2. Episode 312: The C++26 Contracts Fiasco

    Nov 13, 202652 minAudio

    In this episode, Conor and Bryce chat with Zach Laine about the C++26 contracts fiasco. Link to Episode 312 on Website Discuss this episode, leave a comment, or ask a question (on GitHub) Socials ADSP: The Podcast:...

    • Apple Podcasts

    Credit the people on this episode

  3. Episode 311: Career Changes, GPUs & More with Zach Laine

    Nov 6, 202628 minAudio

    In this episode, Conor and Bryce talk with Zach Laine about his career and the career changes he has made. Link to Episode 311 on Website Discuss this episode, leave a comment, or ask a question (on GitHub) Socials...

    • Apple Podcasts

    Credit the people on this episode

  4. Episode 310: Leaving NVIDIA for... ?

    Oct 30, 202624 minAudio

    In this episode, Conor and Bryce talk about Bryce's next chapter and where he is leaving NVIDIA for. Link to Episode 310 on Website Discuss this episode, leave a comment, or ask a question (on GitHub) Socials ADSP: The...

    • Apple Podcasts

    Credit the people on this episode

  5. Episode 309: Default Construction & CppCon 2026 with Jason Turner

    Oct 23, 202629 minAudio

    In this episode, Conor and Ben chat with Jason Turner about default construction in C++. Link to Episode 309 on Website Discuss this episode, leave a comment, or ask a question (on GitHub) Socials ADSP: The Podcast:...

    • Apple Podcasts

    Credit the people on this episode