Skip to content

Commit 2ab58f8

Browse files
committed
use slots to save memory
1 parent 00274f3 commit 2ab58f8

6 files changed

Lines changed: 25 additions & 8 deletions

File tree

dictdatabase/configuration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55

6-
@dataclass
6+
@dataclass(slots=True)
77
class Confuguration:
88
storage_directory: str = "ddb_storage"
99
indent: int | str | None = "\t" # eg. "\t" or 4 or None

dictdatabase/indexing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class Indexer:
3838
- value_hash: The hash of the value bytes
3939
"""
4040

41+
__slots__ = ("data", "path")
42+
4143
def __init__(self, db_name: str):
4244
# Make path of index file
4345
db_name = db_name.replace("/", "___")

dictdatabase/io_unsafe.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from . import config, utils, byte_codes, indexing, io_bytes
88

99

10-
@dataclass(frozen=True)
10+
@dataclass(frozen=True, slots=True)
1111
class PartialDict:
1212
prefix: bytes
1313
key: str
@@ -17,7 +17,7 @@ class PartialDict:
1717
suffix: bytes
1818

1919

20-
@dataclass(frozen=True)
20+
@dataclass(frozen=True, slots=True)
2121
class PartialFileHandle:
2222
db_name: str
2323
partial_dict: PartialDict

dictdatabase/locking.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,15 @@ class AbstractLock:
7979
An abstract lock doesn't do anything by itself. A subclass of it needs to
8080
call super().__init__(...) and then only exit __init__ when the lock is aquired.
8181
"""
82+
83+
__slots__ = ("id", "time_ns", "db_name", "need_path", "path", "ddb_dir")
84+
8285
id: str
8386
time_ns: int
8487
db_name: str
85-
need_path: str = None
86-
path: str = None
88+
need_path: str
89+
path: str
90+
ddb_dir: str
8791

8892
def __init__(self, db_name: str):
8993
"""
@@ -101,8 +105,7 @@ def _lock(self):
101105
def _unlock(self):
102106
for p in ("need_path", "path"):
103107
try:
104-
path = getattr(self, p, None)
105-
if path:
108+
if path := getattr(self, p, None):
106109
os.unlink(path)
107110
except FileNotFoundError:
108111
pass

dictdatabase/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,13 @@ def at(*path, key: str = None, where: Callable[[Any, Any], bool] = None) -> DDBM
8181

8282

8383
class DDBMethodChooser:
84+
85+
__slots__ = ("path", "key", "where", "op_type")
86+
8487
path: str
8588
key: str
8689
where: Callable[[Any, Any], bool]
90+
op_type: OperationType
8791

8892

8993
def __init__(self, path: tuple, key: str = None, where: Callable[[Any, Any], bool] = None):

dictdatabase/session.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,18 @@ class DDBSession(Generic[T]):
1818
the changes will be lost after exiting the with statement.
1919
"""
2020

21-
in_session: bool = False
21+
__slots__ = ("in_session", "db_name", "as_type", "where", "key", "op_type", "data_handle", "write_lock", "partial_handle", "original_data")
22+
23+
in_session: bool
24+
db_name: str
2225
as_type: T
26+
where: Callable[[Any, Any], bool]
27+
key: str
28+
op_type: Any
29+
2330

2431
def __init__(self, db_name: str, op_type, key: str = None, where: Callable[[Any, Any], bool] = None, as_type: T = None):
32+
self.in_session = False
2533
self.db_name = db_name
2634
self.as_type = as_type
2735
self.where = where

0 commit comments

Comments
 (0)