Re: plenty code is confused about function level static

  • Jump to comment-1
    ranier.vf@gmail.com2024-04-18T12:08:04+00:00
    On 18/04/2024 00:39, Andres Freund wrote: >We have a fair amount of code that uses non-constant function level static >variables for read-only data. Which makes little sense - it prevents the >compiler from understanding >a) that the data is read only and can thus be put into a segment that's shared >between all invocations of the program >b) the data will be the same on every invocation, and thus from optimizing >based on that. >The most common example of this is that all our binaries use >static struct option long_options[] = { ... }; >which prevents long_options from being put into read-only memory. +1 static const allows the compiler to make additional optimizations. >There are lots of places that could benefit from adding 'static >const'. I found a few more places. Patch 004 The opposite would also help, adding static. In these places, I believe it is safe to add static, allowing the compiler to transform into read-only, definitively. Patch 005 best regards, Ranier Vilela
    • Jump to comment-1
      andres@anarazel.de2024-04-18T17:16:15+00:00
      Hi, On 2024-04-18 09:07:43 -0300, Ranier Vilela wrote: > On 18/04/2024 00:39, Andres Freund wrote: > >There are lots of places that could benefit from adding 'static > >const'. > > I found a few more places. Good catches. > Patch 004 > > The opposite would also help, adding static. > In these places, I believe it is safe to add static, > allowing the compiler to transform into read-only, definitively. I don't think this would even compile? E.g. LockTagTypeNames, pg_wchar_table are declared in a header and used across translation units. Greetings, Andres Freund
      • Jump to comment-1
        ranier.vf@gmail.com2024-04-18T17:43:58+00:00
        Em qui., 18 de abr. de 2024 às 14:16, Andres Freund <andres@anarazel.de> escreveu: > Hi, > > On 2024-04-18 09:07:43 -0300, Ranier Vilela wrote: > > On 18/04/2024 00:39, Andres Freund wrote: > > >There are lots of places that could benefit from adding 'static > > >const'. > > > > I found a few more places. > > Good catches. > > > > Patch 004 > > > > The opposite would also help, adding static. > > In these places, I believe it is safe to add static, > > allowing the compiler to transform into read-only, definitively. > > I don't think this would even compile? Compile, at least with msvc 2022. Pass ninja test. E.g. LockTagTypeNames, pg_wchar_table > are declared in a header and used across translation units. > Sad. There should be a way to export a read-only (static const) variable. Better remove these. v1-0005 attached. best regards, Ranier Vilela