Skip to content
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

variant: extended API to support (optional) referenced strings and bytes #43

Merged
merged 9 commits into from
Apr 12, 2024

Conversation

edsiper
Copy link
Member

@edsiper edsiper commented Apr 12, 2024

Note: this PR bumps CFL to v0.5.0

This PR extends the variants functionality to support referenced values. When adding strings or bytes as variants, the current API creates a copy of those, however there are cases where the caller control its own buffer only wants to have a reference but not a new copy to avoid memory duplication; this is the case of an optimization we are doing in Fluent Bit.

This is a refactor that affects variants, kvlist and arrays, the following is a summary of the new APIs that changed (these are the latest versions):

variants

struct cfl_variant *cfl_variant_create_from_string_s(char *value, size_t value_size, int referenced);
struct cfl_variant *cfl_variant_create_from_bytes(char *value, size_t length, int referenced);

This PR also adds two new functions to set and retrieve the size of such buffers:

void cfl_variant_size_set(struct cfl_variant *var, size_t size);
size_t cfl_variant_size_get(struct cfl_variant *var);

kvlist

int cfl_kvlist_insert_bytes(struct cfl_kvlist *list, char *key, char *value,
                            char *value, size_t value_length,
                            int referenced);

int cfl_kvlist_insert_bytes_s(struct cfl_kvlist *list,
                              char *key, size_t key_size,
                              char *value, size_t value_length,
                              int referenced);

int cfl_kvlist_insert_string_s(struct cfl_kvlist *list,
                              char *key, size_t key_size,
                              char *value, size_t value_size,
                              int referenced);

array

int cfl_array_append_string_s(struct cfl_array *array, char *str, size_t str_len, int referenced);
int cfl_array_append_bytes(struct cfl_array *array, char *value, size_t length, int referenced);

In addition to the changes in array, I have added a new unit test for the array API.


These are breaking changes that once this library is updated, we will need changes also in CMetrics and CTraces

edsiper added 9 commits April 12, 2024 09:23
Signed-off-by: Eduardo Silva <[email protected]>
When creating variants as strings or bytes, upon creation the bytes for those
data types are allocated in a new memory buffer, it's a copy of what the caller
passed as a reference. There are cases where due to performance and
optimization reasons the caller might want to keep a reference to the original
buffer rather than a full copy.

This patch adds a new flag called 'referenced' that can be used by the new API
changes coming in the new series of patches.

Signed-off-by: Eduardo Silva <[email protected]>
The following patch does prototype changes for functions that create variants of
string and byte types, as well it adds two new functions to set the size/lengths
of the buffers. The new prototypes are as follows:

 - struct cfl_variant *cfl_variant_create_from_string_s(char *value, size_t value_size, int referenced)
 - struct cfl_variant *cfl_variant_create_from_bytes(char *value, size_t length, int referenced)

the new `referenced` flag marks if the caller desires to keep a reference to the original buffer
rather a full copy in a new buffer.

Also this patch adds two new functions to set and retrieve the size of such buffers:

- void cfl_variant_size_set(struct cfl_variant *var, size_t size);
- size_t cfl_variant_size_get(struct cfl_variant *var);

Signed-off-by: Eduardo Silva <[email protected]>
The following patch adjust the code to use the new Variant API prototypes and
also changes some specific kvlist functions to reflect the functionality. The
following functions have been modified:

  int cfl_kvlist_insert_bytes(struct cfl_kvlist *list,
                             char *key, char *value,
                             size_t value_length);
                             size_t value_length,
                             int referenced);

  int cfl_kvlist_insert_bytes_s(struct cfl_kvlist *list,
                                char *key, size_t key_size,
                                char *value,
                                size_t value_length);
                                size_t value_length,
                                int referenced);

  int cfl_kvlist_insert_string_s(struct cfl_kvlist *list,
                                 char *key, size_t key_size,
                                 char *value, size_t value_size);
                                 char *value, size_t value_size,
                                 int referenced);

Signed-off-by: Eduardo Silva <[email protected]>
This patch adjust the following functions to manipulate the new 'referenced' field
from variants:

 int cfl_array_append_string_s(struct cfl_array *array, char *str, size_t str_len, int referenced)
 int cfl_array_append_bytes(struct cfl_array *array, char *value, size_t length, int referenced)

It also adds an extra check for the array size when it's resizable.

Signed-off-by: Eduardo Silva <[email protected]>
@edsiper edsiper merged commit 5d771a8 into master Apr 12, 2024
23 checks passed
@edsiper edsiper deleted the referenced branch April 12, 2024 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant