Debian Codegraph @

www/whyc.html

raw
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.gif" 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 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#c_is_best">1. C Is Best</a></div>
129 <div class="fancy-toc2"><a href="#performance">1.1. Performance</a></div>
130 <div class="fancy-toc2"><a href="#compatibility">1.2. Compatibility</a></div>
131 <div class="fancy-toc2"><a href="#low_dependency">1.3. Low-Dependency</a></div>
132 <div class="fancy-toc2"><a href="#stability">1.4. Stability</a></div>
133 <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>
134 <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>
135 </div>
136 </div>
137 <script>
138 function toggle_toc(){
139 var sub = document.getElementById("toc_sub")
140 var mk = document.getElementById("toc_mk")
141 if( sub.style.display!="block" ){
142 sub.style.display = "block";
143 mk.innerHTML = "&#x25bc;";
144 } else {
145 sub.style.display = "none";
146 mk.innerHTML = "&#x25ba;";
147 }
148 }
149 </script>
150 </div>
151
152
153
154
155 <h1 id="c_is_best"><span>1. </span>C Is Best</h1>
156
157 <blockquote><table border="1"><tr><td>
158 Note: Sections 2.0 and 3.0 of this article were added in response
159 to comments on
160 <a href="https://news.ycombinator.com/item?id=16585120">Hacker News</a> and
161 <a href="https://www.reddit.com/r/programming/comments/84fzoc/why_is_sqlite_coded_in_c/">Reddit</a>.
162 </td></tr></table></blockquote>
163
164 <p>
165 Since its inception on 2000-05-29, SQLite has been implemented in generic C.
166 C was and continues to be the best language for implementing a software
167 library like SQLite. There are no plans to recode SQLite in any other
168 programming language at this time.
169
170 </p><p>
171 The reasons why C is the best language to implement SQLite include:
172
173
174 </p><ul>
175 <li> Performance
176 </li><li> Compatibility
177 </li><li> Low-dependency
178 </li><li> Stability
179 </li></ul>
180
181 <h2 id="performance"><span>1.1. </span>Performance</h2>
182
183 <p>An intensively used low-level library like SQLite needs to be fast.
184 (And SQLite is fast, see <a href="intern-v-extern-blob.html">Internal Versus External BLOBs</a> and
185 <a href="fasterthanfs.html">35% Faster Than The Filesystem</a> for example.)
186
187 </p><p>C is a great language for writing fast code. C is sometimes
188 described as "portable assembly language". It enables to developers
189 to code as close to the underlying hardware as possible while still
190 remaining portable across platforms.
191
192 </p><p>Other programming languages sometimes claim to be "as fast as C".
193 But no other language claims to be faster than C for general-purpose
194 programming, because none are.
195
196 </p><h2 id="compatibility"><span>1.2. </span>Compatibility</h2>
197
198 <p>Nearly all systems have the ability to call libraries
199 written in C. This is not true of other implementation languages.
200
201 </p><p>So, for example, Android applications written in Java are able to
202 invoke SQLite (through an adaptor). Maybe it would have been more
203 convenient for Android if SQLite had been coded in Java as that would
204 make the interface simpler. However, on iPhone applications are coded
205 in Objective-C or Swift, neither of which have the ability to call
206 libraries written in Java. Thus, SQLite would be unusable on iPhones
207 had it been written in Java.
208
209 </p><h2 id="low_dependency"><span>1.3. </span>Low-Dependency</h2>
210
211 <p>Libraries written in C do not have a huge run-time dependency.
212 In its minimum configuration, SQLite requires only the following
213 routines from the standard C library:
214
215 </p><center>
216 <table border="0">
217 <tr>
218 <td valign="top">
219 <ul>
220 <li> memcmp()
221 </li><li> memcpy()
222 </li><li> memmove()
223 </li><li> memset()
224 </li></ul>
225 </td>
226 <td>&nbsp;&nbsp;&nbsp;</td>
227 <td valign="top">
228 <ul>
229 <li> strcmp()
230 </li><li> strlen()
231 </li><li> strncmp()
232 </li></ul>
233 </td>
234 </tr>
235 </table>
236 </center>
237
238 <p>
239 In a more complete build, SQLite also uses library routines like
240 malloc() and free() and operating system interfaces for opening, reading,
241 writing, and closing files. But even then, the number of dependencies
242 is very small. Other "modern" language, in contrast, often require
243 multi-megabyte runtimes loaded with thousands and thousands of interfaces.
244
245 </p><h2 id="stability"><span>1.4. </span>Stability</h2>
246
247 <p>
248 The C language is old and boring.
249 It is a well-known and well-understood language.
250 This is exactly what one wants when developing a module like SQLite.
251 Writing a small, fast, and reliable database engine is hard enough as it
252 is without the implementation language changing out from under you with
253 each update to the implementation language specification.
254
255 </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>
256
257 <p>
258 Some programmers cannot imagine developing a complex system like
259 SQLite in a language that is not "object oriented". So why is
260 SQLite not coded in C++ or Java?
261
262 </p><ol>
263 <li><p>
264 Libraries written in C++ or Java can generally only be used by
265 applications written in the same language. It is difficult to
266 get an application written in Haskell or Java to invoke a library
267 written in C++. On the other hand, libraries written in C are
268 callable from any programming language.
269
270 </p></li><li><p>
271 Object-Oriented is a design pattern, not a programming language.
272 You can do object-oriented programming in any language you want,
273 including assembly language. Some languages (ex: C++ or Java) make
274 object-oriented easier. But you can still do object-oriented programming
275 in languages like C.
276
277 </p></li><li><p>
278 Object-oriented is not the only valid design pattern.
279 Many programmers have been taught to think purely in terms of
280 objects. And, to be fair, objects are often a good way to
281 decompose a problem. But objects are not the only way, and are
282 not always the best way to decompose a problem. Sometimes good old
283 procedural code is easier to write, easier to maintain and understand,
284 and faster than object-oriented code.
285
286 </p></li><li><p>
287 When SQLite was first being developed, Java was a young and immature
288 language. C++ was older, but was undergoing such growing pains that
289 it was difficult to find any two C++ compilers that worked the same
290 way. So C was definitely a better choice back when SQLite was first
291 being developed. The situation is less stark now, but there is little
292 to no benefit in recoding SQLite at this point.
293 </p></li></ol>
294
295 <h1 id="why_isn_t_sqlite_coded_in_a_safe_language_"><span>3. </span>Why Isn't SQLite Coded In A "Safe" Language?</h1>
296
297 <p>
298 There has lately been a lot of interest in "safe" programming languages
299 like Rust or Go in which it is impossible, or is at least difficult, to make
300 common programming errors like memory leaks or array overruns. So the
301 question often arises as to why SQLite is not coded in a "safe" language.
302
303 </p><ol>
304 <li><p>
305 None of the safe programming languages existed for the first 10 years
306 of SQLite's existence. SQLite could be recoded in Go or Rust, but doing
307 so would probably introduce far more bugs than would be fixed, and it
308 seems also likely to result in slower code.
309
310 </p></li><li><p>
311 Safe languages insert additional machine branches to do things like
312 verify that array accesses are in-bounds. In correct code, those
313 branches are never taken. That means that the machine code cannot
314 be 100% branch tested, which is an important component of SQLite's
315 quality strategy.
316
317 </p></li><li><p>
318 Safe languages usually want to abort if they encounter an out-of-memory
319 (OOM) situation. SQLite is designed to recover gracefully from an OOM.
320 It is unclear how this could be accomplished in the current crop of
321 safe languages.
322
323 </p></li><li><p>
324 All of the existing safe languages are new. The developers of SQLite
325 applaud the efforts of computer language researchers in trying to
326 develop languages that are easier to program safely. We encourage these
327 efforts to continue. But we ourselves are more interested in old and
328 boring languages when it comes to implementing SQLite.
329 </p></li></ol>
330
331 <p>
332 All that said, it is possible that SQLite might
333 one day be recoded in Rust. Recoding SQLite in Go is unlikely
334 since Go hates assert(). But Rust is a possibility. Some
335 preconditions that must occur before SQLite is recoded in Rust
336 include:
337
338 </p><p>
339 </p><ol type="A">
340 <li> Rust needs to mature a little more, stop changing so fast, and
341 move further toward being old and boring.
342 </li><li> Rust needs to demonstrate that it can be used to create general-purpose
343 libraries that are callable from all other programming languages.
344 </li><li> Rust needs to demonstrate that it can produce object code that
345 works on obscure embedded devices, including devices that lack
346 an operating system.
347 </li><li> Rust needs to pick up the necessary tooling that enables one to
348 do 100% branch coverage testing of the compiled binaries.
349 </li><li> Rust needs a mechanism to recover gracefully from OOM errors.
350 </li><li> Rust needs to demonstrate that it can do the kinds of work that
351 C does in SQLite without a significant speed penalty.
352 </li></ol>
353
354 <p>
355 If you are a "rustacean" and feel that Rust already meets the
356 preconditions listed above, and that SQLite should be recoded in
357 Rust, then you are welcomed and encouraged
358 to contact the SQLite developers privately
359 and argue your case.
360 </p><p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/whyc.in?m=a3c9b502ed">2022-07-29 00:41:26</a> UTC </small></i></p>
361