From 32c7be659bcbd1cfdd7b42e4ff2a4934bd572be6 Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Wed, 8 Jan 2025 10:18:46 -0500 Subject: [PATCH] perf(commands/utils): use lru cache for matching --- commands/utils.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/commands/utils.py b/commands/utils.py index f005b85..20d8fb8 100644 --- a/commands/utils.py +++ b/commands/utils.py @@ -1,4 +1,5 @@ from enum import Enum +from functools import lru_cache import constants @@ -24,6 +25,7 @@ class Command(Enum): VOLUME = "volume" +@lru_cache def match_token(token: str) -> list[Command]: if token.lower() == "r": return [Command.RELOAD] @@ -44,11 +46,13 @@ def match_token(token: str) -> list[Command]: ) +@lru_cache def match(command: str) -> list[Command] | None: if tokens := tokenize(command): return match_token(tokens[0]) +@lru_cache def tokenize(string: str) -> list[str]: tokens = [] token = ""