From 3dea670091b0fc4a20d623c928292f7bd94892d8 Mon Sep 17 00:00:00 2001 From: syeopite Date: Mon, 19 Jul 2021 21:30:41 -0700 Subject: [PATCH] Switch to structs in extractors.cr for performance --- src/invidious/helpers/extractors.cr | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/invidious/helpers/extractors.cr b/src/invidious/helpers/extractors.cr index e8226888..68e84850 100644 --- a/src/invidious/helpers/extractors.cr +++ b/src/invidious/helpers/extractors.cr @@ -27,7 +27,7 @@ end # They're accessed through the process() method which validates the given data as applicable # to their specific struct and then use the internal parse() method to assemble the struct # specific to their category. -private class ItemParser +private abstract struct ItemParser # Base type for all item parsers. def process(item : JSON::Any, author_fallback : AuthorFallback) end @@ -36,7 +36,7 @@ private class ItemParser end end -private class VideoParser < ItemParser +private struct VideoParser < ItemParser def process(item, author_fallback) if item_contents = (item["videoRenderer"]? || item["gridVideoRenderer"]?) return self.parse(item_contents, author_fallback) @@ -94,7 +94,7 @@ private class VideoParser < ItemParser end end -private class ChannelParser < ItemParser +private struct ChannelParser < ItemParser def process(item, author_fallback) if item_contents = (item["channelRenderer"]? || item["gridChannelRenderer"]?) return self.parse(item_contents, author_fallback) @@ -125,7 +125,7 @@ private class ChannelParser < ItemParser end end -private class GridPlaylistParser < ItemParser +private struct GridPlaylistParser < ItemParser def process(item, author_fallback) if item_contents = item["gridPlaylistRenderer"]? return self.parse(item_contents, author_fallback) @@ -151,7 +151,7 @@ private class GridPlaylistParser < ItemParser end end -private class PlaylistParser < ItemParser +private struct PlaylistParser < ItemParser def process(item, author_fallback) if item_contents = item["playlistRenderer"]? return self.parse(item_contents, author_fallback) @@ -195,7 +195,7 @@ private class PlaylistParser < ItemParser end end -private class CategoryParser < ItemParser +private struct CategoryParser < ItemParser def process(item, author_fallback) if item_contents = item["shelfRenderer"]? return self.parse(item_contents, author_fallback) @@ -262,7 +262,7 @@ end # a structure we can more easily use via the parsers above. Their internals are # identical to the item parsers. -private class ItemsContainerExtractor +private abstract struct ItemsContainerExtractor def process(item : Hash(String, JSON::Any)) end @@ -270,7 +270,7 @@ private class ItemsContainerExtractor end end -private class YoutubeTabsExtractor < ItemsContainerExtractor +private struct YoutubeTabsExtractor < ItemsContainerExtractor def process(initial_data) if target = initial_data["twoColumnBrowseResultsRenderer"]? self.extract(target) @@ -304,7 +304,7 @@ private class YoutubeTabsExtractor < ItemsContainerExtractor end end -private class SearchResultsExtractor < ItemsContainerExtractor +private struct SearchResultsExtractor < ItemsContainerExtractor def process(initial_data) if target = initial_data["twoColumnSearchResultsRenderer"]? self.extract(target) @@ -326,7 +326,7 @@ private class SearchResultsExtractor < ItemsContainerExtractor end end -private class ContinuationExtractor < ItemsContainerExtractor +private struct ContinuationExtractor < ItemsContainerExtractor def process(initial_data) if target = initial_data["continuationContents"]? self.extract(target)