Conor

Ben, we're talking about Ben. Prolific speaker on the C speaker track, uh, algorithm expert, polyglot.

Ben Deane

Entendlement is interesting for another reason. It's I've long considered it the sort of most unloved algorithm or most overlooked algorithm.

Conor

Welcome to ADSP the podcast episode 75, recorded on April 19th, 2022. My name is Connor, and today with my co-host Bryce, we talk to algorithm expert Bendine about C algorithms. Well, let's let's do this right off the bat.

Bryce

We're hopping we'll well before we hop into things, we'll get uh I I know I have a feeling I know what Connor's about to do. They're never gonna sponsor us, Connor. They're never gonna sponsor us. Did you hear net Netflix down 20% to the after after hours today? So maybe maybe maybe NVIDIA will replace Netflix as the N in uh uh in Fang.

Ben Deane

I feel like that acronym is now increasingly irrelevant with the players in the market, you know.

Bryce

I feel like I throw a lot of shade at Netflix on this podcast for absolutely no reason. And if anybody who works at Netflix is listening to this podcast, I really have no problem with you. It sounds like a great company, but I will continue to throw shade. Alright, Connor, do your thing.

Conor

Um, our thing is that we're terrible. Well, we're good. I think we're decent podcast co-hosts, uh, but we do a terrible job of introducing our guests as as demonstrated by our last recording with guests when we had Patrice and uh Jason.

Bryce

I thought this was a bubbly. I thought you were gonna go do a bubbly.

Conor

Oh no, we well, we've got the brand new bubbly flavor.

Ben Deane

It's well, so how is bubbly? Because uh what's the name of the other one? I can't remember. Um uh Perrier? No, no. Uh-huh. No.

Bryce

They even they spell bubbly wrong.

Ben Deane

Oh gosh. I had one today and I can't even remember the one. No, it's uh it's a flavored, it's it's Le Croix? La Croix, that's the one. Oh see, that's big down in the States. It's not really that big up here in Canada. But I've just discovered Spindrift, which is which is like La Croix, but nice. It's I have to say they have compromised. It's not zero calories, it's a whole five calories a can.

Bryce

La Croix, if you want, if you want to sponsor us, we will make Ben come back and apologize.

Ben Deane

I'm not a big LaCroix drinker, but I do drink LaCroix, but I've recently discovered Spindrift.

Bryce

We tolerate it. We tolerate everything.

Conor

Yeah, exactly. What's what's the difference? See, well, I just want to talk like this is meta because we were gonna this is what we're we are literally proving what we're bad at. We're bad at introducing our guests. We got two seconds into introducing Ben, and then Bryce was like, weren't you gonna talk about bubbly? And then whatever percentage of like attention deficit disorder I have, I was like, no, but let me tell you, and now we're talking, anyways, let's finish this topic. Uh Spindrift, is that what it's called? Spindrift?

Ben Deane

Spindrift is is like LeCroix, except they've compromised, as I say, very, very slightly on the calories for much nicer flavor, in my opinion. Interesting.

Bryce

You know, my my my go-to drink is vitamin Water Zero. I literally have a fridge full of vitamin Water Zero at all times.

Conor

I like I don't drink water. I just isn't that like the crypto, isn't that like the crypto of drinks? It's just a big scam.

Bryce

It's I'm uh it tastes too good to have zero calories. I'm sure something in it is gonna kill me, but I am addicted.

Conor

That's fair enough.

Bryce

Literal, literally, my my my mother was here the last week visiting me, and she's like, I want a glass of water. There's no water in this apartment. Can I just have some regular water?

Conor

Wait, don't you isn't is the tap water in New York not drinkable?

Bryce

It is, but I don't know. Like they want all of all of the women in my life want like old water, and my apartment does not have it.

Conor

Alright. Well, any of the companies, uh, aforementioned companies, including Sapsucker, which might be the spindrift of Canada, which I think it has like 20 calories or 15 calories a can, but it's like it takes like recycled, I don't know, syrup or something.

Bryce

And um recycled?

Conor

I don't know. Honestly, it's got it's something it comes something from a tree. Something from a tree's in it.

Bryce

I don't want syrup that somebody else has already had.

Conor

I I don't think that's I don't think that's what it is, but it's like I think there's a process of making syrup, and then there's some like uh, you know, like in an oil, what do they call that stuff in an oil plant or oil refinery, and then there's like a pool where all the bad stuff goes. It's called like something off.

Bryce

Oh, that makes more sense. That makes more sense. What did you I think Ben actually knew the name of it?

