| 1 |
<!DOCTYPE html> |
| 2 |
<html><head> |
| 3 |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 4 |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> |
| 5 |
<link href="sqlite.css" rel="stylesheet"> |
| 6 |
<title>Why Is SQLite Coded In C</title> |
| 7 |
<!-- path= --> |
| 8 |
</head> |
| 9 |
<body> |
| 10 |
<div class=nosearch> |
| 11 |
<a href="index.html"> |
| 12 |
<img class="logo" src="images/sqlite370_banner.svg" alt="SQLite" border="0"> |
| 13 |
</a> |
| 14 |
<div><!-- IE hack to prevent disappearing logo --></div> |
| 15 |
<div class="tagline desktoponly"> |
| 16 |
Small. Fast. Reliable.<br>Choose any three. |
| 17 |
</div> |
| 18 |
<div class="menu mainmenu"> |
| 19 |
<ul> |
| 20 |
<li><a href="index.html">Home</a> |
| 21 |
<li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a> |
| 22 |
<li class='wideonly'><a href='about.html'>About</a> |
| 23 |
<li class='desktoponly'><a href="docs.html">Documentation</a> |
| 24 |
<li class='desktoponly'><a href="download.html">Download</a> |
| 25 |
<li class='wideonly'><a href='copyright.html'>License</a> |
| 26 |
<li class='desktoponly'><a href="support.html">Support</a> |
| 27 |
<li class='desktoponly'><a href="prosupport.html">Purchase</a> |
| 28 |
<li class='search' id='search_menubutton'> |
| 29 |
<a href="javascript:void(0)" onclick='toggle_search()'>Search</a> |
| 30 |
</ul> |
| 31 |
</div> |
| 32 |
<div class="menu submenu" id="submenu"> |
| 33 |
<ul> |
| 34 |
<li><a href='about.html'>About</a> |
| 35 |
<li><a href='docs.html'>Documentation</a> |
| 36 |
<li><a href='download.html'>Download</a> |
| 37 |
<li><a href='support.html'>Support</a> |
| 38 |
<li><a href='prosupport.html'>Purchase</a> |
| 39 |
</ul> |
| 40 |
</div> |
| 41 |
<div class="searchmenu" id="searchmenu"> |
| 42 |
<form method="GET" action="search"> |
| 43 |
<select name="s" id="searchtype"> |
| 44 |
<option value="d">Search Documentation</option> |
| 45 |
<option value="c">Search Changelog</option> |
| 46 |
</select> |
| 47 |
<input type="text" name="q" id="searchbox" value=""> |
| 48 |
<input type="submit" value="Go"> |
| 49 |
</form> |
| 50 |
</div> |
| 51 |
</div> |
| 52 |
<script> |
| 53 |
function toggle_div(nm) { |
| 54 |
var w = document.getElementById(nm); |
| 55 |
if( w.style.display=="block" ){ |
| 56 |
w.style.display = "none"; |
| 57 |
}else{ |
| 58 |
w.style.display = "block"; |
| 59 |
} |
| 60 |
} |
| 61 |
function toggle_search() { |
| 62 |
var w = document.getElementById("searchmenu"); |
| 63 |
if( w.style.display=="block" ){ |
| 64 |
w.style.display = "none"; |
| 65 |
} else { |
| 66 |
w.style.display = "block"; |
| 67 |
setTimeout(function(){ |
| 68 |
document.getElementById("searchbox").focus() |
| 69 |
}, 30); |
| 70 |
} |
| 71 |
} |
| 72 |
function div_off(nm){document.getElementById(nm).style.display="none";} |
| 73 |
window.onbeforeunload = function(e){div_off("submenu");} |
| 74 |
/* Disable the Search feature if we are not operating from CGI, since */ |
| 75 |
/* Search is accomplished using CGI and will not work without it. */ |
| 76 |
if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){ |
| 77 |
document.getElementById("search_menubutton").style.display = "none"; |
| 78 |
} |
| 79 |
/* Used by the Hide/Show button beside syntax diagrams, to toggle the */ |
| 80 |
function hideorshow(btn,obj){ |
| 81 |
var x = document.getElementById(obj); |
| 82 |
var b = document.getElementById(btn); |
| 83 |
if( x.style.display!='none' ){ |
| 84 |
x.style.display = 'none'; |
| 85 |
b.innerHTML='show'; |
| 86 |
}else{ |
| 87 |
x.style.display = ''; |
| 88 |
b.innerHTML='hide'; |
| 89 |
} |
| 90 |
return false; |
| 91 |
} |
| 92 |
var antiRobot = 0; |
| 93 |
function antiRobotGo(){ |
| 94 |
if( antiRobot!=3 ) return; |
| 95 |
antiRobot = 7; |
| 96 |
var j = document.getElementById("mtimelink"); |
| 97 |
if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href"); |
| 98 |
} |
| 99 |
function antiRobotDefense(){ |
| 100 |
document.body.onmousedown=function(){ |
| 101 |
antiRobot |= 2; |
| 102 |
antiRobotGo(); |
| 103 |
document.body.onmousedown=null; |
| 104 |
} |
| 105 |
document.body.onmousemove=function(){ |
| 106 |
antiRobot |= 2; |
| 107 |
antiRobotGo(); |
| 108 |
document.body.onmousemove=null; |
| 109 |
} |
| 110 |
setTimeout(function(){ |
| 111 |
antiRobot |= 1; |
| 112 |
antiRobotGo(); |
| 113 |
}, 100) |
| 114 |
antiRobotGo(); |
| 115 |
} |
| 116 |
antiRobotDefense(); |
| 117 |
</script> |
| 118 |
<div class=fancy> |
| 119 |
<div class=nosearch> |
| 120 |
<div class="fancy_title"> |
| 121 |
Why Is SQLite Coded In C |
| 122 |
</div> |
| 123 |
<details class="fancy_toc"> |
| 124 |
<summary>Table Of Contents</summary> |
| 125 |
<div id="toc_sub"><div class="fancy-toc1"><a href="#c_is_best">1. C Is Best</a></div> |
| 126 |
<div class="fancy-toc2"><a href="#performance">1.1. Performance</a></div> |
| 127 |
<div class="fancy-toc2"><a href="#compatibility">1.2. Compatibility</a></div> |
| 128 |
<div class="fancy-toc2"><a href="#low_dependency">1.3. Low-Dependency</a></div> |
| 129 |
<div class="fancy-toc2"><a href="#stability">1.4. Stability</a></div> |
| 130 |
<div class="fancy-toc1"><a href="#why_isn_t_sqlite_coded_in_an_object_oriented_language_">2. Why Isn't SQLite Coded In An Object-Oriented Language?</a></div> |
| 131 |
<div class="fancy-toc1"><a href="#why_isn_t_sqlite_coded_in_a_safe_language_">3. Why Isn't SQLite Coded In A "Safe" Language?</a></div> |
| 132 |
</div> |
| 133 |
</details> |
| 134 |
</div> |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
<h1 id="c_is_best"><span>1. </span>C Is Best</h1> |
| 140 |
|
| 141 |
<blockquote><table border="1"><tr><td> |
| 142 |
Note: Sections 2.0 and 3.0 of this article were added in response |
| 143 |
to comments on |
| 144 |
<a href="https://news.ycombinator.com/item?id=16585120">Hacker News</a> and |
| 145 |
<a href="https://www.reddit.com/r/programming/comments/84fzoc/why_is_sqlite_coded_in_c/">Reddit</a>. |
| 146 |
</td></tr></table></blockquote> |
| 147 |
|
| 148 |
<p> |
| 149 |
Since its inception on 2000-05-29, SQLite has been implemented in generic C. |
| 150 |
C was and continues to be the best language for implementing a software |
| 151 |
library like SQLite. There are no plans to recode SQLite in any other |
| 152 |
programming language at this time. |
| 153 |
|
| 154 |
</p><p> |
| 155 |
The reasons why C is the best language to implement SQLite include: |
| 156 |
|
| 157 |
|
| 158 |
</p><ul> |
| 159 |
<li> Performance |
| 160 |
</li><li> Compatibility |
| 161 |
</li><li> Low-dependency |
| 162 |
</li><li> Stability |
| 163 |
</li></ul> |
| 164 |
|
| 165 |
<h2 id="performance"><span>1.1. </span>Performance</h2> |
| 166 |
|
| 167 |
<p>An intensively used low-level library like SQLite needs to be fast. |
| 168 |
(And SQLite is fast, see <a href="intern-v-extern-blob.html">Internal Versus External BLOBs</a> and |
| 169 |
<a href="fasterthanfs.html">35% Faster Than The Filesystem</a> for examples.) |
| 170 |
|
| 171 |
</p><p>C is a great language for writing fast code. C is sometimes |
| 172 |
described as "portable assembly language". It enables developers |
| 173 |
to code as close to the underlying hardware as possible while still |
| 174 |
remaining portable across platforms. |
| 175 |
|
| 176 |
</p><p>Other programming languages sometimes claim to be "as fast as C". |
| 177 |
But no other language claims to be faster than C for general-purpose |
| 178 |
programming, because none are. |
| 179 |
|
| 180 |
</p><h2 id="compatibility"><span>1.2. </span>Compatibility</h2> |
| 181 |
|
| 182 |
<p>Nearly all systems have the ability to call libraries |
| 183 |
written in C. This is not true of other implementation languages. |
| 184 |
|
| 185 |
</p><p>So, for example, Android applications written in Java are able to |
| 186 |
invoke SQLite (through an adaptor). Maybe it would have been more |
| 187 |
convenient for Android if SQLite had been coded in Java as that would |
| 188 |
make the interface simpler. However, on iPhone applications are coded |
| 189 |
in Objective-C or Swift, neither of which have the ability to call |
| 190 |
libraries written in Java. Thus, SQLite would be unusable on iPhones |
| 191 |
had it been written in Java. |
| 192 |
|
| 193 |
</p><h2 id="low_dependency"><span>1.3. </span>Low-Dependency</h2> |
| 194 |
|
| 195 |
<p>Libraries written in C do not have a huge run-time dependency. |
| 196 |
In its minimum configuration, SQLite requires only the following |
| 197 |
routines from the standard C library: |
| 198 |
|
| 199 |
</p><center> |
| 200 |
<table border="0"> |
| 201 |
<tr> |
| 202 |
<td valign="top"> |
| 203 |
<ul> |
| 204 |
<li> memcmp() |
| 205 |
</li><li> memcpy() |
| 206 |
</li><li> memmove() |
| 207 |
</li><li> memset() |
| 208 |
</li></ul> |
| 209 |
</td> |
| 210 |
<td> </td> |
| 211 |
<td valign="top"> |
| 212 |
<ul> |
| 213 |
<li> strcmp() |
| 214 |
</li><li> strlen() |
| 215 |
</li><li> strncmp() |
| 216 |
</li></ul> |
| 217 |
</td> |
| 218 |
</tr> |
| 219 |
</table> |
| 220 |
</center> |
| 221 |
|
| 222 |
<p> |
| 223 |
In a more complete build, SQLite also uses library routines like |
| 224 |
malloc() and free() and operating system interfaces for opening, reading, |
| 225 |
writing, and closing files. But even then, the number of dependencies |
| 226 |
is very small. Other "modern" languages, in contrast, often require |
| 227 |
multi-megabyte runtimes loaded with thousands and thousands of interfaces. |
| 228 |
|
| 229 |
</p><h2 id="stability"><span>1.4. </span>Stability</h2> |
| 230 |
|
| 231 |
<p> |
| 232 |
The C language is old and boring. |
| 233 |
It is a well-known and well-understood language. |
| 234 |
This is exactly what one wants when developing a module like SQLite. |
| 235 |
Writing a small, fast, and reliable database engine is hard enough as it |
| 236 |
is without the implementation language changing out from under you with |
| 237 |
each update to the implementation language specification. |
| 238 |
|
| 239 |
</p><h1 id="why_isn_t_sqlite_coded_in_an_object_oriented_language_"><span>2. </span>Why Isn't SQLite Coded In An Object-Oriented Language?</h1> |
| 240 |
|
| 241 |
<p> |
| 242 |
Some programmers cannot imagine developing a complex system like |
| 243 |
SQLite in a language that is not "object oriented". So why is |
| 244 |
SQLite not coded in C++ or Java? |
| 245 |
|
| 246 |
</p><ol> |
| 247 |
<li><p> |
| 248 |
Libraries written in C++ or Java can generally only be used by |
| 249 |
applications written in the same language. It is difficult to |
| 250 |
get an application written in Haskell or Java to invoke a library |
| 251 |
written in C++. On the other hand, libraries written in C are |
| 252 |
callable from any programming language. |
| 253 |
|
| 254 |
</p></li><li><p> |
| 255 |
Object-Oriented is a design pattern, not a programming language. |
| 256 |
You can do object-oriented programming in any language you want, |
| 257 |
including assembly language. Some languages (ex: C++ or Java) make |
| 258 |
object-oriented easier. But you can still do object-oriented programming |
| 259 |
in languages like C. |
| 260 |
|
| 261 |
</p></li><li><p> |
| 262 |
Object-oriented is not the only valid design pattern. |
| 263 |
Many programmers have been taught to think purely in terms of |
| 264 |
objects. And, to be fair, objects are often a good way to |
| 265 |
decompose a problem. But objects are not the only way, and are |
| 266 |
not always the best way to decompose a problem. Sometimes good old |
| 267 |
procedural code is easier to write, easier to maintain and understand, |
| 268 |
and faster than object-oriented code. |
| 269 |
|
| 270 |
</p></li><li><p> |
| 271 |
When SQLite was first being developed, Java was a young and immature |
| 272 |
language. C++ was older, but was undergoing such growing pains that |
| 273 |
it was difficult to find any two C++ compilers that worked the same |
| 274 |
way. So C was definitely a better choice back when SQLite was first |
| 275 |
being developed. The situation is less stark now, but there is little |
| 276 |
to no benefit in recoding SQLite at this point. |
| 277 |
</p></li></ol> |
| 278 |
|
| 279 |
<h1 id="why_isn_t_sqlite_coded_in_a_safe_language_"><span>3. </span>Why Isn't SQLite Coded In A "Safe" Language?</h1> |
| 280 |
|
| 281 |
<p> |
| 282 |
There has lately been a lot of interest in "safe" programming languages |
| 283 |
like Rust or Go in which it is impossible, or is at least difficult, to make |
| 284 |
common programming errors like memory leaks or array overruns. So the |
| 285 |
question often arises as to why SQLite is not coded in a "safe" language. |
| 286 |
|
| 287 |
</p><ol> |
| 288 |
<li><p> |
| 289 |
None of the safe programming languages existed for the first 10 years |
| 290 |
of SQLite's existence. SQLite could be recoded in Go or Rust, but doing |
| 291 |
so would probably introduce far more bugs than would be fixed, and it |
| 292 |
may also result in slower code. |
| 293 |
|
| 294 |
</p></li><li><p> |
| 295 |
Safe languages insert additional machine branches to do things like |
| 296 |
verify that array accesses are in-bounds. In correct code, those |
| 297 |
branches are never taken. That means that the machine code cannot |
| 298 |
be 100% branch tested, which is an important component of SQLite's |
| 299 |
quality strategy. |
| 300 |
|
| 301 |
</p></li><li><p> |
| 302 |
Safe languages usually want to abort if they encounter an out-of-memory |
| 303 |
(OOM) situation. SQLite is designed to recover gracefully from an OOM. |
| 304 |
It is unclear how this could be accomplished in the current crop of |
| 305 |
safe languages. |
| 306 |
|
| 307 |
</p></li><li><p> |
| 308 |
All of the existing safe languages are new. The developers of SQLite |
| 309 |
applaud the efforts of computer language researchers in trying to |
| 310 |
develop languages that are easier to program safely. We encourage these |
| 311 |
efforts to continue. But we ourselves are more interested in old and |
| 312 |
boring languages when it comes to implementing SQLite. |
| 313 |
</p></li></ol> |
| 314 |
|
| 315 |
<p> |
| 316 |
All that said, it is possible that SQLite might |
| 317 |
one day be recoded in Rust. Recoding SQLite in Go is unlikely |
| 318 |
since Go hates assert(). But Rust is a possibility. Some |
| 319 |
preconditions that must occur before SQLite is recoded in Rust |
| 320 |
include: |
| 321 |
|
| 322 |
</p><p> |
| 323 |
</p><ol type="A"> |
| 324 |
<li> Rust needs to mature a little more, stop changing so fast, and |
| 325 |
move further toward being old and boring. |
| 326 |
</li><li> Rust needs to demonstrate that it can be used to create general-purpose |
| 327 |
libraries that are callable from all other programming languages. |
| 328 |
</li><li> Rust needs to demonstrate that it can produce object code that |
| 329 |
works on obscure embedded devices, including devices that lack |
| 330 |
an operating system. |
| 331 |
</li><li> Rust needs to pick up the necessary tooling that enables one to |
| 332 |
do 100% branch coverage testing of the compiled binaries. |
| 333 |
</li><li> Rust needs a mechanism to recover gracefully from OOM errors. |
| 334 |
</li><li> Rust needs to demonstrate that it can do the kinds of work that |
| 335 |
C does in SQLite without a significant speed penalty. |
| 336 |
</li></ol> |
| 337 |
|
| 338 |
<p> |
| 339 |
If you are a "rustacean" and feel that Rust already meets the |
| 340 |
preconditions listed above, and that SQLite should be recoded in |
| 341 |
Rust, then you are welcomed and encouraged |
| 342 |
to contact the SQLite developers privately |
| 343 |
and argue your case. |
| 344 |
</p><p align="center"><small><i>This page was last updated on 2025-05-09 15:56:17Z </small></i></p> |
| 345 |
|