-
Notifications
You must be signed in to change notification settings - Fork 1.9k
pack: 2.8x performance improvements for JSON parsing #10766
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aa8e52e
pack: add new JSON parser wrapper for yyjson
edsiper 2d5f2c5
pack_json: new API to abstract JSON parser backend
edsiper 649e7ed
tests: internal: mp: fix tests and API
edsiper 9eaf94c
lib: yyjson: add v0.12.0
edsiper eb8ec93
lib: yyjson: add Unicode replacement and surrogate tolerance flags
edsiper 090967a
cmake: register yyjson path
edsiper 47d8aaa
benchmarks: add json benchmark pack tool
edsiper de14084
lib: yyjson: sync latest changes from PR
edsiper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| add_executable(flb-bench-pack_json pack_json.c) | ||
| target_link_libraries(flb-bench-pack_json | ||
| fluent-bit-static | ||
| ${CMAKE_THREAD_LIBS_INIT} | ||
| ) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <time.h> | ||
| #include <unistd.h> | ||
|
|
||
| #include <fluent-bit.h> | ||
| #include <fluent-bit/flb_pack_json.h> | ||
|
|
||
| #define ITERATIONS 100000 | ||
|
|
||
| static long diff_ns(struct timespec *s, struct timespec *e) | ||
| { | ||
| return (e->tv_sec - s->tv_sec) * 1000000000L + (e->tv_nsec - s->tv_nsec); | ||
| } | ||
|
|
||
| static int compare_encoders_output(char *json, size_t len) | ||
| { | ||
| int ret; | ||
|
|
||
| int root_type1; | ||
| char *out_buf1; | ||
| size_t out_size1 = 0; | ||
|
|
||
| int root_type2; | ||
| char *out_buf2; | ||
| size_t out_size2 = 0; | ||
|
|
||
| struct flb_pack_opts opts = {0}; | ||
|
|
||
| /* jsmn */ | ||
| opts.backend = FLB_PACK_JSON_BACKEND_JSMN; | ||
| ret = flb_pack_json_ext(json, len, &out_buf1, &out_size1, &root_type1, &opts); | ||
| if (ret != 0) { | ||
| fprintf(stderr, "error jsmn\n"); | ||
| return -1; | ||
| } | ||
|
Comment on lines
+32
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix memory leak when first encoding fails If the first encoding fails, /* jsmn */
opts.backend = FLB_PACK_JSON_BACKEND_JSMN;
ret = flb_pack_json_ext(json, len, &out_buf1, &out_size1, &root_type1, &opts);
if (ret != 0) {
fprintf(stderr, "error jsmn\n");
return -1;
}
🤖 Prompt for AI Agents |
||
| fprintf(stderr, "jsmn records count: %d\n", flb_mp_count(out_buf1, out_size1)); | ||
|
|
||
| /* yyjson */ | ||
| opts.backend = FLB_PACK_JSON_BACKEND_YYJSON; | ||
| ret = flb_pack_json_ext(json, len, &out_buf2, &out_size2, &root_type2, &opts); | ||
| if (ret != 0) { | ||
| fprintf(stderr, "error yyjson\n"); | ||
| flb_free(out_buf1); | ||
| return -1; | ||
| } | ||
|
|
||
| if (out_size1 != out_size2 || memcmp(out_buf1, out_buf2, out_size1) != 0) { | ||
| fprintf(stderr, "msgpack mismatch between jsmn and yyjson\n"); | ||
| fprintf(stderr, "jsmn size: %zu, yyjson size: %zu\n", out_size1, out_size2); | ||
| flb_free(out_buf1); | ||
| flb_free(out_buf2); | ||
| return -1; | ||
| } | ||
|
|
||
| flb_free(out_buf1); | ||
| flb_free(out_buf2); | ||
| return 0; | ||
| } | ||
|
|
||
| int main(int argc, char **argv) | ||
| { | ||
| int i; | ||
| int ret; | ||
| size_t len; | ||
| struct timespec ts, te; | ||
| long d_jsmn, d_yyjson; | ||
| char *mp_buf; | ||
| size_t mp_size; | ||
| struct flb_pack_opts opts = {0}; | ||
| int root_type; | ||
| char *json; | ||
| int iterations = 100; | ||
| char *log_file = NULL; | ||
| int opt; | ||
| uint64_t total_bytes; | ||
| double mibps_jsmn; | ||
| double mibps_yyjson; | ||
| double ratio; | ||
| double reduction; | ||
|
|
||
| /* Parse command-line options */ | ||
| while ((opt = getopt(argc, argv, "i:f:")) != -1) { | ||
| switch (opt) { | ||
| case 'i': | ||
| iterations = atoi(optarg); | ||
| if (iterations <= 0) { | ||
| fprintf(stderr, "Invalid value for -i (iterations): %s\n", optarg); | ||
| return 1; | ||
| } | ||
| break; | ||
| case 'f': | ||
| log_file = optarg; | ||
| break; | ||
| default: | ||
| fprintf(stderr, "Usage: %s -f log_file [-i iterations]\n", argv[0]); | ||
| return 1; | ||
| } | ||
| } | ||
|
|
||
| if (log_file == NULL) { | ||
| fprintf(stderr, "Error: log file (-f) is required.\n"); | ||
| fprintf(stderr, "Usage: %s -f log_file [-i iterations]\n", argv[0]); | ||
| return 1; | ||
| } | ||
|
|
||
| ret = flb_utils_read_file(log_file, &json, &len); | ||
| if (ret != 0) { | ||
| fprintf(stderr, "error reading %s\n", log_file); | ||
| return 1; | ||
| } | ||
|
|
||
| printf("Comparing encoders output: "); | ||
| ret = compare_encoders_output(json, len); | ||
| if (ret != 0) { | ||
| printf("failed\n"); | ||
| free(json); | ||
| exit(EXIT_FAILURE); | ||
| } | ||
| printf("ok\n\n"); | ||
|
|
||
| printf("Benchmarking JSON packing to msgpack\n"); | ||
| printf("-------------------------------------------\n\n"); | ||
| printf("Iterations : %d\n", iterations); | ||
| printf("JSON size : %zu bytes\n", len); | ||
| printf("-------------------------------------------\n\n"); | ||
|
|
||
| /* JSMN */ | ||
| opts.backend = FLB_PACK_JSON_BACKEND_JSMN; | ||
| clock_gettime(CLOCK_MONOTONIC, &ts); | ||
| for (i = 0; i < iterations; i++) { | ||
| ret = flb_pack_json_ext(json, len, &mp_buf, &mp_size, &root_type, &opts); | ||
| if (ret != 0) { | ||
| fprintf(stderr, "error jsmn\n"); | ||
| free(json); | ||
| return 1; | ||
| } | ||
| flb_free(mp_buf); | ||
| } | ||
| clock_gettime(CLOCK_MONOTONIC, &te); | ||
| d_jsmn = diff_ns(&ts, &te); | ||
|
|
||
| /* YYJSON */ | ||
| opts.backend = FLB_PACK_JSON_BACKEND_YYJSON; | ||
| clock_gettime(CLOCK_MONOTONIC, &ts); | ||
| for (i = 0; i < iterations; i++) { | ||
| ret = flb_pack_json_ext(json, len, &mp_buf, &mp_size, &root_type, &opts); | ||
| if (ret != 0) { | ||
| fprintf(stderr, "error yyjson\n"); | ||
| free(json); | ||
| return 1; | ||
| } | ||
| flb_free(mp_buf); | ||
| } | ||
| clock_gettime(CLOCK_MONOTONIC, &te); | ||
| d_yyjson = diff_ns(&ts, &te); | ||
|
|
||
|
|
||
| total_bytes = (uint64_t) len * (uint64_t) iterations; | ||
|
|
||
| mibps_jsmn = (double) total_bytes / d_jsmn * 1e9 / (1024.0 * 1024.0); | ||
| mibps_yyjson = (double) total_bytes / d_yyjson * 1e9 / (1024.0 * 1024.0); | ||
|
|
||
| ratio = (double) d_jsmn / (double) d_yyjson; | ||
| reduction = (double) (d_jsmn - d_yyjson) * 100.0 / (double) d_jsmn; | ||
|
|
||
| printf("-------------------------------------------\n"); | ||
| printf("old encoder : %ld ns | %.2f MiB/s\n", d_jsmn, mibps_jsmn); | ||
| printf("new encoder : %ld ns | %.2f MiB/s\n", d_yyjson, mibps_yyjson); | ||
| printf("-------------------------------------------\n"); | ||
| printf("Speedup : %.2fx (time -%.2f%%)\n", ratio, reduction); | ||
|
|
||
| free(json); | ||
| return 0; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Include missing headers for memcmp and fixed-width ints; drop unused macro
memcmprequires<string.h>.<stdint.h>(and<inttypes.h>if printing 64-bit ints).ITERATIONSis unused.📝 Committable suggestion
🤖 Prompt for AI Agents