Ben Deane

What is it called? You mean like a sump? Like that just collects the runoff kind of yeah, yeah, runoff.

Conor

That's what I was thinking of. That's the word. Um my older sister as an engineer would have definitely uh had it right away. And uh sister? Shannon. She's um yeah, she's in Calgar now. Left left me all alone in Toronto, but uh I think it's basically the runoff of like maple syrup, which is still good for stuff, but not syrup. And so this drink company could be the spindriff equivalent.

Ben Deane

Speaking of syrup, I was gonna say the current syrup I have in my fridge. I just made pancakes and I noticed on the bottle, and this is gonna offend everyone, right? On the bottle it says product of USA and Canada.

Bryce

Oh, that is that is wrong. That is wrong. Why? There is one good maple tree product from Canada, and that's maple butter, specifically the type of maple butter that JF's brother brings me every time I see him. And yes, this is a coded way of me telling JF to tell his brother to continue to bring me uh maple butter.

Conor

He just said like two episodes ago that he doesn't have time to listen to this. You just ripped on him for like 10 minutes.

Bryce

Somebody else will listen to this, like honor somebody, and and will then tell JF, and then JF will tell his brother, I got a system. But no, real maple syrup is uh is made in Vermont. It is it is a product of Vermont, America.

Conor

Alright, ten minutes later. Today, our guest, I think, other than Sean Parent, you are the second ever returning guest to the podcast, unless if my memory's failing me. We've definitely and and for the listener, we cut every time we record with someone into like two or three episodes. So if you're thinking, what are you talking about? Everyone's been on two or three times. False, everyone's been on once, except for Sean Parent, and then we we cut that up into 30 to 35, 40 minute episodes. So, Ben, the last time you were on was in um the on during the virtual C Now of 2020, I think.

Bryce

No, it was 2021.

Ben Deane

I was gonna say this almost this time last year.

Bryce

This podcast did not exist at the time of that where C now 2020 would have happened.

Ben Deane

You're on your 74th week or whatever it is.

Conor

Yeah, let's well, let's be honest. We've all lost track of time and uh in in this virtual new world that we live in. Um but wait, Bryce, don't say anything. We were we were like we were we were almost there, we were almost introducing him. Uh hands are on the hills. So if you if you're in the C community, you've probably heard of Ben. Uh he's a prolific talk speaker, winner of many best speaker awards at C now.

Bryce

Did you say talk speaker?

Conor

I'm a talk speaker. Well, you know, honestly, folks, I should read a sentence that I wrote the other day on Discord. It was so there were so many skipped words and grammar mistakes. Anyways, so he's a prolific speaker with dude.

Ben Deane

Speaker with you.

Conor

Speaker, I think that's the one too proper. I gotta be honest, I just got back from running 18k uh very quickly at Run Club. So I'm a little bit in uh I've like I'm in like a two and a half thousand calorie deficit right now.

Bryce

Did you shower?

Conor

I did, yeah. Uh and I I showered right before. Anyways, Ben, we're talking about Ben, prolific speaker on the C speaker track, uh, algorithm expert, polyglot. If you've listened to his CPP cast episode, I think you started off by talk going through the uh plethora of languages that you have um studied at one point or used. Um likely.

Ben Deane

Jason always asks about history and that sort of thing. So yeah.

Conor

Yes, a fellow uh Ben is a fellow uh program language historian uh enthusiast. What do you what is it? Is it a bookophile or uh some someone you have a library? Bibliophile? Bibliophile.

Bryce

You are really bad at words today, Connor.

Conor

This is gonna be a rough takeoff. And the reason Ben's on today, and Ben's one of my favorite speakers, what let's list off a couple of your talks. What's the I'm not even gonna remember any of the names of them. Declarative.

Ben Deane

Yeah. One was called easy to use, hard to misuse, declarative style in C. Yes. Fantastic.

Bryce

Ben, what's your favorite talk that you've ever given? Uh that's probably a hard question to answer.

Ben Deane

That's a hard one, but I suspect like several speakers, I feel like I've given the same talk in different guises several times. Um, or variations on a theme. There are two styles of talks that I really think about. The easy style, which is here's a thing I did, here's the code, here's a C feature, here's how to explain here's how to think about it, explain it, etc. Right? The easy style of talk to me is like you have code, you explain it. The harder style, the more but the more rewarding style is the sort of more philosophical style. Um can be illustrated with code, of course, but uh that style is more of the kind of declarative style talk. So I like I like both styles. Like I I I like the philosophical style, but but the easier one to put together is definitely like here's the code, here's how it works style. Easy to use, yeah.

