From 88d94cad92fcf17bdb8cc0c68610ba8742313ce8 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Sun, 19 Aug 2018 23:19:08 -0700 Subject: [PATCH] Substantially improve Scrubber performance. Previously, we were making a new copy of the entire source string after every scrubbed substitution. In the case of our new, larger log files, this was very slow. It's been changed so we only ever create one new copy. In practice, on a Moto E (2014), scrubbing a 1.5MB log went from >4000ms to ~100ms. --- .../securesms/logsubmit/util/Scrubber.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/org/thoughtcrime/securesms/logsubmit/util/Scrubber.java b/src/org/thoughtcrime/securesms/logsubmit/util/Scrubber.java index e0a1ac118e..8f928bd6a9 100644 --- a/src/org/thoughtcrime/securesms/logsubmit/util/Scrubber.java +++ b/src/org/thoughtcrime/securesms/logsubmit/util/Scrubber.java @@ -46,18 +46,23 @@ public class Scrubber { Log.d(TAG, "scrubbing input"); String out = in; for (Pattern pattern : patterns) { - Matcher matcher = pattern.matcher(out); - while (matcher.find()) { + Matcher matcher = pattern.matcher(out); + StringBuilder builder = new StringBuilder(); + int lastEndingPos = 0; + + while (matcher.find()) { + builder.append(out.substring(lastEndingPos, matcher.start())); - StringBuilder builder = new StringBuilder(out.substring(0, matcher.start())); final String censored = matcher.group().substring(0,1) + new String(new char[matcher.group().length()-3]).replace("\0", "*") + matcher.group().substring(matcher.group().length()-2); builder.append(censored); - builder.append(out.substring(matcher.end())); + + lastEndingPos = matcher.end(); Log.i(TAG, "replacing a match on /" + pattern.toString() + "/ => " + censored); - out = builder.toString(); } + builder.append(out.substring(lastEndingPos)); + out = builder.toString(); } return out; }