[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using gperf to allow switch-case over strings (in c++17)

From: Bruno Haible
Subject: Re: Using gperf to allow switch-case over strings (in c++17)
Date: Sun, 26 Dec 2021 18:31:35 +0100

Hello Viktor,

> I'm a C++ developer and tried using gperf for generating code that would 
> allow me to write a switch-case over strings.
> This didn't work at first, so I dug into the source that gperf had 
> generated and added "constexpr" declarations in a few
> places. With that, my switch-case over strings worked! (requires c++17)

Improved support of newer C++ versions is, of course, desirable in gperf.
Therefore I appreciate that you have taken the time to write this patch.

Regarding the coding style, the patch is perfect.

But I have two remarks.

1) Since 'constexpr' is a built-in keyword in C++, why does --language=C++
not imply --constexpr in the first place? In other words, as a user I
would like the generated code to be optimal without any additional
command-line options.

Probably this goal can be achieved by removing the changes in
options.h, options.cc, input.cc. In output.cc define
constexpr_qualifier to something like
"#if __cplusplus >= 201703L\nconstexpr\n#endif\n"
and constexpr_or_static to something like
"#if __cplusplus >= 201703L\nconstexpr\n#else\nstatic\n#endif\n"

2) The generated code
>          g++ --std=c++17 -o test test_public.cpp

If you compile with option -S, you will see that in the functions
Perfect_Hash::hash and Perfect_Hash::in_word_set the arrays are
initialized *at every invocation*.

I understand that this is because [1] specifies that
  "the function body must not contain:
   ... a definition of a variable of static ... storage duration"

But it is a performance killer! gperf is meant to generate fast code,
and one of the necessary requirements is that the arrays are prepared
once only (and preferrably in read-only storage).

To me, this is a show-stopper.

Do you see a way to use 'constexpr' with e.g. the Perfect::hash
function while at the same time keep the array in 'static' storage?


[1] https://en.cppreference.com/w/cpp/language/constexpr

reply via email to

[Prev in Thread] Current Thread [Next in Thread]