Skip to content

Commit

Permalink
processor_content_modifier: use new CFL API to reference strings and …
Browse files Browse the repository at this point in the history
…bytes

This patch adjust the CFL api usage in order to reference the strings and
bytes values (instead of doing a copy), this is a performance optimization.

Signed-off-by: Eduardo Silva <eduardo@calyptia.com>
  • Loading branch information
edsiper committed Apr 18, 2024
1 parent 1ce8d04 commit 8bf039b
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions plugins/processor_content_modifier/cm_logs.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ static int hash_transformer(void *context, struct cfl_variant *value)
return FLB_FALSE;
}

if (cfl_sds_len(converted_value->data.as_string) == 0) {
if (cfl_variant_size_get(converted_value) == 0) {
cfl_variant_destroy(converted_value);

return FLB_TRUE;
}

Expand Down Expand Up @@ -118,9 +117,12 @@ static int hash_transformer(void *context, struct cfl_variant *value)
return FLB_FALSE;
}

/* NOTE: this part does a manual modification of the variant content */
if (value->type == CFL_VARIANT_STRING ||
value->type == CFL_VARIANT_BYTES) {
cfl_sds_destroy(value->data.as_string);
if (value->referenced == CFL_FALSE) {
cfl_sds_destroy(value->data.as_string);
}
}
else if (value->type == CFL_VARIANT_ARRAY) {
cfl_array_destroy(value->data.as_array);
Expand All @@ -131,6 +133,7 @@ static int hash_transformer(void *context, struct cfl_variant *value)

value->type = CFL_VARIANT_STRING;
value->data.as_string = encoded_hash;
cfl_variant_size_set(value, cfl_sds_len(encoded_hash));

return FLB_TRUE;
}
Expand Down Expand Up @@ -178,7 +181,8 @@ int cfl_variant_convert(struct cfl_variant *input_value,
output_type == CFL_VARIANT_BYTES) {

tmp = cfl_variant_create_from_string_s(input_value->data.as_string,
cfl_sds_len(input_value->data.as_string));
cfl_variant_size_get(input_value),
CFL_FALSE);
if (!tmp) {
return CFL_FALSE;
}
Expand Down Expand Up @@ -232,7 +236,7 @@ int cfl_variant_convert(struct cfl_variant *input_value,
if (ret < 0 || ret >= sizeof(buf)) {
return CFL_FALSE;
}
tmp = cfl_variant_create_from_string_s(buf, ret);
tmp = cfl_variant_create_from_string_s(buf, ret, CFL_FALSE);
}
else if (output_type == CFL_VARIANT_BOOL) {
as_int = CFL_FALSE;
Expand Down Expand Up @@ -261,7 +265,7 @@ int cfl_variant_convert(struct cfl_variant *input_value,
if (ret < 0 || ret >= sizeof(buf)) {
return CFL_FALSE;
}
tmp = cfl_variant_create_from_string_s(buf, ret);
tmp = cfl_variant_create_from_string_s(buf, ret, CFL_FALSE);
}
else if (output_type == CFL_VARIANT_BOOL) {
as_int = CFL_FALSE;
Expand All @@ -288,7 +292,7 @@ int cfl_variant_convert(struct cfl_variant *input_value,
if (output_type == CFL_VARIANT_STRING ||
output_type == CFL_VARIANT_BYTES) {

tmp = cfl_variant_create_from_string_s("null", 4);
tmp = cfl_variant_create_from_string_s("null", 4, CFL_FALSE);
}
else if (output_type == CFL_VARIANT_BOOL) {
tmp = cfl_variant_create_from_bool(CFL_FALSE);
Expand Down Expand Up @@ -350,7 +354,8 @@ static int run_action_insert(struct content_modifier_ctx *ctx,

/* insert the new value */
kvlist = obj->variant->data.as_kvlist;
ret = cfl_kvlist_insert_string_s(kvlist, key, cfl_sds_len(key), value, cfl_sds_len(value));
ret = cfl_kvlist_insert_string_s(kvlist, key, cfl_sds_len(key), value, cfl_sds_len(value),
CFL_FALSE);
if (ret != 0) {
printf("Failed to insert key: %s\n", key);
return -1;
Expand All @@ -376,7 +381,8 @@ static int run_action_upsert(struct content_modifier_ctx *ctx,
}

/* insert the key with the updated value */
ret = cfl_kvlist_insert_string_s(kvlist, key, cfl_sds_len(key), value, cfl_sds_len(value));
ret = cfl_kvlist_insert_string_s(kvlist, key, cfl_sds_len(key), value, cfl_sds_len(value),
CFL_FALSE);
if (ret != 0) {
return -1;
}
Expand Down Expand Up @@ -461,7 +467,8 @@ static void cb_extract_regex(const char *name, const char *value, size_t value_l
cfl_kvlist_remove(kvlist, (char *) name);
}

cfl_kvlist_insert_string_s(kvlist, (char *) name, strlen(name), (char *) value, value_length);
cfl_kvlist_insert_string_s(kvlist, (char *) name, strlen(name), (char *) value, value_length,
CFL_FALSE);
}

int run_action_extract(struct content_modifier_ctx *ctx,
Expand Down Expand Up @@ -489,7 +496,9 @@ int run_action_extract(struct content_modifier_ctx *ctx,
return -1;
}

match_count = flb_regex_do(regex, v->data.as_string, cfl_sds_len(v->data.as_string), &match_list);
match_count = flb_regex_do(regex,
v->data.as_string,
cfl_variant_size_get(v), &match_list);
if (match_count <= 0) {
return -1;
}
Expand Down

0 comments on commit 8bf039b

Please sign in to comment.