From 12f3f975525863e593216ecf36ca817d162474e1 Mon Sep 17 00:00:00 2001 From: Jim Man <58356745+jimman2003@users.noreply.github.com> Date: Tue, 27 Jun 2023 04:28:29 +0300 Subject: [PATCH] perf: return earlier when possible (#427) --- .../patches/components/LithoFilterPatch.java | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/patches/components/LithoFilterPatch.java b/app/src/main/java/app/revanced/integrations/patches/components/LithoFilterPatch.java index 314dabcb..95c68b41 100644 --- a/app/src/main/java/app/revanced/integrations/patches/components/LithoFilterPatch.java +++ b/app/src/main/java/app/revanced/integrations/patches/components/LithoFilterPatch.java @@ -83,9 +83,10 @@ final class CustomFilterGroup extends StringFilterGroup { class ByteArrayFilterGroup extends FilterGroup { // Modified implementation from https://stackoverflow.com/a/1507813 private int indexOf(final byte[] data, final byte[] pattern) { + if (data.length == 0) + return -1; // Computes the failure function using a boot-strapping process, // where the pattern is matched against itself. - final int[] failure = new int[pattern.length]; int j = 0; @@ -103,7 +104,6 @@ class ByteArrayFilterGroup extends FilterGroup { // KMP matching algorithm. j = 0; - if (data.length == 0) return -1; for (int i = 0; i < data.length; i++) { while (j > 0 && pattern[j] != data[i]) { @@ -130,7 +130,8 @@ class ByteArrayFilterGroup extends FilterGroup { public FilterGroupResult check(final byte[] bytes) { var matched = false; for (byte[] filter : filters) { - if (indexOf(bytes, filter) == -1) continue; + if (indexOf(bytes, filter) == -1) + continue; matched = true; break; @@ -181,7 +182,8 @@ abstract class FilterGroupList> implements Iterable< protected boolean contains(final V stack) { for (T filterGroup : this) { - if (!filterGroup.isEnabled()) continue; + if (!filterGroup.isEnabled()) + continue; var result = filterGroup.check(stack); if (result.isFiltered()) { @@ -205,7 +207,8 @@ abstract class Filter { final protected ByteArrayFilterGroupList protobufBufferFilterGroups = new ByteArrayFilterGroupList(); /** - * Check if the given path, identifier or protobuf buffer is filtered by any {@link FilterGroup}. + * Check if the given path, identifier or protobuf buffer is filtered by any + * {@link FilterGroup}. * * @return True if filtered, false otherwise. */ @@ -232,34 +235,36 @@ abstract class Filter { @RequiresApi(api = Build.VERSION_CODES.N) @SuppressWarnings("unused") public final class LithoFilterPatch { - private static final Filter[] filters = new Filter[]{ - new DummyFilter() // Replaced by patch. + private static final Filter[] filters = new Filter[] { + new DummyFilter() // Replaced by patch. }; @SuppressWarnings("unused") - public static boolean filter(final StringBuilder pathBuilder, final String identifier, final ByteBuffer protobufBuffer) { - // TODO: Maybe this can be moved to the Filter class, to prevent unnecessary string creation - // because some filters might not need the path. + public static boolean filter(final StringBuilder pathBuilder, final String identifier, + final ByteBuffer protobufBuffer) { + // TODO: Maybe this can be moved to the Filter class, to prevent unnecessary + // string creation + // because some filters might not need the path. var path = pathBuilder.toString(); // It is assumed that protobufBuffer is empty as well in this case. - if (path.isEmpty()) return false; + if (path.isEmpty()) + return false; LogHelper.printDebug(() -> String.format( "Searching (ID: %s, Buffer-size: %s): %s", - identifier, protobufBuffer.remaining(), path - )); + identifier, protobufBuffer.remaining(), path)); var protobufBufferArray = protobufBuffer.array(); for (var filter : filters) { var filtered = filter.isFiltered(path, identifier, protobufBufferArray); - LogHelper.printDebug(() -> - String.format("%s (ID: %s): %s", filtered ? "Filtered" : "Unfiltered", identifier, path) - ); + LogHelper.printDebug( + () -> String.format("%s (ID: %s): %s", filtered ? "Filtered" : "Unfiltered", identifier, path)); - if (filtered) return true; + if (filtered) + return true; } return false;