Optimize example/*json*'s indentation output

On a mid-range x86_64 laptop, processing the 181 MiB citylots.json file
from github.com/zemirco/sf-city-lots-json:

$ time gen/bin/example-jsonptr < citylots.json > /dev/null
Before this commit:
real    0m3.612s
After:
real    0m1.949s
Ratio: 1.85x
This commit is contained in:
Nigel Tao committed 2020-08-19 00:02:30 +10:00
1 parent 1864d9c01a
commit 0a0c7d68aa
3 files changed
+72 -41

No files matched your search

+34 -20
View File
@@ -120,10 +120,22 @@ static const char* g_usage =
// ---- // ----
// parse_flags enforces that g_flags.spaces <= 8 (the length of #define NEW_LINE_THEN_256_SPACES \
// INDENT_SPACES_STRING). "\n " \
#define INDENT_SPACES_STRING " " " " \
#define INDENT_TAB_STRING "\t" " " \
" "
#define NEW_LINE_THEN_256_TABS \
"\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
const char* g_new_line_then_256_indent_bytes;
uint32_t g_bytes_per_indent_depth;
uint8_t g_dst_array[32768]; uint8_t g_dst_array[32768];
wuffs_base__io_buffer g_dst; wuffs_base__io_buffer g_dst;
@@ -151,8 +163,9 @@ struct {
bool output_cbor_metadata_as_comments; bool output_cbor_metadata_as_comments;
bool output_extra_comma; bool output_extra_comma;
bool output_inf_nan_numbers; bool output_inf_nan_numbers;
size_t spaces;
bool tabs; bool tabs;
uint32_t spaces;
} g_flags = {0}; } g_flags = {0};
std::string // std::string //
@@ -274,17 +287,14 @@ class Callbacks : public wuffs_aux::DecodeCborCallbacks {
do { do {
switch (g_ctx) { switch (g_ctx) {
case context::none: case context::none:
// No-op. goto skip_indentation;
break;
case context::in_list_after_bracket: case context::in_list_after_bracket:
TRY(write_dst("\n", g_flags.compact_output ? 0 : 1));
g_ctx = context::in_list_after_value; g_ctx = context::in_list_after_value;
break; break;
case context::in_list_after_value: case context::in_list_after_value:
TRY(write_dst(",\n", g_flags.compact_output ? 1 : 2)); TRY(write_dst(",", 1));
break; break;
case context::in_dict_after_brace: case context::in_dict_after_brace:
TRY(write_dst("\n", g_flags.compact_output ? 0 : 1));
g_ctx = context::in_dict_after_key; g_ctx = context::in_dict_after_key;
break; break;
case context::in_dict_after_key: case context::in_dict_after_key:
@@ -292,15 +302,16 @@ class Callbacks : public wuffs_aux::DecodeCborCallbacks {
g_ctx = context::in_dict_after_value; g_ctx = context::in_dict_after_value;
goto skip_indentation; goto skip_indentation;
case context::in_dict_after_value: case context::in_dict_after_value:
TRY(write_dst(",\n", g_flags.compact_output ? 1 : 2)); TRY(write_dst(",", 1));
g_ctx = context::in_dict_after_key; g_ctx = context::in_dict_after_key;
break; break;
} }
if (!g_flags.compact_output) { if (!g_flags.compact_output) {
for (size_t i = 0; i < g_depth; i++) { uint32_t indent = g_depth * g_bytes_per_indent_depth;
TRY(write_dst(g_flags.tabs ? INDENT_TAB_STRING : INDENT_SPACES_STRING, TRY(write_dst(g_new_line_then_256_indent_bytes, 1 + (indent & 0xFF)));
g_flags.tabs ? 1 : g_flags.spaces)); for (indent >>= 8; indent > 0; indent--) {
TRY(write_dst(g_new_line_then_256_indent_bytes + 1, 0x100));
} }
} }
} while (false); } while (false);
@@ -581,13 +592,12 @@ class Callbacks : public wuffs_aux::DecodeCborCallbacks {
} else if ((g_ctx != context::in_list_after_bracket) && } else if ((g_ctx != context::in_list_after_bracket) &&
(g_ctx != context::in_dict_after_brace)) { (g_ctx != context::in_dict_after_brace)) {
if (g_flags.output_extra_comma) { if (g_flags.output_extra_comma) {
TRY(write_dst(",\n", 2)); TRY(write_dst(",", 1));
} else {
TRY(write_dst("\n", 1));
} }
for (size_t i = 0; i < g_depth; i++) { uint32_t indent = g_depth * g_bytes_per_indent_depth;
TRY(write_dst(g_flags.tabs ? INDENT_TAB_STRING : INDENT_SPACES_STRING, TRY(write_dst(g_new_line_then_256_indent_bytes, 1 + (indent & 0xFF)));
g_flags.tabs ? 1 : g_flags.spaces)); for (indent >>= 8; indent > 0; indent--) {
TRY(write_dst(g_new_line_then_256_indent_bytes + 1, 0x100));
} }
} }
g_ctx = (flags & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST) g_ctx = (flags & WUFFS_BASE__TOKEN__VBD__STRUCTURE__TO_LIST)
@@ -610,6 +620,10 @@ main1(int argc, char** argv) {
TRY(parse_flags(argc, argv)); TRY(parse_flags(argc, argv));
g_new_line_then_256_indent_bytes =
g_flags.tabs ? NEW_LINE_THEN_256_TABS : NEW_LINE_THEN_256_SPACES;
g_bytes_per_indent_depth = g_flags.tabs ? 1 : g_flags.spaces;
FILE* in = stdin; FILE* in = stdin;
if (g_flags.remaining_argc > 1) { if (g_flags.remaining_argc > 1) {
return g_usage; return g_usage;
+4 -2
View File
@@ -193,9 +193,11 @@ struct {
int remaining_argc; int remaining_argc;
char** remaining_argv; char** remaining_argv;
uint32_t max_output_depth;
char* query_c_string;
bool strict_json_pointer_syntax; bool strict_json_pointer_syntax;
uint32_t max_output_depth;
char* query_c_string;
} g_flags = {0}; } g_flags = {0};
std::string // std::string //
+34 -19
View File
@@ -270,10 +270,22 @@ bool g_sandboxed = false;
int g_input_file_descriptor = 0; // A 0 default means stdin. int g_input_file_descriptor = 0; // A 0 default means stdin.
// parse_flags enforces that g_flags.spaces <= 8 (the length of #define NEW_LINE_THEN_256_SPACES \
// INDENT_SPACES_STRING). "\n " \
#define INDENT_SPACES_STRING " " " " \
#define INDENT_TAB_STRING "\t" " " \
" "
#define NEW_LINE_THEN_256_TABS \
"\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" \
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
const char* g_new_line_then_256_indent_bytes;
uint32_t g_bytes_per_indent_depth;
#ifndef DST_BUFFER_ARRAY_SIZE #ifndef DST_BUFFER_ARRAY_SIZE
#define DST_BUFFER_ARRAY_SIZE (32 * 1024) #define DST_BUFFER_ARRAY_SIZE (32 * 1024)
@@ -581,12 +593,14 @@ struct {
bool input_allow_comments; bool input_allow_comments;
bool input_allow_extra_comma; bool input_allow_extra_comma;
bool input_allow_inf_nan_numbers; bool input_allow_inf_nan_numbers;
uint32_t max_output_depth;
bool output_extra_comma; bool output_extra_comma;
char* query_c_string;
size_t spaces;
bool strict_json_pointer_syntax; bool strict_json_pointer_syntax;
bool tabs; bool tabs;
uint32_t max_output_depth;
uint32_t spaces;
char* query_c_string;
} g_flags = {0}; } g_flags = {0};
const char* // const char* //
@@ -722,6 +736,10 @@ initialize_globals(int argc, char** argv) {
return g_usage; return g_usage;
} }
g_new_line_then_256_indent_bytes =
g_flags.tabs ? NEW_LINE_THEN_256_TABS : NEW_LINE_THEN_256_SPACES;
g_bytes_per_indent_depth = g_flags.tabs ? 1 : g_flags.spaces;
g_query.reset(g_flags.query_c_string); g_query.reset(g_flags.query_c_string);
// If the query is non-empty, suppress writing to stdout until we've // If the query is non-empty, suppress writing to stdout until we've
@@ -923,14 +941,12 @@ handle_token(wuffs_base__token t, bool start_of_token_chain) {
(g_ctx != context::in_dict_after_brace) && (g_ctx != context::in_dict_after_brace) &&
!g_flags.compact_output) { !g_flags.compact_output) {
if (g_flags.output_extra_comma) { if (g_flags.output_extra_comma) {
TRY(write_dst(",\n", 2)); TRY(write_dst(",", 1));
} else {
TRY(write_dst("\n", 1));
} }
for (uint32_t i = 0; i < g_depth; i++) { uint32_t indent = g_depth * g_bytes_per_indent_depth;
TRY(write_dst( TRY(write_dst(g_new_line_then_256_indent_bytes, 1 + (indent & 0xFF)));
g_flags.tabs ? INDENT_TAB_STRING : INDENT_SPACES_STRING, for (indent >>= 8; indent > 0; indent--) {
g_flags.tabs ? 1 : g_flags.spaces)); TRY(write_dst(g_new_line_then_256_indent_bytes + 1, 0x100));
} }
} }
@@ -956,11 +972,10 @@ handle_token(wuffs_base__token t, bool start_of_token_chain) {
TRY(write_dst(",", 1)); TRY(write_dst(",", 1));
} }
if (!g_flags.compact_output) { if (!g_flags.compact_output) {
TRY(write_dst("\n", 1)); uint32_t indent = g_depth * g_bytes_per_indent_depth;
for (size_t i = 0; i < g_depth; i++) { TRY(write_dst(g_new_line_then_256_indent_bytes, 1 + (indent & 0xFF)));
TRY(write_dst( for (indent >>= 8; indent > 0; indent--) {
g_flags.tabs ? INDENT_TAB_STRING : INDENT_SPACES_STRING, TRY(write_dst(g_new_line_then_256_indent_bytes + 1, 0x100));
g_flags.tabs ? 1 : g_flags.spaces));
} }
} }
} }