go-sqlite3 icon indicating copy to clipboard operation
go-sqlite3 copied to clipboard

Why does compiling with `CGO_CFLAGS="-DSQLITE_DQS=0"` make my binary bigger?

Open playfulpachyderm opened this issue 1 year ago • 1 comments

This is a slightly odd question. I was curious what effect removing the DQS support would have on my release binary, so I tested it. To my surprise, adding CGO_CFLAGS="-DSQLITE_DQS=0" increased the size instead of decreasing it. The effect size is substantial, too; removing support for DQS made it go from 11803168 to 11996064 bytes-- almost 200 kb bigger.

I tried to check if there was something in particular that got bigger, so I rebuilt both without ldflags="-w -s" and ran:

diff <(nm --format=posix --radix=d --size-sort tw | grep -i sqlite | awk '{print $1, $4}') <(nm --format=posix --radix=d --size-sort tw-with-dqs | grep -i sqlite | awk '{print $1, $4}')

to see if there were any sqlite symbols that changed size significantly due to the change. Well, it turns out they all did; the output was apparently every sqlite symbol.

I'm relatively inexperienced with compilers and linkers, but this result seems surprising; why would removing this feature have such a large effect on the compiled result?

playfulpachyderm avatar Nov 04 '24 06:11 playfulpachyderm

Ok, after a bit of investigation I noticed it's because my CGO_CFLAGS clobbered the default ones which included -O2, which explains the substantial size increase. So that explains that part.

I also noticed that most of my CFLAGS had no effect; it seems that #cgo CFLAGS: ... directives in the source override command-line environment variables. So that explains some other confusion I had.

In the process of investigating this, I noticed that a lot of CFLAGS are set in sqlite3.go. Is there any way for me to override them? For example, if I know I don't use FTS3 and I don't want it included?

playfulpachyderm avatar Nov 04 '24 06:11 playfulpachyderm