Skip to content

Commit 8bf039b

Browse files
committed
processor_content_modifier: use new CFL API to reference strings and 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 <[email protected]>
1 parent 1ce8d04 commit 8bf039b

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

plugins/processor_content_modifier/cm_logs.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ static int hash_transformer(void *context, struct cfl_variant *value)
8484
return FLB_FALSE;
8585
}
8686

87-
if (cfl_sds_len(converted_value->data.as_string) == 0) {
87+
if (cfl_variant_size_get(converted_value) == 0) {
8888
cfl_variant_destroy(converted_value);
89-
9089
return FLB_TRUE;
9190
}
9291

@@ -118,9 +117,12 @@ static int hash_transformer(void *context, struct cfl_variant *value)
118117
return FLB_FALSE;
119118
}
120119

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

132134
value->type = CFL_VARIANT_STRING;
133135
value->data.as_string = encoded_hash;
136+
cfl_variant_size_set(value, cfl_sds_len(encoded_hash));
134137

135138
return FLB_TRUE;
136139
}
@@ -178,7 +181,8 @@ int cfl_variant_convert(struct cfl_variant *input_value,
178181
output_type == CFL_VARIANT_BYTES) {
179182

180183
tmp = cfl_variant_create_from_string_s(input_value->data.as_string,
181-
cfl_sds_len(input_value->data.as_string));
184+
cfl_variant_size_get(input_value),
185+
CFL_FALSE);
182186
if (!tmp) {
183187
return CFL_FALSE;
184188
}
@@ -232,7 +236,7 @@ int cfl_variant_convert(struct cfl_variant *input_value,
232236
if (ret < 0 || ret >= sizeof(buf)) {
233237
return CFL_FALSE;
234238
}
235-
tmp = cfl_variant_create_from_string_s(buf, ret);
239+
tmp = cfl_variant_create_from_string_s(buf, ret, CFL_FALSE);
236240
}
237241
else if (output_type == CFL_VARIANT_BOOL) {
238242
as_int = CFL_FALSE;
@@ -261,7 +265,7 @@ int cfl_variant_convert(struct cfl_variant *input_value,
261265
if (ret < 0 || ret >= sizeof(buf)) {
262266
return CFL_FALSE;
263267
}
264-
tmp = cfl_variant_create_from_string_s(buf, ret);
268+
tmp = cfl_variant_create_from_string_s(buf, ret, CFL_FALSE);
265269
}
266270
else if (output_type == CFL_VARIANT_BOOL) {
267271
as_int = CFL_FALSE;
@@ -288,7 +292,7 @@ int cfl_variant_convert(struct cfl_variant *input_value,
288292
if (output_type == CFL_VARIANT_STRING ||
289293
output_type == CFL_VARIANT_BYTES) {
290294

291-
tmp = cfl_variant_create_from_string_s("null", 4);
295+
tmp = cfl_variant_create_from_string_s("null", 4, CFL_FALSE);
292296
}
293297
else if (output_type == CFL_VARIANT_BOOL) {
294298
tmp = cfl_variant_create_from_bool(CFL_FALSE);
@@ -350,7 +354,8 @@ static int run_action_insert(struct content_modifier_ctx *ctx,
350354

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

378383
/* insert the key with the updated value */
379-
ret = cfl_kvlist_insert_string_s(kvlist, key, cfl_sds_len(key), value, cfl_sds_len(value));
384+
ret = cfl_kvlist_insert_string_s(kvlist, key, cfl_sds_len(key), value, cfl_sds_len(value),
385+
CFL_FALSE);
380386
if (ret != 0) {
381387
return -1;
382388
}
@@ -461,7 +467,8 @@ static void cb_extract_regex(const char *name, const char *value, size_t value_l
461467
cfl_kvlist_remove(kvlist, (char *) name);
462468
}
463469

464-
cfl_kvlist_insert_string_s(kvlist, (char *) name, strlen(name), (char *) value, value_length);
470+
cfl_kvlist_insert_string_s(kvlist, (char *) name, strlen(name), (char *) value, value_length,
471+
CFL_FALSE);
465472
}
466473

467474
int run_action_extract(struct content_modifier_ctx *ctx,
@@ -489,7 +496,9 @@ int run_action_extract(struct content_modifier_ctx *ctx,
489496
return -1;
490497
}
491498

492-
match_count = flb_regex_do(regex, v->data.as_string, cfl_sds_len(v->data.as_string), &match_list);
499+
match_count = flb_regex_do(regex,
500+
v->data.as_string,
501+
cfl_variant_size_get(v), &match_list);
493502
if (match_count <= 0) {
494503
return -1;
495504
}

0 commit comments

Comments
 (0)