-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding the option for customs constexpr string constants #157
base: main
Are you sure you want to change the base?
Adding the option for customs constexpr string constants #157
Conversation
The reason they are not string_view by default is because djinni string is mapped to If we change djinni string parameters to use std::string_view in C++, this will result in a mismatch between djinni strings in results and records vs as parameters. I personally feel that complicates things too much. |
At least in my library, we would rather the small overhead of having to potentially allocate the string if we end up taking one of those constants and passing it to a Djinni function, rather than paying the startup cost of heap allocating all strings during program boot. Given that this is an option and is by default disabled, callers who would prefer to keep the strings in static storage for the lifetime of the program can still do that. But I would really appreciate having the option to decide which approach makes most sense for me. |
Actually it just make sense to make the interface functions also to take a string_view by value as parameter in C++. |
Performance wise you are correct. But this means we cannot simply tell the user that djinni string is std::string in C++. They are string_view when used as single parameter, and std::string as return values or record members. |
As long as that's opt in behavior then that's probably a reasonable tradeoff? |
Context
The motivation for this change is that string constants are defined in C++ as static const std::strings, which during the program startup they do allocations on the heap and copy data.
Changes
This PR adds a couple cli options which will allow the user to specify a constexpr string constant type, for example,
std::string_view
andconst char*
.As well it allows to optional include a header file (for example
<string_view>
). In the case of usingconst char*
the optional header can be undefined and no header file will be included.Testing
static const std::string
was defined in the generated header, the<string>
header file included and in the generated source the definition of the constant was added.const char*
and removed the header option from the build script, observed how the string constant was defined with the primitive type and no include files were added.