3

I just noticed that at the beginning of the sqlite 3 source code file, it says:

/******************************************************************************
** This file is an amalgamation of many separate C source files from SQLite
** version 3.27.1.  By combining all the individual C code files into this
** single large file, the entire code can be compiled as a single translation
** unit.  This allows many compilers to do optimizations that would not be
** possible if the files were compiled separately.  Performance improvements
** of 5% or more are commonly seen when SQLite is compiled as a single
** translation unit.

I don't know this before and can't find an authoritative source to back it up. Is this true? What about C++?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Just a learner
  • 26,690
  • 50
  • 155
  • 234
  • 3
    It depends on too many factors to give a general, all purpose answer. – Govind Parmar Feb 21 '19 at 17:35
  • Where did you see that? Did you try asking the user who wrote it? – brunorey Feb 21 '19 at 17:37
  • 1
    If the combined code has all functions that should not be accessed from outside the file marked `static`, then the optimizer can do more work moving function bodies around and inlining big functions that are called just once and so on. This can improve performance; it is not guaranteed to do so by any stretch of anyone's imagination. For SQLite 3, it also simplifies the distribution; one source file is easier to distribute than many. – Jonathan Leffler Feb 21 '19 at 17:37
  • @brunorey — the OP saw it in the comment at the head of the source code for SQLite 3, as shown in the question. You could find it at https://sqlite.org/. – Jonathan Leffler Feb 21 '19 at 17:38
  • This is language independent its a compiler thing, so C++ as well as C as well as other compiled languages. – old_timer Feb 21 '19 at 17:43
  • @brunorey: If you grab a release copy of SQLIte from the Download page(s) and look in the top of the `sqlite3.c` file in any 3.x release, you should find more or less the disclaimer message. The source file is 7+ MiB. The `sqlite3.h` header that you use in your application is about 0.5 MiB. As of today, the current version appears to be 3.27.1; I downloaded a 3.26 version in mid-January, so any current version number is ephemeral. – Jonathan Leffler Feb 21 '19 at 17:52
  • I wonder why my comment was removed... – Eugene Sh. Feb 21 '19 at 18:32

1 Answers1

5

It will not automatically make it faster, but there is some truth to it. Having everything in a single file allows the compiler to do optimizations that would be impossible otherwise. For instance, a function cannot be inlined unless it belongs to the same translation unit in which it is called.

Inlining a function basically means that the function call is replaced with the function body. A benefit of this is that you get to skip the jump to the function code and the return jump. But if the function is in another translation unit, then the compiler will only know the prototype of the function and not the function body, which in turn means that it has to do a jump.

With that being said, I would strongly advice not to use that approach. If you really need that final tweak, then use some kind of script that can create a single c-file from your source tree.

klutt
  • 30,332
  • 17
  • 55
  • 95
  • 3
    Today many compilers/linkers offer Link Time Optimization. This should give you the same benefits. – doron Feb 21 '19 at 18:00