From 51842314799c9a4cf2a759e76823fb2815caca9d Mon Sep 17 00:00:00 2001 From: litetex <40789489+litetex@users.noreply.github.com> Date: Thu, 10 Jul 2025 23:23:34 +0200 Subject: [PATCH 1/2] Do a quick check if the n parameter is even present before exec regex --- .../services/youtube/YoutubeThrottlingParameterUtils.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeThrottlingParameterUtils.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeThrottlingParameterUtils.java index f7a9bbbc5..86634f841 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeThrottlingParameterUtils.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeThrottlingParameterUtils.java @@ -196,6 +196,11 @@ final class YoutubeThrottlingParameterUtils { */ @Nullable static String getThrottlingParameterFromStreamingUrl(@Nonnull final String streamingUrl) { + // Do a quick check if the n parameter is even present, if not abort + // This improves performance by 60-900x + if (!streamingUrl.contains("&n=") && !streamingUrl.contains("?n=")) { + return null; + } try { return Parser.matchGroup1(THROTTLING_PARAM_PATTERN, streamingUrl); } catch (final Parser.RegexException e) { From 0dfaeb20fa6842a2d345174e2bc07c11de439c4d Mon Sep 17 00:00:00 2001 From: litetex <40789489+litetex@users.noreply.github.com> Date: Fri, 11 Jul 2025 20:12:50 +0200 Subject: [PATCH 2/2] Added doc for related code --- .../services/youtube/YoutubeThrottlingParameterUtils.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeThrottlingParameterUtils.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeThrottlingParameterUtils.java index 86634f841..ea9f107e9 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeThrottlingParameterUtils.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeThrottlingParameterUtils.java @@ -18,6 +18,8 @@ import java.util.regex.Pattern; */ final class YoutubeThrottlingParameterUtils { + // NOTE: When changing this you should also change the quick exit/shortcut + // in getThrottlingParameterFromStreamingUrl private static final Pattern THROTTLING_PARAM_PATTERN = Pattern.compile("[&?]n=([^&]+)"); private static final String SINGLE_CHAR_VARIABLE_REGEX = "[a-zA-Z0-9$_]";