Skip to content

Commit

Permalink
Add module lazy loading in __init__ files (#87)
Browse files Browse the repository at this point in the history
This commit was made to avoid loading all of the tables when not required. This brings down the memory usage from ~400Mib to ~2MiB

Co-authored-by: Beni Reydman <[email protected]>
  • Loading branch information
JAM-Beni and BeniReydman authored Mar 7, 2024
1 parent eeb723e commit 2c24d77
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
31 changes: 25 additions & 6 deletions python/phevaluator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
"""Package for evaluating a poker hand."""
from . import hash as hash_ # FIXME: `hash` collides to built-in function
from . import tables
from .card import Card
from .evaluator import _evaluate_cards, evaluate_cards
from .evaluator_omaha import _evaluate_omaha_cards, evaluate_omaha_cards
from .utils import sample_cards

__all__ = ["tables", "Card", "evaluate_cards", "evaluate_omaha_cards", "sample_cards"]

# import mapping to objects in other modules
all_by_module = {
"phevaluator": ["card", "evaluator_omaha", "evaluator", "hash", "tables", "utils"],
"phevaluator.card": ["Card"],
"phevaluator.evaluator": ["_evaluate_cards", "evaluate_cards"],
"phevaluator.evaluator_omaha": ["_evaluate_omaha_cards", "evaluate_omaha_cards"],
"phevaluator.utils": ["sample_cards"]
}

# Based on werkzeug library
object_origins = {}
for module, items in all_by_module.items():
for item in items:
object_origins[item] = module

__docformat__ = "google"


# Based on https://peps.python.org/pep-0562/ and werkzeug library
def __getattr__(name):
"""lazy submodule imports"""
if name in object_origins:
module = __import__(object_origins[name], None, None, [name])
return getattr(module, name)
else:
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
44 changes: 24 additions & 20 deletions python/phevaluator/tables/__init__.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
"""Pre-calculated tables."""
from .dptables import CHOOSE, DP, SUITS
from .hashtable import FLUSH
from .hashtable5 import NO_FLUSH_5
from .hashtable6 import NO_FLUSH_6
from .hashtable7 import NO_FLUSH_7
from .hashtable_omaha import FLUSH_OMAHA, NO_FLUSH_OMAHA
# import mapping to pre-calculated tables in other modules
all_by_module = {
"phevaluator.tables.dptables": ["CHOOSE", "DP", "SUITS"],
"phevaluator.tables.hashtable": ["FLUSH"],
"phevaluator.tables.hashtable5": ["NO_FLUSH_5"],
"phevaluator.tables.hashtable6": ["NO_FLUSH_6"],
"phevaluator.tables.hashtable7": ["NO_FLUSH_7"],
"phevaluator.tables.hashtable_omaha": ["FLUSH_OMAHA", "NO_FLUSH_OMAHA"]
}

__all__ = [
"BINARIES_BY_ID",
"CHOOSE",
"DP",
"SUITBIT_BY_ID",
"SUITS",
"FLUSH",
"NO_FLUSH_5",
"NO_FLUSH_6",
"NO_FLUSH_7",
"FLUSH_OMAHA",
"NO_FLUSH_OMAHA",
]
# Based on werkzeug library
object_origins = {}
for module, items in all_by_module.items():
for item in items:
object_origins[item] = module

# fmt: off
BINARIES_BY_ID = [
Expand All @@ -39,3 +33,13 @@

SUITBIT_BY_ID = [0x1, 0x8, 0x40, 0x200] * 13
# fmt: on


# Based on https://peps.python.org/pep-0562/ and werkzeug library
def __getattr__(name):
"""lazy submodule imports"""
if name in object_origins:
module = __import__(object_origins[name], None, None, [name])
return getattr(module, name)
else:
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

0 comments on commit 2c24d77

Please sign in to comment.