From f80d978db90f9ea3a5e781c3029fbd3adb08aeda Mon Sep 17 00:00:00 2001 From: Kyle Simpson Date: Wed, 15 Dec 2021 10:40:28 -0600 Subject: [PATCH 1/6] fixing performance bug (see #64) in Perl implementation --- json_minify/lib/JSON_minify.pm | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/json_minify/lib/JSON_minify.pm b/json_minify/lib/JSON_minify.pm index 9e7c978..e644fa7 100644 --- a/json_minify/lib/JSON_minify.pm +++ b/json_minify/lib/JSON_minify.pm @@ -3,6 +3,8 @@ ## JSON_minify.pm ## Copyright ©2018 Rémi Cohen-Scali ## +## Patched by Kyle Simpson, 2021 +## ## ## Permission is hereby granted, free of charge, to any person obtaining a copy ## of this software and associated documentation files (the “Software”), to @@ -23,7 +25,7 @@ ## IN THE SOFTWARE. ## -our $VERSION = '1.1'; +our $VERSION = '2.0'; package JSON_minify; @@ -53,6 +55,8 @@ sub minify_string { my $new_str = ""; # Current position of processing in input content my $index = 0; + # Previous match index + my $prevIndex = 0; # Flag indicating if processing is currently inside a multi line comment my $in_multi = 0; @@ -106,6 +110,8 @@ sub minify_string { $new_str .= ' ' x ($input_pos - $index - $token_len); } + # Save previous index + $prevIndex = $index; # As we copied the input chars, let's set index to actual position $index = $input_pos; # And get the match in a temporary @@ -117,6 +123,10 @@ sub minify_string { my $leftcontext = substr($input_string, 0, $input_pos-1); # Match it searching for a string of backslash (i.e. \ or \\ or \\\ etc) # at the end of the string + # + # FIXME: this match should start at the `$prevIndex` location + # to avoid a bad performance bug, but how to do that in perl? + # see: https://github.com/getify/JSON.minify/issues/64 (my $escaped = $leftcontext) =~ m/(\\)*$/; # Get length of match my $escaped_full_len = length $& || ''; From 51b12a2925e44792d523a4101cd64279654fa4f4 Mon Sep 17 00:00:00 2001 From: Kyle Simpson Date: Wed, 15 Dec 2021 11:01:07 -0600 Subject: [PATCH 2/6] addressing FIXME --- json_minify/lib/JSON_minify.pm | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/json_minify/lib/JSON_minify.pm b/json_minify/lib/JSON_minify.pm index e644fa7..c62b690 100644 --- a/json_minify/lib/JSON_minify.pm +++ b/json_minify/lib/JSON_minify.pm @@ -120,13 +120,9 @@ sub minify_string { if ($token eq '"' && ! $in_comment) { # Get the left context of the match - my $leftcontext = substr($input_string, 0, $input_pos-1); + my $leftcontext = substr($input_string, $prevIndex, $input_pos-1); # Match it searching for a string of backslash (i.e. \ or \\ or \\\ etc) # at the end of the string - # - # FIXME: this match should start at the `$prevIndex` location - # to avoid a bad performance bug, but how to do that in perl? - # see: https://github.com/getify/JSON.minify/issues/64 (my $escaped = $leftcontext) =~ m/(\\)*$/; # Get length of match my $escaped_full_len = length $& || ''; From aa6109dc289d6b4da292326b023525b777ee1577 Mon Sep 17 00:00:00 2001 From: Kyle Simpson Date: Wed, 15 Dec 2021 11:16:55 -0600 Subject: [PATCH 3/6] fixing incorrect argument to substr(..) --- json_minify/lib/JSON_minify.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json_minify/lib/JSON_minify.pm b/json_minify/lib/JSON_minify.pm index c62b690..a0b5bb5 100644 --- a/json_minify/lib/JSON_minify.pm +++ b/json_minify/lib/JSON_minify.pm @@ -120,7 +120,7 @@ sub minify_string { if ($token eq '"' && ! $in_comment) { # Get the left context of the match - my $leftcontext = substr($input_string, $prevIndex, $input_pos-1); + my $leftcontext = substr($input_string, $prevIndex, $input_pos-1-$prevIndex); # Match it searching for a string of backslash (i.e. \ or \\ or \\\ etc) # at the end of the string (my $escaped = $leftcontext) =~ m/(\\)*$/; From a2c83eb1bb9dff4bccd3b0e98a40bdf8f4cf58d8 Mon Sep 17 00:00:00 2001 From: Kyle Simpson Date: Tue, 21 Dec 2021 18:00:32 -0800 Subject: [PATCH 4/6] per #64, more performance tweaks --- json_minify/lib/JSON_minify.pm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/json_minify/lib/JSON_minify.pm b/json_minify/lib/JSON_minify.pm index a0b5bb5..8ea15b3 100644 --- a/json_minify/lib/JSON_minify.pm +++ b/json_minify/lib/JSON_minify.pm @@ -25,7 +25,7 @@ ## IN THE SOFTWARE. ## -our $VERSION = '2.0'; +our $VERSION = '3.0'; package JSON_minify; @@ -99,7 +99,7 @@ sub minify_string { my $tmp = substr $input_string, $index, $len; ## Eventually strip spaces - if (! $in_string && $strip_space) {$tmp =~ s/[[:space:]]*//gm;} + if (! $in_string && $strip_space) {$tmp =~ s/[[:space:]]+//gm;} # And add it in final result $new_str .= $tmp; } @@ -123,7 +123,7 @@ sub minify_string { my $leftcontext = substr($input_string, $prevIndex, $input_pos-1-$prevIndex); # Match it searching for a string of backslash (i.e. \ or \\ or \\\ etc) # at the end of the string - (my $escaped = $leftcontext) =~ m/(\\)*$/; + (my $escaped = $leftcontext) =~ m/(\\)+$/; # Get length of match my $escaped_full_len = length $& || ''; From df0db1700928b89d7ee644ead924c58b5b52765b Mon Sep 17 00:00:00 2001 From: Kyle Simpson Date: Tue, 21 Dec 2021 18:08:44 -0800 Subject: [PATCH 5/6] per #64, tweaking regex, trying to fix tests --- json_minify/lib/JSON_minify.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json_minify/lib/JSON_minify.pm b/json_minify/lib/JSON_minify.pm index 8ea15b3..784aa84 100644 --- a/json_minify/lib/JSON_minify.pm +++ b/json_minify/lib/JSON_minify.pm @@ -123,7 +123,7 @@ sub minify_string { my $leftcontext = substr($input_string, $prevIndex, $input_pos-1-$prevIndex); # Match it searching for a string of backslash (i.e. \ or \\ or \\\ etc) # at the end of the string - (my $escaped = $leftcontext) =~ m/(\\)+$/; + (my $escaped = $leftcontext) =~ m/(\\\\)+$/; # Get length of match my $escaped_full_len = length $& || ''; From 0f59902c3c89d9bafc94dc70c4834177ea7ae22c Mon Sep 17 00:00:00 2001 From: Kyle Simpson Date: Tue, 21 Dec 2021 18:16:21 -0800 Subject: [PATCH 6/6] Reverting "tweaking regex, trying to fix tests" This reverts commit df0db1700928b89d7ee644ead924c58b5b52765b. --- json_minify/lib/JSON_minify.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json_minify/lib/JSON_minify.pm b/json_minify/lib/JSON_minify.pm index 784aa84..8ea15b3 100644 --- a/json_minify/lib/JSON_minify.pm +++ b/json_minify/lib/JSON_minify.pm @@ -123,7 +123,7 @@ sub minify_string { my $leftcontext = substr($input_string, $prevIndex, $input_pos-1-$prevIndex); # Match it searching for a string of backslash (i.e. \ or \\ or \\\ etc) # at the end of the string - (my $escaped = $leftcontext) =~ m/(\\\\)+$/; + (my $escaped = $leftcontext) =~ m/(\\)+$/; # Get length of match my $escaped_full_len = length $& || '';