Conor

Easy to use, hard to misuse. You had the other one too recently, I think it was at CPPCon, which was the or no, that was at C now as well. Identifying monoids. Is that yeah? And also the talk that uh I don't think ever gets mentioned enough. And it definitely, well, it was at CPPCon, so it didn't quote unquote win any awards, which is maybe why some of the other ones get more buzz, uh, even though obviously they're they're really good, is the std accumulate and algorithm empire. I think that talk is fantastic because of sort of just the exercise of, I think you use the word sort of perversely, like you're using the algorithms in ways they shouldn't be used. Yes. And and bending them as an exercise to really see like what they're capable of. And a bunch of the things you point out is like, don't do this stuff, but the exercise of doing this with the, you know, where you give a little summary of the talk.

Ben Deane

Well, that talk, like the talk that Jason and I did, constexpr things, it it's kind of interesting to look back and see that that talk is of its time, right? And to look back and be like, wow, that was like you say, that was just an exercise in making accumulate implement different algorithms, and it was a sort of a thought experiment, if you like. I mean, it was an actual code experiment, but in a sense it's a thought experiment, but interesting, but interesting to look back at and see, you know, it's kind of of its time now, and I look like all of us, I look back at my own code and I think, you know, what was I thinking? I shouldn't have done that. Like there are better ways to do that. In a sense, for that talk, it's fine because the whole thing is predicated on just this is really interesting and a good way to think about it, but don't ever write this code sort of thing.

Conor

The key quote from that talk that I've sort of clipped into a couple of my talks is that at a certain point you go through the exercise of seeing how many algorithms you can actually implement using std accumulate or some. The sort of platonic equivalent of std accumulate, yeah. Yeah. And yes, so 77 of the 90 of the Right, right.

Ben Deane

In I think we had 90 algorithms in 14, something like that.

Bryce

Connor, what just like one sec, one sec. Connor is experiencing undefined behavior right now.

Ben Deane

Yes. Come back to us, Connor. Are you good? Can people can people hear me? Yeah.

Bryce

We can hear you throughout the entire ordeal.

Ben Deane

I'm guessing the listener can also hear you.

Conor

I in the middle of me saying the 77 of the 90 algorithms are implementable in terms of accumulate. Uh, this woman started talking in my headphones saying, Your headphones are out of battery. Please recharge, turning off. Okay.

Bryce

See, but I feel I feel like that was probably not the first warning that you got that your headphones were low on battery because that's true.

Conor

That's true. That lady said something earlier today, and I ignored her.

Bryce

Um Yeah, you should have listened.

Conor

Alright, Ben, back to you.

Ben Deane

You were gonna say one of your talks. One of my talks that had quite a bit of impact and people still seem to remember uh or or at least let's say folks outside of the rarefied bubble of conference speakers that I talk to sort of in the real world uh and or in the games industry as I was in, um they really enjoyed the um using types effectively talk that I gave uh to I think it was CPPCon 16. Is that the one where you have the you know how many types of I have a little game, yeah, a little type theory game um talking about sites ty uh types as characterized by the cardinality of the set of values they hold.

Bryce

Are you are you coming to C Post Post now this year, Ben?

Ben Deane

Yep. Yes, I'll be there. That's in just over that's in less than two weeks now, right?

Bryce

Yeah.

Conor

Yeah, by the time this airs, what day is it? It's the 19th century.

Bryce

In fact, one of the reasons that we're recording this is because I will be at C Post Post now. And uh or the one of the reasons we're recording this now is because I'm gonna be gone for the first two weeks of May. And uh and Connor needs all the Bryce Gold to produce to produce the podcast.

Conor

Maybe I'll just end up going solo.

Bryce

We've never we've never done an episode where we No no no that's not true. We had the one episode, right? At C Plus P now last year. One episode without me.

Ben Deane

Connor and Tony and I.

Conor

Yeah. Ben it was Ben and I for the first bit, and then Canadians have a habit of showing up halfway into the recordings.

Bryce

Um that's not foreshadowing. There's no random Canadian showing up today.

Ben Deane

So how this goes is I I listen to your your podcast probably every other week. Um, and I catch up. And then invariably, it's like on the Saturday morning when I have some time, and then invariably I I I DM Connor on Twitter. I only use Twitter for DMs these days, but I DM Connor and I say, I have opinions.

Conor

