Any good reason for const char[] in a function?

34 views
Skip to first unread message

Victor Costan

unread,
Jun 11, 2018, 5:59:10 AM6/11/18
to cxx
Dear C++ experts,

While skimming through code using //sql, I saw a fair amount of places code SQL instructions using a const char[] (note the lack of static) in a function. Results (including places that use "static"): https://cs.chromium.org/search/?q=const%5C+char%5C+.*sql.*%5C%5B%5C%5D%5C+%5C%3D+-file:src/third_party/&p=5&sq=package:chromium&type=cs

I played a bit on godbolt.org and learned that clang 6 figures out the "static" regardless of optimization level, whereas gcc 8.1 optimizes to a static string with -Os, but initializes a string on the stack with -O2.

Before going in blindly and adding static to SQL query strings inside functions, I'd like to know if there's any benefit to leaving out the "static", so I don't regress something out of ignorance.

Please help?

Thank you very much,
    Victor

Hans Wennborg

unread,
Jun 11, 2018, 7:25:08 AM6/11/18
to Victor Costan, cxx
On Mon, Jun 11, 2018 at 11:59 AM, Victor Costan <pwn...@chromium.org> wrote:
> While skimming through code using //sql, I saw a fair amount of places code
> SQL instructions using a const char[] (note the lack of static) in a
> function. Results (including places that use "static"):
> https://cs.chromium.org/search/?q=const%5C+char%5C+.*sql.*%5C%5B%5C%5D%5C+%5C%3D+-file:src/third_party/&p=5&sq=package:chromium&type=cs
>
> I played a bit on godbolt.org and learned that clang 6 figures out the
> "static" regardless of optimization level, whereas gcc 8.1 optimizes to a
> static string with -Os, but initializes a string on the stack with -O2.

Actually, the reason Clang 6 doesn't copy the string onto the stack is
that it assumes -fmerge-all-constants by default. In trunk Clang that
has been turned off because it's not strictly speaking
standards-conforming (the constant is supposed to have a unique
address). This doesn't matter for Chromium though, as we build with
-fmerge-all-constants explicitly turned on.

> Before going in blindly and adding static to SQL query strings inside
> functions, I'd like to know if there's any benefit to leaving out the
> "static", so I don't regress something out of ignorance.

There's no benefit that I know of, except it being shorter without "static" :-)

Cheers,
Hans

Gabriel Charette

unread,
Jun 11, 2018, 11:15:45 AM6/11/18
to Hans Wennborg, Victor Costan, cxx
There was a discussion about this a while back : https://groups.google.com/a/chromium.org/d/msg/chromium-dev/pF7nAmUQ2uw/wrNhX5bN8YkJ

The conclusion was that we should add static for all non-global constant compound types. I once tried to write a regex to mass migrate but never completed the work; happy to review if you succeed :).

IIUC the reason the compiler can't optimize it out if it's not static is that technically someone could take the pointer to it and expect it to be on the stack (and there's also the possibility of const_cast -- i.e. from what I understand const establishes a usage rule but isn't used for optimizations). In practice it would be silly to do this with a const char[] but the compiler can't assume it's not done (and I'm told look-ahead optimizations, e.g. figuring out if it's used that way, are full of corner cases and not done).

--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To post to this group, send email to c...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAB8jPhdFiKUE9mY%3D9dh05Wj99vEf5GEQv_Nn2%3D7VvMUNQY_xtQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages