Reliability Lessons From SQLite
Comments
Transcript
Machine-transcribed by YouTube. Expect errors in names, commands and version numbers.
Thank you, Isaac. You give me too much credit. Seriously. Seriously. I um I've got a lot of slides here. You might want to follow along. I I might go quickly. I don't know. But I'm gonna This is very technical talk. I love giving technical talks. And so we're going to we're going to uh talk technical here. Um SQLite exists because Informics did not work.
Uh we're back in the 1990s. I had a uh I was a sub subcontractor. I
was two major corporations removed from the end customer. And my job was to write uh some client programs that would talk to an informant informics database, pull some data off of them, uh find good approximate solutions to an NPcomplete problem and present a compelling graphical display of the solution. And that was a fun problem. Enjoyed working on it. uh the two major corporations in between me and the customer took all the profit. I did all the work but I had a lot of fun doing it but so but along the way the informance was supplied by the customer specified to the customer. I didn't have any choice in it. I didn't have any control over it and sometimes inform would go down. Uh
this was ultimately traced to a configuration problem. Uh the system administrator didn't set it up right or something. But when the informant server would go down, my my program had to paint a dialogue box. And and before you hate on me for this dialogue box, that was the state-of-the-art dialogue box in the 1990s. Uh the um and so what else can you do? I mean, I can't talk to the I can't get to the data I need. I have to put it up. So, but the the end user, they would see this and then they would send me the bug report and that made me very sad. Um,
that caused me pain to use Andrew's words. So, I thought, do we really need that server? Why can't we talk directly to the data on disk and get rid of the server in the middle that's going to um uh, you know, that's
that's causing me this pain? Why why can't why why do I need that? And so I thought, well, um I I I looked around. I couldn't find anything like that. I thought, well, I'll just um I'll write my own database
engine. How hard can that be, right? Uh
turns turns out it's harder than you'd think. But um so that was the that was that's kind of the origin story of SQLite. It's it's built because the software was not working. And and it's grown over the years. It took me a while to get it going, but uh it it announced, in case you haven't heard of it, when I made these slides, I didn't realize that everybody knew what it was, but in case you haven't heard it, it is a fullfeatured SQL implementation. It's power save asset transactions. A C library, it's not not a system. It's not something that can go wrong. A database is a single file on disk. You just move the file around. You can email it to your friends. Uh which is pretty cool.
Well-defined file format. You might not know that the uh US Library of Congress
has has designated SQLite database files
as a preferred storage medium for long time long-term archival storage of binary data. [applause] I I did not know that myself until somebody on social media pointed it brought it to my attention. Um but it's backwards metal. It's in the public domain. We release everything freely. So it uh
there's a team of us working on it. It's a small team. Uh right now there are three committers including myself. Uh all the code is in a single file of C. So it's really easy to install in your project. It's a big file uh you know over a quarter million quarter million lines long. But we we don't actually edit that one big file usually. Uh we've got a bunch of individual source files and our build process is to kind of concatenate C code together to make one big file. It's all handwritten. Well, I say that uh we do write programs that write C code. We're not using agents to write code, but we do write our own programs to write code. And so some of it's built that way. So anyway, uh this uh the the project design lifespan is 50 years. first code was on May 29th of 2000 and all all of my
friends were retiring and you know they're you know spending time with their grandkids and Richard when are you going to retire? Well in 2050 I've got 24 years to go. So um at least that's the plan. We'll see how that goes. Um and to to my great surprise it has become possibly the most widely used bit of software in the world. his eyes. He pointed out at the beginning he he stole a slide from my talk. Uh it's used in all your mobile devices right now sitting around you where you're sitting within probably five or six feet of you there's probably hundreds of instances of esculite quietly running and making your life better and uh that's just
amazing. There's by there's there's no way to know because it's free and most people use it and don't even tell me. So, but we good estimates are well over a trillion SQLite databases in active use right now. A responsible uh the
people at Apple and um Google tell me that about half of the file system IO on
your phone is SQLite. Most of the other
half is um media files, big media files that are going back and forth. and and there's there's you know people do write XML and and whatever and other database engines but most of it's SQLite.
So how did this happen? Um well you know
a few years ago this this cartoon appeared and oh they're talking about SQLite. Uh I'm not sure. I've never actually been to Nebraska [snorts] and I do have people helping me but that that kind of gives you the idea of what's been going on here. So why is it used so much? What what is it about SQLite that that it it has become so popular that it went so viral? Well, one it's it's useful. It solves a problem. U people need to store their data. The the saying is that without data you're either lucky or you're wrong. So so you need to store your data and everybody wants to do that. Um it's easy to deploy. It's just one file of C code.
You just drop it in your program and compile it and away you go. It's open source, public domain. It's out there. You can do anything you want with it. And people do do very unnatural things with it. Uh but but
that's that's the freedom that comes with it. And and and you don't have to pay anything. Uh which improves its adoption. There's no low dependency. I mean it it it just uses the standard C library there. You it doesn't have a big supply chain to to worry about. doesn't use much in terms of resources. It runs on your eyewatch. It runs on small embedded devices and it usually doesn't crash. So, uh you know the so the reason is
that it it became popular I think is because it solves more problems than it creates which is kind of a good life philosophy in general. If I mean if you want to be successful in your career, if you want to have a good career, if you want to do things, solve more problems than you create. Uh, another way thinking, another way of thinking of that is it just works. And and as I've discovered since I've been here, um, there's been there's like a a
huge community of people who just enthusiastically embrace uh, SQLite. I
mean, I saved this this tweet from a few years ago. I don't know what they were talking about. I don't I don't know who Mr. Christokov is. U I agree all modern
tech is inherently bad except for SQLite. He may have an overly positive opinion of SQLite, but I do appreciate his enthusiasm. Thank you very much, sir. So, anyway, uh it took me a while to kind of get this going. Uh there were there there was right we're on SPite version three and it came out in 2004 and there was a version one and a version two and see I've never had I've never had any formal training in database technology and Dan who who's contributed maybe almost half the code has had no computer
science background whatsoever. he's a mechanical engineer and so we had to learn a lot for a few years and but when we came out with version three it really began being adopted by uh the tech titans of the of
that of that age. Uh do does anybody remember America Online? uh they they they were a big customer and uh they paid uh they gave us some proh some money to work on some things and um the world's largest manufacturer of cell phones at the time was Motorola uh Nokia got involved with this but but
uh before this I thought you know I can write code that is practically bug-free it's just it just runs it doesn't ever have any problems but about this time uh it was going viral and an obscure startup company in Mountain View was um uh starting to use it. Uh you might have heard of it called Android and and they were sending me like a bug report daily and this was this was frustrating and I was beginning to realize maybe I can't write perfect software after all and I what what can I do about this? And I was also doing some some work for Rockwell Collins and that's an avionics manufacturer and they told me about this this document called Do 178B. Uh does anybody ever heard did who's familiar with this? A few people. Uh it's obsolete. Now apparently there's DO78 C.
Uh I I never purchased that one. The what what I like about this is that it's very succinct. It's only 84 pages long.
Uh most of the quality assurance documents are like bookshelves full of stuff. This one's very short little brief thing. You can go online and buy it. It It costs $6.50
per page.
I'm not making that up. They think very highly of their PDF. Um uh but the it it it gives you a really cool way of thinking about software from a from the ava avionics point of view and uh it's kind of there's three main elements they look at. They look at the requirements analysis and tracking. We we do that at SQL. I'm not going to talk about today. Um they talk about configuration and life cycle management. And I've got one slide
on that. Most of the rest of this talk is going to be about testing and how we do it and how that makes it more reliable and all the problems we've had and how difficult that is, but why that's really what you ought to be doing. So, uh my one slide
on configuration management is that uh the DO178B inspired me to write my own version control system for SQLite. It's called Fossil. Um and people say that I'm crazy for that. I should be doing everything in in git. But this was contemporaneous with git. Git and fossil started that they both derived from another version control system you probably never heard of called um monotone. Anybody heard of monotone? A few people have heard of that. Monotone had the original idea of identifying the artifacts using a a
cryptographic hash and and so git uses that. Fossil does that. It's the same basic idea. It's just different implementations. This is a whole another talk on fossil. And if you want if you want me to give you that talk in private after this, let me know. [snorts] But uh the big thing to learn from from DO17B is that the
idea if it if it it hasn't been tested, it doesn't work. And so the the rule at SQLite is that we follow 100% MCDC
or 100% modified decision cons modified
condition decision test coverage
and that's that's a mouthful MCDC and it's a very technical meaning and you can look up the details of the technical meaning on Wikipedia if you want to. For this talk and in SQLite I I I'm going to narrow it down to two things with this test coverage. That means that every branch operation at the machine code level has been tested in both directions. And two, every bit in a bit mask test
makes a difference in the outcome. So for example, if you've got a piece of code that says this is madeup code, obviously a equals 5 and b is seven or c is 11. You've got at least four test cases to verify that this works. Uh you've got to say that you've got to have a case where a is not equal to five. And then by the by the virtue of short circuit evaluation, you don't need to deal with anything with B and C in that case. But if A is equal to five, you have to do B equals 7, B not equal 7, C1, and B not equal 7, C not equal 11. Do you see how that goes? So you've got to you've got to verify that you have all those cases covered.
You have to have test test cases that cover all those conditions. And and if you're doing a bit mask test like X and
seven not equal, you need to make sure that each bit has been tested separately
in that test. And there are other things about this, but this is this is the idea of what you have to do. So how do we actually measure uh this in and our product? It turns out that GCC uh allows you to compile things in a special way and then you run the program and it gives you uh the the program called G-code generates uh a big file. uh in the case of SQLite it's about 15 and a half megabytes 320,000 lines of of output that shows you how many times every branch operation was taken and then you you you develop a script or something to process that file and it shows you how many times every branch is taken and in this this case we've got 100% MCD coverage on that particular line of code because all the branches were exercised but uh it's
looking for cases where the number of times taken is zero and and then but how do you do the the
bitmask thing? So we we we've got a a macro called test case and when we're testing coverage test case actually puts another branch in in the in the code and we have to give it something to do. I wrote no op there but it does something so that the the optimizer doesn't optimize it out. Yeah, you have to give it something to do. And then then you put these test case macros throughout the code that verify in this case that we're that that every one of those bits that we care about in that bit mask has been tested
when it's both set and clear.
But but but notice that in in a deliverable test case is is is nothing. it it it it
only runs when we're when we're checking to see our uh if our test values are right. You can also use test case for checking boundary condition. This is also very useful for making sure you're not don't have one off by one errors that you check that make sure you got a test case uh where on on both sides of
an of an inequality. But but the thing to realize here and and deal 178B goes into great detail on this is that you've really got you're testing three different types. There's three different kinds of tests that you're interested in. You've got first of all, you have to verify that your test cases are actually testing what you
think they are. You have to make sure your test cases are correct. That they they're testing all the requirements to make sure all the requirements are fulfilled and that you have full test coverage. So that's number one. And then you're you're testing the source code. And there's lots of ways to do this. You want to make sure the source with the right answer, but also you've got sanitizers, very static analyzers, this sort of thing. But finally, we're also testing the deliverable object code in DO178B. We do not trust compilers.
Just because the source code is correct does not mean that the compiled code is correct. And as it happens, uh, SQLite in its history has found bugs in each of GCC, clang, and MSVC. And you can still go look in the source code and find bug workarounds. I mean, all the bugs have been fixed now in the newest versions, but people still use older versions of the compiler. So, we have workarounds for these bugs. And you can find them in the SQLite source code.
Uh, so how do how do we actually test the product? It is a C library. So you know normally an application would would talk to the C library and the C library then talks to your file system to get the answer. Um it but escalates on the
monolith we've got these plug-in there there's various ways of there's various subcomponents that we can use and control. So when when the uh when we
write a test program, it's just like another application, right? I mean, it it just talks to the library because we're testing it exactly the way we we we fly. And what the test program can do
is we have special interfaces that we can go in and we can substitute variations on uh these uh these
pluggable interfaces that allow us to do interesting things. For example, we've got this module called the VFS, which is what we use to talk to the file to the operating system, and we can plug in alternative VFS's that do that inject inject errors. Uh we we and
in the VFS's that we that we do this with uh the ones we write, the the
operating system interfaces are called by pointer. In other words, we have we don't call the operating system interfaces directly. We have a table of pointers to the operating system entry point which and we have a published API that we can change those pointers. I'll show you this. This is actual code. We we have a a table of of of pointers to the APIs. And so if we need to uh you know how do you make the how do you make the open system call fail in a predictable way that's that's hard to do
but what we can do we can test the product by substituting an alternative pointer there using a well-known API and
we've got a wonky version of open that will raise an error for us in a predictable way and then we can verify that the product deals with that error correctly. Uh, out of memory testing is a big deal. I know a lot of y'all program on Linux and Linux processes never Malik never fails. No, no, seriously, it doesn't because if you allocate too much memory, it it just kills some other random process on your machine and steals its memory, right? I mean, that's the way things go now. And uh even like uh correct me if I'm wrong, but I think uh most you know if you're programming in Rust and you run out of memory, it just kills the whole process off, right? So uh but but SQLite runs on things like your watch and stuff where running out of memory is a bad thing and they don't have much memory to begin with. So we have to have to deal with the case of what happens if Malik fails. What happens when you run out of memory and we want to unwind the stack gracefully, return an error and not leak memory in the process. So we do that by we can you can uh substitute an alternative memory allocator. You don't have to the the default is to just use system malic, right? But you can substitute an alternative at at at start time. And we
do that and we rig this all we we substitute a rigged alternative that will deliberately fail memory allocations and we do this in a loop. We have a test test module and uh some some
code we want to test and we do this in a loop. We said fail on the first allocation. Okay, make sure you you get a message and you fail on the second allocation and so forth and you keep doing this until you get all the way through the test without ever failing. In other words, you've done all the mal allocations. uh and and in this way we find a lot of errors in out of memory paths. Same thing with IO testing, we can we can plug in alternative uh IO mechanisms and or
instead of using the real IO that's provided by your operating system, we can we can intercept those calls and inject errors and we put it in a loop and we inject errors at various ports parts of the test and and make sure that they're all captured and and handled appropriately. So I don't know if you remember one of my early slides, one of the requirements of Esculite is that you know power safe acid transactions. That term power safe I made that up really when an acid transaction is supposed to be power safe. By powers safe, I'm emphasizing the fact that you can be in the middle of a transaction, part of it's been written to the file system, part of it still in memory pending the right and uh you lose power or the
operating system crashes or whatever and it doesn't all get into disk. the the the idea the spec in SQLite is that even after you power back up either the entire transaction made it to disk and is complete or the whole thing is rolled back which is a very important property for databases. How do you test that? I know in in uh Certino, California, Apple has a I don't know if it's a room
or a building just full of machines and they're hooked hooked to hooked up to uh switches that automatically kill their power every now and then and reboot them looking for this kind of error. I mean, and if you have millions and millions of dollars to devote to thography, you can do stuff like that. I I don't have that kind of money. So, how do how do we do that? Um, same idea. Uh, with crash testing, we we can plug in an alternative backend, a fake
file system that records all of the file
operations. And it takes snapshots of this file system after every system call. And then it pulls these snapshots
and it corrupts the file system in ways that might happen if you lost power. It's assuming out of order, right? So it it puts the rights in a random order and forgets half of them or some of them. Uh it it does things out of order and and this scrambles things around. Does this over and over again and make sure that the transaction either made it to disk completely and correctly or that was completely rolled back.
That's a lot of testing. All of this goes into um oh here's the diagram of of
how we do that. So all of this goes into uh what I was going to say is that when you're writing tests, it's not sufficient. You you you don't have a complete product and then you write tests for your completed product. The product has to be designed to be testable from the get-go. And and so when I was developing teach
or when I was developing the test for SQLite, I had to go in and change SQLite itself radically. And one of the interfaces I had to add was SQLite 3 test control. This is a published interface. It's available in every version of SQLite that that you use in your application. Probably you're not using it. You shouldn't be using it. In fact, the documentation for this says this is for testing purposes only. You ought not be using it. but it's included in the builds because otherwise we wouldn't be testing what we're flying. So, uh it does things like turn query optimizations on and off or just query parameters. Uh I I'll talk about the fault sim thing which is really cool in just a moment.
But but there's a a a pseudo random number generator in SQLite that it uses. You can use this interface for example to seed it to a known value so that your tests are reproducible. Uh there there are some some modules within SQLite that are just inherently difficult to test. They have their own unit tests and they're built in and we can run those unit tests using test control and and much much more. Here's there's
this uh internal function in SQLite. the
the the design rule is that
functions that begin with SQLite 3 underscore are published APIs and those that begin with SQLite 3 and don't have an underscore after them are internal use only and subject to change. So this is internal use only and this function
fault sim or fault you know for fault simulation in production it always returns false it
should never be true in a production build but using the test control API we
can make it return non zero and we can do that we can we can make it return whatever value we want and it doesn't have to do this every time it can just do that sometimes like on every inth call or something like that. And and so here's an example of where it's actually used. I mean, we need to make sure that SQLite doesn't commonly produce kickoff threads. And in fact, it never does unless you ask it to. But what how do you how do you how would you test that the error recovery for Pthread
create not working? How how would you do this otherwise? So here we we you know we we set a fault simulation that causes it to pretend that pthread create was unable to create a thread and how are you going to recover from that? There are 24 instances of that uh in the latest code uh which you're welcome to go check out for yourself. So um
just a little bit of background um some of you might be familiar with the TCL language. Does anybody know TCL? Had experience with that? It's a really great language. Uh John wrote it about 1988. Uh this is uh I reading his code really taught me to program in C. I mean I I knew C before. You can get some of my code from before I read his code and afterwards and it's it's night and day. It's a totally different world. Um [snorts] I really appreciate that and I was I was actually part of the tickle core team for a while. Um, but anyway,
uh, what you might not know is SQLite really started out as a a TCL extension. Excuse me. The, you know, it used to be called Tickle and then there was this thing called Me Too and it was determined that Tickle was too risque and so now they've rebranded as TCL. So there's that. I'm I'm still I'm old habits die hard. But anyway, uh, SQLite
was a TCL extension that escaped into the wild. It really started out as a TCL. That was that was its whole purpose. So naturally, all of the testing in in SQLite is done in in TCL, at least the initial testing for the first few years. That's all the tests we had uh currently and they're still there. If you if you pull the source code and you type make test, it's going to run the TCL tests. And if you do a make test, I think it's well I I checked the other day and it's 14 and a half million different tests. Now there there's really only like 20,000 cases, but a lot of them are in loops with the parameters varying. So it it comes out to 14 and a half million to run the whole thing. Um it seems like it was it's in my backup slides. I think it was
12 CPU hours or something like that. It's part of the source tree. Six and a half times larger than SQLite itself is the code to do all of this. But it's a custom build. In other words, in order to use the TCL tests, you have to give SQLite some special compile time options that you would not normally give it in production. It's it's really a source code test. It's not an object code test because it's not testing exactly the same code that you're delivering. And we wanted because of DO178B, we wanted to test exactly what was being delivered. And so I had this idea of writing a new test harness uh which we call TH3. Yes, there was a TH1 and TH2.
They didn't work out. Uh and I started that in 2008 and it achieved 100% MCDC in 2009.
And that was that was a really hard year. uh a long year of 80our days and
thinking that why why am I why am I tormenting myself this way? But really
it was very interesting once once we got to 100% NCDC those bugs that we were getting from Android and elsewhere they just kind of stopped. They really did. I was I was shocked. I didn't I I didn't really believe about stepped off the edge. I [snorts] didn't believe that this was really going to happen, but it it worked very very well. And then for several years after that, we we really didn't have any bugs
uh coming into SQLite. We'd achieved bug-free code, or so I thought.
Yeah, that those were happy days. We could we could focus we could focus on um uh making the product better. But understand that TH3 was not a oneandone. Uh when you think, oh well, I've I've got I've got my 100% MCDZ testing now. I'm done. I'm finished with that. No, it's an ongoing maintenance thing. This is a snapshot I just did this morning of all the number of check-ins to TH3, non-merge check-ins to TH3. And as you see, there was a lot of work in the early two years, but I mean, it's ongoing even to this day. There's a lot of maintenance work. Every time we make a change, every time we add a new feature, every time a new bug is found, we have to update TH3 as well. So there is a maintenance burden involved in this, but it but it pays good dividends.
Now, so 100% MCDC testing is not the
only kind of testing we do. You know, I I'm going to say I'm going to make the bold claim that your code comments, it's part of your testing. Code comments are very important. This is I I hear so many people saying, "Oh, you know, if you write your code right, you don't need to put any comments because it's self-explanatory." And I don't think that is correct. I I believe very strongly that you should have human readable, human language comments on all of your functions and procedures and variables, concise, not boilerplate, not boilerplate comments. Don't fall in that trap of you've got a a particular format. And this is so that people not yet born can understand what you've written. also so that you at least in my case so that I can understand why I did something
a week after I'd written it but also because as humans we we have different neural pathways for writing formal language writing code and and and spoken language and and you may have experienced this you may have some experience with this who's ever experienced something like this where you're you're working on some problem hey Carl look at this problem with me I've been working on this for hours clearly cannot be zero because why? Wait, wait. No. Oh, I got it now. Never mind. Thank you, Carl. You've all experienced this, haven't you? And it's because it's because, you know, you were
you were thinking in a formal language. You weren't writing the code and you were not using human language and you're using different neural neural pathways. And this works the other way as well. If if you're thinking in in human language, you're in meetings designing something or you're prompting your AI or whatever it is you're doing in the human language, uh, and and you think, "Oh, this is a great design. This is going to work out great." And then you sit down to write the code and you immediately realize, "Oh, that's never going to work." It works both ways. You need to do both. Use your whole brain.
It's a very important feature. about onethird of the SQLite source code is comment and that's it's not use it's not useless it's very important for be for it being there uh we use heavy use of assert and this was a controversial thing I never thought assert would be so controversial uh apparently in fact we had a big fight with go about this uh so I so the the idea of assert of
course is that it's an invariant if it ever an assert ever fails that's a bug in your program. Okay. But it turns out that a and and think of assert as an executable comment. You say I'm reading
code and I see a comment and I see some code, I'm not going to assume that the comment in the code agree. But if I see an assert and I know this thing is passing tests, then I then I believe the assert. So um but the developers of Go
they were thinking look we've seen a lot of people and they use assert to validate inputs and that's wrong and so we're going to forbid assert from being in Go and and because of that I had some um sharp criticism of Go on my website and
and the Go people took umbrage at this and they contacted me and we had a meeting and they agreed to tone down
their rhetoric on assert and I agreed to tone down my rhetoric against go and and and and that worked out great and everybody everybody would came away happy. Essite makes heavy use of assert. We really lean on it heavily. There's over 7,500 of them in the code right now. Uh and that's a very important factor. In fact, assert normally is it's on by default. We've done some if deferee magic to make it off by default because when asserts are on the code runs four times slower and and nobody wants that. You have to you have to compile with escalate debug in order to enable asserts.
And it's more than just simple assert statements. We have entire sub routines or procedures uh for checking invariance that are omitted from production builds. Uh so you know here's something in the page cache. We're going to we're checking all of our invariance for uh an object out of the page cache. And this runs when you do a debugging build and so that we know that our page cache doesn't have some subtle uh problems that aren't even showing up in our tests.
uh we have assert like macros never and
always that we can put around test cases and in
production code never is a noop and always is a no-op they just go straight through but but they identify branches that we believe are or in this case never identifies a branch or a condition that is always false. So this we we never expect that branch to be taken. And always of course is the inverse of that. Um and this is a very powerful way of of doing error checking. It's it's think of it like u if you're if you're writing uh zig or uh fill c or something and you want to do bounds checking on an array or something you're going to you're going to insert branches that should never be taken because you're if there's
no bugs in your program then you're never going to read outside the array. Right? So never and always are that way. And we do have never and always macros that we we make use of. And whether or not they the way they function depends on how you're testing. If you're doing if you're measuring coverage, uh never always is just a a hardcoded to one and and never is hardcoded to zero. If you're debugging, they raise they panic if you if if if
the condition is wrong. But for production, they just pass through. The idea is that maybe maybe you've missed something. Maybe there's some bug and maybe the never or the always is going to save you.
So there's there's a lot of these macros. There's test case, always, never, and assert. We use them all over the place. Um, and their behavior
depends on what you're doing. If you're if you're doing the if you're testing your test cases, they behave one way. If you're testing source code, they behave another way. And then in production, they behave a third way. This also, I
think, conflicts with a core go doctrine that your code should only ever compile in a single way. But I I don't know of any other way to do the the kind of testing that that we do at SQLite. Now we talked about MC uh
uh MC uh DC testing. You can go even further and do mutation testing.
By mutation testing I mean you make a small change to the source code or the object code and then you verify that your test break. Uh this is a very powerful thing but it's it and understand it's not testing that your program is correct. is testing that your test procedures are are really checking your program really hard. Um it it's it's problematic though because here this is the actual hash function used for symbols inside of SQLite and what if the branch instruction that implements that not equal to operator what if it's always an unconditional jump then the hash function always returns zero. Okay. Uh that still works.
It might not be quite as fast, but it still works. So, how how do you write a test case to verify that that branch is doing the right thing? That's a that's a hard thing to get right. Um, in general, there's a lot of places in SQLite where we're checking some condition that's very common and then we have some very fast algorithm that we can do in that condition. But sometimes we need to fall out to a more general case that's a lot slower. If if we take a false positive, if we take a false negative on and shortcut works, we're going to do the slower case. It's still going to get the correct answer, but we can't really uh we're going to
miss that the fact that that the code was corrupted. So we I I've never I haven't yet figured out how to do a reliable uh mutation testing requirement. I can get code to be 100% MCDC. I can't get code to be 100%
mutation testing uh reliably. We do a bunch of other testing with uh test on different platforms, different compilers, different architectures. Notice we a big Indian versus little Indian. Back when I first started this big Indian processors were still relatively common. You these days you can't buy them really. I have a I I went on um
e eBay and bought a circa 19 or 2004
MacBook so that I could test on a power PC. Um, and and finally, we disable optimizations. There's a there's a bazillion tests. We keep track of it all with checklists. We make heavy use of checklists because you can't remember it all. And so, we spend all of our time testing. You know, some a few years ago, I was talking to a young young uh programmer right out of school, young woman, and and she she knew who I was,
and she and I asked her, "What' you do?" And and she she felt really bad. And she said, "I'm just a tester." And I thought for a second, you know, that that that's really all I do. [laughter] Um and and and really the you know the anthropic thinks that's what everybody should you you write the tests and and and Claude writes the code. That's their that's their goal, right? I'm I'm not sure if that's that's right or not. Um but there are un un
I came for do for doing all these testing I came across a lot of benefits that I did not expect. I just wanted to get rid of the bugs. But there are a lot of other benefits that come into this where one we can maintain this code base which the world depends on with just three committers. Uh how many committers are there in Postgress? I don't know a 100red 200. There's a bunch. Uh name are there any
other projects that have three committers that are this this big? I don't think so. And the only way we can do that is that we we have this coverage. We can refactor big punk sections of the code to strip out entire subsystems and rewrite them from scratch and do that in a point release with high confidence that we haven't broken anything. That's very important. Uh it's uh makes it a lot easier to find and fix bugs and it makes the code faster.
Honest engine. What uh this here's a
graph of actual performance measurements. uh billions of x64 CPU
cycles needed to run a a particular performance benchmark starting in 20 uh 2009 when we first got 100% MCD because because we have this high level of test coverage we can make small micro optimizations no one optimization is even measurable but you put in thousands and thousands of them and we have tripled the performance of the of the product over over the course of years. It now runs three times faster than it did in 2009. More than three thou three more than three times faster. You know, recently I've seen on
uh social media a fallacy or an unspoken
assumption that source code is distinct from test code, that these are two different things. And I've been seeing that when people there's a big argument that they because somebody rewrote they used the test cases for Postgress to rewrite Postgress and Rust. Well, their agent did it. They just used the test cases and and and and all the arguments back and forth on either side were we're assuming that the test cases and the code were distinct. But but I've showed you that with at least in the case of SQLite, there's a significant overlap between the test cases and the code. uh even about 15 to 20% you know in the
in the chip design community and in and integrated circuits about 15 to 20% of the transistors on your new CPU are um only used during
manufacturing testing. They're not they're not used after the device is is
uh packaged and put in your computer. So 15 to 20% of your code maybe that maybe that should be your target for how much of your code is actually used for testing and it seems like dead weight but no it's it's there because to make it faster I they're smaller once you take out the source code testing part but still it's there and as I was writing this I you know if we scale this closer to the way SQLite does it we see that the test code is significantly larger than the source code and I was thinking so many of these People are telling me that I need to be writing um just write the tests and then get an agent to write the code and this will vastly improve my productivity.
And maybe I'm being dense, but I'm not seeing how that's really going to be that helpful. Somebody can maybe help me understand better why that works. Okay. So, everything's going along peachy for a while, but then we came across um profileg guided fuz





