Hello everyone,
I have come back with some updates on my progress. For the past few weeks, I have been trying to benchmark, profile to optimize the code. After a while, I was able to optimize test1.c and test2.c as I have stated before.
For the test2.c I was able to combine 2 ifdef TEST_FORMATTED together. In the end, it gives the same result, and became more optimized. The code was quite short, so I do not really have anything else to add or remove. If it pass the test, it is ok for me.
Old:
#ifdef TEST_FORMATTED
int sflags = 0;
#endif
MC_SET_DEBUG(1);
#ifdef TEST_FORMATTED
sflags = parse_flags(argc, argv);
#endif
New:
#ifdef TEST_FORMATTED
int sflags = 0;
sflags = parse_flags(argc, argv);
#endif
With the test1.c, I used the same changed in test1.c, that I changed ifdef TEST_FORMATTED so that it takes less lines. I also applied Hoisting to optimize the loop part. Here is how I changed it:
Old:
my_array = json_object_new_array();
json_object_array_add(my_array, json_object_new_int(1));
json_object_array_add(my_array, json_object_new_int(2));
json_object_array_add(my_array, json_object_new_int(3));
json_object_array_put_idx(my_array, 4, json_object_new_int(5));
printf("my_array=\n");
for(i=0; i < json_object_array_length(my_array); i++)
{
json_object *obj = json_object_array_get_idx(my_array, i);
printf("\t[%d]=%s\n", (int)i, json_object_to_json_string(obj));
}
printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
json_object_put(my_array);
test_array_del_idx();
my_array = json_object_new_array();
json_object_array_add(my_array, json_object_new_int(3));
json_object_array_add(my_array, json_object_new_int(1));
json_object_array_add(my_array, json_object_new_int(2));
json_object_array_put_idx(my_array, 4, json_object_new_int(0));
printf("my_array=\n");
for(i=0; i < json_object_array_length(my_array); i++)
{
json_object *obj = json_object_array_get_idx(my_array, i);
printf("\t[%d]=%s\n", (int)i, json_object_to_json_string(obj));
}
printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
json_object_array_sort(my_array, sort_fn);
printf("my_array=\n");
for(i=0; i < json_object_array_length(my_array); i++)
{
json_object *obj = json_object_array_get_idx(my_array, i);
printf("\t[%d]=%s\n", (int)i, json_object_to_json_string(obj));
}
New:
my_array = json_object_new_array();
json_object_array_add(my_array, json_object_new_int(1));
json_object_array_add(my_array, json_object_new_int(2));
json_object_array_add(my_array, json_object_new_int(3));
json_object_array_put_idx(my_array, 4, json_object_new_int(5));
unsigned int my_array_length = json_object_array_length(my_array);
printf("my_array=\n");
for(i=0; i < my_array_length; i++)
{
json_object *obj = json_object_array_get_idx(my_array, i);
printf("\t[%d]=%s\n", (int)i, json_object_to_json_string(obj));
}
printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
json_object_put(my_array);
test_array_del_idx();
my_array = json_object_new_array();
json_object_array_add(my_array, json_object_new_int(3));
json_object_array_add(my_array, json_object_new_int(1));
json_object_array_add(my_array, json_object_new_int(2));
json_object_array_put_idx(my_array, 4, json_object_new_int(0));
printf("my_array=\n");
for(i=0; i < my_array_length; i++)
{
json_object *obj = json_object_array_get_idx(my_array, i);
printf("\t[%d]=%s\n", (int)i, json_object_to_json_string(obj));
}
printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
json_object_array_sort(my_array, sort_fn);
printf("my_array=\n");
for(i=0; i < my_array_length; i++)
{
json_object *obj = json_object_array_get_idx(my_array, i);
printf("\t[%d]=%s\n", (int)i, json_object_to_json_string(obj));
}
After making sure the result is not changed to provide a valid result, I ran the make command again to get my result, and the runtime has gone down quite a bit. All the tests are passed and there were no problem running the command, which proves my successful with this optimization test.

So in the end, I was able to optimize the code to use less command and make it run better than before. I will try to get my code accepted by the upstream project. Here is my github rep if you want to check out. Thank you for your time.
github Link: https://github.com/hoaianhkhang/json-c/tree/test-build