Skip to content

Commit

Permalink
A bit of more speed improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
adamritter committed Jun 1, 2023
1 parent c0eb2f6 commit 245c353
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
24 changes: 24 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,27 @@ endif()

install(TARGETS fastgron
RUNTIME DESTINATION bin) # for executables

# use ctest
include(CTest)

# Add test
enable_testing()

add_test(version_test COMMAND sh -c "${CMAKE_CURRENT_BINARY_DIR}/fastgron -V 2>&1")
set_tests_properties(version_test PROPERTIES PASS_REGULAR_EXPRESSION "fastgron version ${FASTGRON_VERSION}")

add_test(array_test COMMAND sh -c "echo '{\"a\":[1,2,3]}' | ${CMAKE_CURRENT_BINARY_DIR}/fastgron")
set_tests_properties(array_test PROPERTIES PASS_REGULAR_EXPRESSION "json.a\\[1\\] = 2;\\njson.a\\[2\\] = 3;")

add_test(map_test COMMAND sh -c "echo '{\"a\":{\"c\":1,\"b\":2}}' | ${CMAKE_CURRENT_BINARY_DIR}/fastgron")
set_tests_properties(map_test PROPERTIES PASS_REGULAR_EXPRESSION "json.a.c = 1;\\njson.a.b = 2;")

add_test(sort_map_test COMMAND sh -c "echo '{\"a\":{\"c\":1,\"b\":2}}' | ${CMAKE_CURRENT_BINARY_DIR}/fastgron --sort")
set_tests_properties(sort_map_test PROPERTIES PASS_REGULAR_EXPRESSION "json.a.b = 2;\\njson.a.c = 1;")

add_test(filter_fixed_string_text COMMAND sh -c "echo '{\"a\":[5,4,3,1]}' | ${CMAKE_CURRENT_BINARY_DIR}/fastgron -F 1")
set_tests_properties(filter_fixed_string_text PROPERTIES PASS_REGULAR_EXPRESSION "json.a\\[1\\] = 4;\\njson.a\\[3\\] = 1;")

add_test(sort_and_filter_test COMMAND sh -c "echo '{\"a\":{\"c\":13,\"b\":22,\"a\":31}}' | ${CMAKE_CURRENT_BINARY_DIR}/fastgron --sort -F 3")
set_tests_properties(sort_and_filter_test PROPERTIES PASS_REGULAR_EXPRESSION "json.a.a = 31;\\njson.a.c = 13;")
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ citylots.json can be downloaded here: https://github.com/zemirco/sf-city-lots-js
```
time fastgron ~/Downloads/citylots.json > /dev/null
fastgron ~/Downloads/citylots.json > /dev/null 0.39s user 0.07s system 99% cpu 0.465 total
fastgron ~/Downloads/citylots.json > /dev/null 0.38s user 0.07s system 99% cpu 0.447 total
time gron --no-sort ~/Downloads/citylots.json >/dev/null
gron --no-sort ~/Downloads/citylots.json > /dev/null 27.60s user 30.73s system 158% cpu 36.705 total
Expand Down
51 changes: 28 additions & 23 deletions src/fastgron.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ struct growing_string
{
return {data.data(), len};
}
void clear_mem()
{
len = 0;
data.clear();
}
};

void write_all(string_view s)
Expand All @@ -116,22 +121,29 @@ void batched_print_flush()
batched_out.erase(0);
}

void batched_print(string_view s)
void batched_print_flush_if_needed()
{
batched_out.append(s);
if (batched_out.size() > 1000000)
{
batched_print_flush();
}
}

void batched_print(string_view s)
{
batched_out.append(s);
batched_print_flush_if_needed();
}

void batched_print_no_flush(string_view s)
{
batched_out.append(s);
}

void batched_print(char c)
{
batched_out.append(c);
if (batched_out.size() > 1000000)
{
batched_print_flush();
}
batched_print_flush_if_needed();
}

bool is_js_identifier(string_view s)
Expand Down Expand Up @@ -235,22 +247,16 @@ bool can_show(string_view s)
return true;
}

void gprint(string_view s, growing_string *out_growing_string)
void gprint(string_view s, growing_string &out_growing_string)
{
if (!can_show(s))
{
return;
}

if (out_growing_string)
{
out_growing_string->append(s);
return;
}
batched_print(s);
out_growing_string.append(s);
}

void recursive_print_gron(ondemand::value element, growing_string &path, growing_string *out_growing_string)
void recursive_print_gron(ondemand::value element, growing_string &path, growing_string &out_growing_string)
{
switch (element.type())
{
Expand Down Expand Up @@ -281,10 +287,8 @@ void recursive_print_gron(ondemand::value element, growing_string &path, growing
path.append(" = {};\n");
gprint(path, out_growing_string);
path.erase(base_len);
// gprint("OBJECT************\n", out_growing_string);
if (sort_output)
{
// gprint("SORT**********\n", out_growing_string);
std::vector<std::pair<string, string>> fields;
growing_string out2;
for (auto field : element.get_object())
Expand All @@ -302,7 +306,7 @@ void recursive_print_gron(ondemand::value element, growing_string &path, growing
path.append(".");
path.append(key.value());
}
recursive_print_gron(field.value(), path, &out2);
recursive_print_gron(field.value(), path, out2);
path.erase(base_len);
fields.emplace_back(key_str, string(out2));
out2.erase(0);
Expand All @@ -311,7 +315,7 @@ void recursive_print_gron(ondemand::value element, growing_string &path, growing
{ return a.first < b.first; });
for (auto &field : fields)
{
gprint(field.second, out_growing_string);
out_growing_string.append(field.second);
}
}
else
Expand Down Expand Up @@ -406,6 +410,7 @@ void recursive_print_gron(ondemand::value element, growing_string &path, growing
break;
}
}
batched_print_flush_if_needed();
}

// Parse command-line options
Expand Down Expand Up @@ -838,7 +843,7 @@ void print_filtered_path(growing_string &path, int processed, ondemand::value el
}
if (path.size() == processed)
{
recursive_print_gron(element, path, nullptr);
recursive_print_gron(element, path, batched_out);
}
else if (path.data[processed] == '.')
{
Expand Down Expand Up @@ -1103,11 +1108,11 @@ int main(int argc, char *argv[])
{
ondemand::document_stream docs = parser.iterate_many(json);
int index = 0;
gprint("json = [];\n", nullptr);
gprint("json = [];\n", batched_out);
for (auto doc : docs)
{
growing_string path = growing_string("json[").append(to_string(index++)).append("]");
recursive_print_gron(doc, path, nullptr);
recursive_print_gron(doc, path, batched_out);
}
}
// Execute as single document
Expand All @@ -1133,7 +1138,7 @@ int main(int argc, char *argv[])
}
else
{
recursive_print_gron(val, path, nullptr);
recursive_print_gron(val, path, batched_out);
}
}
batched_print_flush();
Expand Down

0 comments on commit 245c353

Please sign in to comment.