This is true. And this is why Ben, so Ben, this so this is uh gonna be, I think, I'm not sure if it's a response to multiple podcasts, but Ben DM'd me after the C algorithm family feud interview question. Uh of which, oh, I should I should call out.

Bryce

We've got we've had like two or three people who have coded up the solution.

Conor

Two people, one of them was from the C well, shout out to the C North Denver meetup, because I believe Tyler Oh right. What was it? I want to get the name correct here. Tyler Weaver made both a YouTube video and sort of a God bolt and a quick bench, and was basically saying that it looked like the max element where removing an element and then just doing another max element was the fastest and transform reduce was like the slowest.

Bryce

I wasn't that ridiculous max element thing my idea.

Conor

Yeah, but we said it was a bad idea, though.

Bryce

Hey, hey, hey, hey. I, sir, I sir am an architect now. My job is not is not to write code or to have good ideas. My idea my job is just to come up with ideas, and somebody else figures out if they're good or not.

Conor

It's it's yeah. Well, so we'll get back to this. But anyways, there were Tyler Weaver had a YouTube video, and uh there was another individual. I'll grab his name at some point. But so Ben DM'd me after listening to this and said, We need to chat. And I said, uh, do you want to come on the podcast? And he said, Yeah, sure, it doesn't matter to me as long as we get the chat.

Bryce

And so we need to have we need to talk, we need to have conversation.

Conor

Well, this is it. Like, I have opinions, right? Yeah, who who is it that messed up the worst? Was it me or was it Bryce?

Ben Deane

No one messed up. And and I actually really enjoyed the format where like the game show format where you're like you're like, okay, Bryce, now name name the answer. Oh, but name the series of answers in reverse order. So when you posed the question, which was uh find the top two, right? Or the or the minimum two or the maximum two, same same thing, right? In an unsorted array of integers, let's say. Um my immediate thought was nth element. That's what nth element does. That like and and then uh but then of course, as is your won't, um you solved it with some reduction. And that's fine. Of course.

Bryce

This is the reduction.

Ben Deane

Yeah, yeah, yeah, yeah, yeah. So so it kind of is. I still believe the correct name for that algorithm is top two or top n, but perhaps. I mean, I I'm not claiming that nth element is particularly a good name for that application. Uh so it occurred to me like you you're interviewing probably for library developers, right? As opposed to application developers. And there's a difference there. That's right. Because in an application developer, the obvious thing is, well, nth element, right? In a library developer, what I would like as an application developer is for nth element to be implemented with a reduction under the hood, if that is faster, taking into account array size, parallelism, element size, that sort of thing. You made a comment to that effect yourself, Bryce, where you said something about like the the you know, the performance may vary depending on several of those things anyway.

Bryce

Yeah, so so one of the things I think is a good part of this question, and if if I I you know, I as if you said before, I don't ask technical questions in interview but views.

Conor

But if I was and I was gonna use this question, um look at Bryce, trying not to say oh, this was a good question. Actually, let me correct myself. It's a bad it's in a bad category of questions that I don't endorse, but in that bad category.

Bryce

It's more I call into question the entire notion of how like traditional tech interviews are are are conducted. But that aside, um if I'm looking for a library developer, I'm looking for somebody who looks at this question, comes up with multiple different answers, and then like the really the right way to answer this question is to tell me that there is no one correct answer. That here are the options, and that there are going to be different circumstances under which these will be, you know, each one of these will be the right one. And that like maybe the way that you would build a high performance implementation, a generic implementation of this, would be to use a combination of different strategies.

Ben Deane

Yeah, yeah. Because I mean, as a qu as a very specific question, top two, great. The generalization is top n, of course, which in some sense is nth element. Yes. And and that's where it's like, well, top two, it, you know, like a pair is very easy, a very small amount of data, assuming you have a small element size. Um but if you want top a thousand, like that might be a very different sort of problem uh that that parallelization might not be amenable to. And in general, median is a problem that's annoyingly hard to parallelize, I think.

Conor

It's definitely not one of the trivial bread and butter, like a reduction, the bread and butter one.

Bryce

Yeah.

Conor

So that's interesting. I guess my first thought is how many algorithms, like the first one I can think of is I think the st uh std sort is has the behavior where depending on the size, it will choose different uh sort algorithms. And I think like the actual name of, I'm not sure if it's the same across um uh libstdc and libclus plus, but I think it at least one of them uses intro sort, which is like a combination of merge sort and I'm not gonna get the other one wrong, but like quick sort or something like that, where above a certain size it chooses merge sort, but then once it gets down below a certain size, it then switches to another one, and that's actually um there's it's something similar in Python that they have some kind of hybrid algorithm that's and it's named after some individual. So do you know how many algorithms actually in the algorithm header have behavior like that? So that's basically what we're you're arguing here is it should be top n that uses nth element for the cases that on average it's good for, and then it switches to a simple reduction that just has.

