mirror of
https://github.com/revanced/revanced-integrations.git
synced 2025-01-07 10:35:49 +01:00
perf: return earlier when possible (#427)
This commit is contained in:
parent
b4c0619abb
commit
12f3f97552
@ -83,9 +83,10 @@ final class CustomFilterGroup extends StringFilterGroup {
|
|||||||
class ByteArrayFilterGroup extends FilterGroup<byte[]> {
|
class ByteArrayFilterGroup extends FilterGroup<byte[]> {
|
||||||
// Modified implementation from https://stackoverflow.com/a/1507813
|
// Modified implementation from https://stackoverflow.com/a/1507813
|
||||||
private int indexOf(final byte[] data, final byte[] pattern) {
|
private int indexOf(final byte[] data, final byte[] pattern) {
|
||||||
|
if (data.length == 0)
|
||||||
|
return -1;
|
||||||
// Computes the failure function using a boot-strapping process,
|
// Computes the failure function using a boot-strapping process,
|
||||||
// where the pattern is matched against itself.
|
// where the pattern is matched against itself.
|
||||||
|
|
||||||
final int[] failure = new int[pattern.length];
|
final int[] failure = new int[pattern.length];
|
||||||
|
|
||||||
int j = 0;
|
int j = 0;
|
||||||
@ -103,7 +104,6 @@ class ByteArrayFilterGroup extends FilterGroup<byte[]> {
|
|||||||
// KMP matching algorithm.
|
// KMP matching algorithm.
|
||||||
|
|
||||||
j = 0;
|
j = 0;
|
||||||
if (data.length == 0) return -1;
|
|
||||||
|
|
||||||
for (int i = 0; i < data.length; i++) {
|
for (int i = 0; i < data.length; i++) {
|
||||||
while (j > 0 && pattern[j] != data[i]) {
|
while (j > 0 && pattern[j] != data[i]) {
|
||||||
@ -130,7 +130,8 @@ class ByteArrayFilterGroup extends FilterGroup<byte[]> {
|
|||||||
public FilterGroupResult check(final byte[] bytes) {
|
public FilterGroupResult check(final byte[] bytes) {
|
||||||
var matched = false;
|
var matched = false;
|
||||||
for (byte[] filter : filters) {
|
for (byte[] filter : filters) {
|
||||||
if (indexOf(bytes, filter) == -1) continue;
|
if (indexOf(bytes, filter) == -1)
|
||||||
|
continue;
|
||||||
|
|
||||||
matched = true;
|
matched = true;
|
||||||
break;
|
break;
|
||||||
@ -181,7 +182,8 @@ abstract class FilterGroupList<V, T extends FilterGroup<V>> implements Iterable<
|
|||||||
|
|
||||||
protected boolean contains(final V stack) {
|
protected boolean contains(final V stack) {
|
||||||
for (T filterGroup : this) {
|
for (T filterGroup : this) {
|
||||||
if (!filterGroup.isEnabled()) continue;
|
if (!filterGroup.isEnabled())
|
||||||
|
continue;
|
||||||
|
|
||||||
var result = filterGroup.check(stack);
|
var result = filterGroup.check(stack);
|
||||||
if (result.isFiltered()) {
|
if (result.isFiltered()) {
|
||||||
@ -205,7 +207,8 @@ abstract class Filter {
|
|||||||
final protected ByteArrayFilterGroupList protobufBufferFilterGroups = new ByteArrayFilterGroupList();
|
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.
|
* @return True if filtered, false otherwise.
|
||||||
*/
|
*/
|
||||||
@ -232,34 +235,36 @@ abstract class Filter {
|
|||||||
@RequiresApi(api = Build.VERSION_CODES.N)
|
@RequiresApi(api = Build.VERSION_CODES.N)
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public final class LithoFilterPatch {
|
public final class LithoFilterPatch {
|
||||||
private static final Filter[] filters = new Filter[]{
|
private static final Filter[] filters = new Filter[] {
|
||||||
new DummyFilter() // Replaced by patch.
|
new DummyFilter() // Replaced by patch.
|
||||||
};
|
};
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public static boolean filter(final StringBuilder pathBuilder, final String identifier, final ByteBuffer protobufBuffer) {
|
public static boolean filter(final StringBuilder pathBuilder, final String identifier,
|
||||||
// TODO: Maybe this can be moved to the Filter class, to prevent unnecessary string creation
|
final ByteBuffer protobufBuffer) {
|
||||||
// because some filters might not need the path.
|
// 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();
|
var path = pathBuilder.toString();
|
||||||
|
|
||||||
// It is assumed that protobufBuffer is empty as well in this case.
|
// 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(
|
LogHelper.printDebug(() -> String.format(
|
||||||
"Searching (ID: %s, Buffer-size: %s): %s",
|
"Searching (ID: %s, Buffer-size: %s): %s",
|
||||||
identifier, protobufBuffer.remaining(), path
|
identifier, protobufBuffer.remaining(), path));
|
||||||
));
|
|
||||||
|
|
||||||
var protobufBufferArray = protobufBuffer.array();
|
var protobufBufferArray = protobufBuffer.array();
|
||||||
|
|
||||||
for (var filter : filters) {
|
for (var filter : filters) {
|
||||||
var filtered = filter.isFiltered(path, identifier, protobufBufferArray);
|
var filtered = filter.isFiltered(path, identifier, protobufBufferArray);
|
||||||
|
|
||||||
LogHelper.printDebug(() ->
|
LogHelper.printDebug(
|
||||||
String.format("%s (ID: %s): %s", filtered ? "Filtered" : "Unfiltered", identifier, path)
|
() -> String.format("%s (ID: %s): %s", filtered ? "Filtered" : "Unfiltered", identifier, path));
|
||||||
);
|
|
||||||
|
|
||||||
if (filtered) return true;
|
if (filtered)
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
Reference in New Issue
Block a user