From 7f229490b89679b8fe575ad09595b19328e9f34d Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 8 Jun 2020 03:15:23 +0200 Subject: [PATCH] Optimize a few functions... I think. --- lihzahrd/enums/__init__.py | 2 ++ lihzahrd/enums/liquidtype.py | 12 +++++++++--- lihzahrd/enums/prefixtype.py | 2 ++ lihzahrd/{tiles => enums}/rleencoding.py | 8 +++++++- lihzahrd/tiles/__init__.py | 3 +-- lihzahrd/tiles/wiring.py | 14 +++++++++++--- 6 files changed, 32 insertions(+), 9 deletions(-) rename lihzahrd/{tiles => enums}/rleencoding.py (64%) diff --git a/lihzahrd/enums/__init__.py b/lihzahrd/enums/__init__.py index 7e82d03..119170d 100644 --- a/lihzahrd/enums/__init__.py +++ b/lihzahrd/enums/__init__.py @@ -4,6 +4,7 @@ from .itemtype import ItemType from .liquidtype import LiquidType from .walltype import WallType from .prefixtype import PrefixType +from .rleencoding import RLEEncoding __all__ = [ "BlockType", @@ -12,4 +13,5 @@ __all__ = [ "LiquidType", "WallType", "PrefixType", + "RLEEncoding", ] diff --git a/lihzahrd/enums/liquidtype.py b/lihzahrd/enums/liquidtype.py index a0b6801..6481955 100644 --- a/lihzahrd/enums/liquidtype.py +++ b/lihzahrd/enums/liquidtype.py @@ -1,4 +1,5 @@ import enum +import functools class LiquidType(enum.IntEnum): @@ -10,11 +11,16 @@ class LiquidType(enum.IntEnum): @classmethod def from_flags(cls, flags1): - if flags1[3] and flags1[4]: + return cls._from_flags(flags1[3], flags1[4]) + + @classmethod + @functools.lru_cache(4) + def _from_flags(cls, flags13, flags14): + if flags13 and flags14: return cls.HONEY - if flags1[4]: + if flags14: return cls.LAVA - if flags1[3]: + if flags13: return cls.WATER return cls.NO_LIQUID diff --git a/lihzahrd/enums/prefixtype.py b/lihzahrd/enums/prefixtype.py index ba7cc47..6e278f7 100644 --- a/lihzahrd/enums/prefixtype.py +++ b/lihzahrd/enums/prefixtype.py @@ -1,5 +1,6 @@ from typing import * import enum +import functools class PrefixType(enum.IntEnum): @@ -89,6 +90,7 @@ class PrefixType(enum.IntEnum): Legendary2 = 84 @classmethod + @functools.lru_cache(85) def get(cls, i: int) -> Optional["PrefixType"]: if i == 0: return None diff --git a/lihzahrd/tiles/rleencoding.py b/lihzahrd/enums/rleencoding.py similarity index 64% rename from lihzahrd/tiles/rleencoding.py rename to lihzahrd/enums/rleencoding.py index 48c3f51..40a9ec7 100644 --- a/lihzahrd/tiles/rleencoding.py +++ b/lihzahrd/enums/rleencoding.py @@ -1,4 +1,5 @@ import enum +import functools class RLEEncoding(enum.IntEnum): @@ -15,4 +16,9 @@ class RLEEncoding(enum.IntEnum): @classmethod def from_flags(cls, flags1): - return cls(flags1[7] * 2 + flags1[6]) + return cls._from_flags(flags1[6], flags1[7]) + + @classmethod + @functools.lru_cache(3) + def _from_flags(cls, flags16, flags17): + return cls(flags17 * 2 + flags16) diff --git a/lihzahrd/tiles/__init__.py b/lihzahrd/tiles/__init__.py index 73dc13e..b4190ef 100644 --- a/lihzahrd/tiles/__init__.py +++ b/lihzahrd/tiles/__init__.py @@ -1,4 +1,3 @@ -from .rleencoding import RLEEncoding from .shape import Shape from .wiring import Wiring from .frameimportantdata import FrameImportantData @@ -8,5 +7,5 @@ from .liquid import Liquid from .tile import Tile from .tilematrix import TileMatrix -__all__ = ["RLEEncoding", "Shape", "Wiring", "FrameImportantData", "Block", +__all__ = ["Shape", "Wiring", "FrameImportantData", "Block", "Wall", "Liquid", "Tile", "TileMatrix"] diff --git a/lihzahrd/tiles/wiring.py b/lihzahrd/tiles/wiring.py index 950c30d..a6516e5 100644 --- a/lihzahrd/tiles/wiring.py +++ b/lihzahrd/tiles/wiring.py @@ -1,3 +1,6 @@ +import functools + + class Wiring: """Wiring data for a certain tile.""" @@ -35,6 +38,11 @@ class Wiring: def from_flags(cls, flags2=None, flags3=None): if flags2 is not None: if flags3 is not None: - return cls(red=flags2[1], green=flags2[2], blue=flags2[3], yellow=flags3[5], actuator=flags3[1]) - return cls(red=flags2[1], green=flags2[2], blue=flags2[3]) - return cls() + return cls._from_flags(flags2[1], flags2[2], flags2[3], flags3[1], flags3[5]) + return cls._from_flags(flags2[1], flags2[2], flags2[3], False, False) + return cls._from_flags(False, False, False, False, False) + + @classmethod + @functools.lru_cache(32) + def _from_flags(cls, flags21, flags22, flags23, flags31, flags35): + return cls(red=flags21, green=flags22, blue=flags23, yellow=flags35, actuator=flags31)