Bryce

I think really the the question is should nth element implementations special case the like the case of two elements uh or the case of one, two, and like three elements. And actually it would be an interesting experiment to see um uh what the cutoff point is that that at which it makes sense to like special case.

Ben Deane

The other observation I had is I I think the reduction is you can correct me on this, but I think it's by necessity giving you back the ordered pair. The way it would have to be implemented. Whereas nth element, of course, doesn't order the top n necessarily.

Conor

Well, it depends. So like in the parallel version where you have the associative and commutative operation, it actually will be.

Ben Deane

But I think the semantics, yeah, I think the semantics of reducing the pair mean that you have to designate one as the higher and one as the lower, otherwise you'd have to you wouldn't.

Bryce

Yeah, well wait, but why why wouldn't why wouldn't the nth element give you give you the ordered point?

Ben Deane

Nth element doesn't have to order the.

Conor

Because it's not sorted.

Ben Deane

It might end up ordering a pair. I don't exactly know. But if you ask it for top n, it's just gonna it's gonna pin the nth element. Everything above it is gonna belong above it, but not necessarily in the right order.

Conor

So the the API for nth element is three iterators, the beginning of the range, the element that you want to get at the right spot, and then the end of the range. And the only thing you're guaranteed is that everything below that point, your nth element point is less than, or whatever, if if you're using the default comparator, and then everything above it is greater. And then you're guaranteed that at that point is where it would show up in a sort of thing.

Ben Deane

If it were ordered, that would be a partial sort, right? That's that's exactly.

Conor

And I think Kate Gregory actually at one point, she in one of her talks says that partial sort copy should be called top n. And then in the talk that I later gave, I pointed out that actually partial sort copy should be called top n sorted, and nth element copy should be called um top n because the partial sort one is the one that guarantees that your your top n are going to be sorted.

Bryce

But but if the if the case if you're using nth element to do top two, doesn't it happen to be the case that that that it'll be in order just for that case of top two?

Ben Deane

Yes. Oh yeah, actually that's a good point. But but I guess the point is it hasn't done any extra comparisons to make that the case, right?

Bryce

I just meant that like in the case of two, it like it does sort of happen to be. Um yeah, I mean, maybe, maybe Kate's, I I don't I don't think that we need a top n element. I think that the that uh uh you know it really it's just you either use nth element or you use partial sort copy, um, depending on whether you want them ordered or not. Um but but the this this case, uh this question of um special casing um uh you know for some of these lower count cases is in is an interesting one. You know, a lot a lot of the a lot of the um optimizations that we end up doing in the Cub library, which is our library of um algorithmic primitives, um of parallel algorithmic primitives for CUDA, um, a lot of the big performance wins that we get is because like we identify a special case that you know happens to be uh easy to optimize and also like a lot of people just fall into it. So almost almost every algorithm has some decision tree of you know uh if it's this case, you do this thing, and otherwise you do this thing.

Ben Deane

It's I I have long considered it the sort of most unloved algorithm or or most overlooked algorithm. Uh and as you know, in I think it was 17, um the complexity boundaries of sort were tightened. It used to say prior to seven, I think it's 17, right maybe 14 or even 11, but maybe it was 11. Um but it used to say uh n log n on average, and now the standard says sort is n log n, period. Right?

Bryce

I I think we cleaned that up in 17 because we we cleaned up a lot of the complexities when we added the parallel algorithms and we had to figure out the complexities for them, but I could be wrong.

Ben Deane

The same cleanup was not done for nth element. Nth element is linear on average, but I think the same essentially the same work, which is the work of um Dave Musser, um he did a lot of work on this uh back back in the 2000s, I think it was, um, that that led to intro sort that Connor was talking about and and the possibility of making sort n log n on average. I think that same underlying uh that same underlying work uh translates to making nth element linear period, if we wanted that.

Conor

Someone's gotta write a paper. Listener, out there if you're interested in getting involved in standard committee work.

Ben Deane

Yeah, because intro select is the uh algorithm counterpart to for n element that for for intro sort.

Conor

Right, right. Yeah. This ends part one of what will probably be a four part series on algorithms. Thanks for listening. We hope you enjoy and have a great day.