From 0f0dc0a81836156a89b7fef95f8585ef536fa81a Mon Sep 17 00:00:00 2001 From: Mix <32300164+mnixry@users.noreply.github.com> Date: Thu, 17 Feb 2022 23:37:28 +0800 Subject: [PATCH] :zap: improve full match performance with frozenset --- nonebot/rule.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/nonebot/rule.py b/nonebot/rule.py index 60e6035e..2e0b0c9d 100644 --- a/nonebot/rule.py +++ b/nonebot/rule.py @@ -182,20 +182,15 @@ class FullmatchRule: __slots__ = ("msg", "ignorecase") def __init__(self, msg: Tuple[str, ...], ignorecase: bool = False): - self.msg = msg + self.msg = frozenset(map(str.casefold, msg) if ignorecase else msg) self.ignorecase = ignorecase async def __call__( - self, type: str = EventType(), text: str = EventPlainText() + self, type_: str = EventType(), text: str = EventPlainText() ) -> bool: - if type != "message": - return False - return bool( - text - and any( - full.lower() == text.lower() if self.ignorecase else full == text - for full in self.msg - ) + return ( + type_ == "message" + and (text.casefold() if self.ignorecase else text) in self.msg )