Skip to content

Commit 069ce0f

Browse files
committed
(improvement) metadata: remove __dict__ from __slots__ for real memory savings, make _RowView a Mapping
- Remove '__dict__' and '__weakref__' from __slots__ on all metadata classes. All dynamic attribute assignments target attributes already declared in __slots__, so this is safe and gives real per-instance savings (~264 bytes per ColumnMetadata, ~516 KB for 2000-column schemas). - Make _RowView inherit from collections.abc.Mapping and add __iter__/__len__, giving it a complete Mapping interface (keys/values/items for free). - Add maintainability comment to _SELECT_COLUMNS noting coupling with _build_column_metadata.
1 parent 12defc1 commit 069ce0f

1 file changed

Lines changed: 15 additions & 18 deletions

File tree

cassandra/metadata.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@
5252
from cassandra.util import maybe_add_timeout_to_query
5353

5454

55-
class _RowView(object):
55+
class _RowView(Mapping):
5656
"""
5757
Lightweight read-only view over a row tuple, supporting dict-like access.
5858
Shares a single index map across all rows from the same result set,
5959
avoiding per-row dict allocation overhead.
60+
61+
Implements the :class:`collections.abc.Mapping` protocol, providing
62+
``__getitem__``, ``__iter__``, ``__len__``, ``get``, ``keys``,
63+
``values``, ``items``, and ``__contains__`` for free.
6064
"""
6165

6266
__slots__ = ("_row", "_index_map")
@@ -68,6 +72,12 @@ def __init__(self, row, index_map):
6872
def __getitem__(self, key):
6973
return self._row[self._index_map[key]]
7074

75+
def __iter__(self):
76+
return iter(self._index_map)
77+
78+
def __len__(self):
79+
return len(self._index_map)
80+
7181
def get(self, key, default=None):
7282
idx = self._index_map.get(key)
7383
if idx is not None:
@@ -1132,8 +1142,6 @@ class KeyspaceMetadata(object):
11321142
"virtual",
11331143
"graph_engine",
11341144
"_exc_info",
1135-
"__dict__",
1136-
"__weakref__",
11371145
)
11381146

11391147
def __init__(
@@ -1306,8 +1314,6 @@ class UserType(object):
13061314
"name",
13071315
"field_names",
13081316
"field_types",
1309-
"__dict__",
1310-
"__weakref__",
13111317
)
13121318

13131319
def __init__(self, keyspace, name, field_names, field_types):
@@ -1389,8 +1395,6 @@ class Aggregate(object):
13891395
"initial_condition",
13901396
"return_type",
13911397
"deterministic",
1392-
"__dict__",
1393-
"__weakref__",
13941398
)
13951399

13961400
def __init__(
@@ -1509,8 +1513,6 @@ class Function(object):
15091513
"deterministic",
15101514
"monotonic",
15111515
"monotonic_on",
1512-
"__dict__",
1513-
"__weakref__",
15141516
)
15151517

15161518
def __init__(
@@ -1647,8 +1649,6 @@ class TableMetadata(object):
16471649
"_exc_info",
16481650
"virtual",
16491651
"extensions",
1650-
"__dict__",
1651-
"__weakref__",
16521652
)
16531653

16541654
compaction_options = {
@@ -2098,8 +2098,6 @@ class ColumnMetadata(object):
20982098
"is_static",
20992099
"is_reversed",
21002100
"_cass_type",
2101-
"__dict__",
2102-
"__weakref__",
21032101
)
21042102

21052103
def __init__(
@@ -2140,8 +2138,6 @@ class IndexMetadata(object):
21402138
"name",
21412139
"kind",
21422140
"index_options",
2143-
"__dict__",
2144-
"__weakref__",
21452141
)
21462142

21472143
def __init__(self, keyspace_name, table_name, index_name, kind, index_options):
@@ -2391,7 +2387,7 @@ class TriggerMetadata(object):
23912387
table.
23922388
"""
23932389

2394-
__slots__ = ("table", "name", "options", "__dict__", "__weakref__")
2390+
__slots__ = ("table", "name", "options")
23952391

23962392
def __init__(self, table_metadata, trigger_name, options=None):
23972393
self.table = table_metadata
@@ -3193,6 +3189,9 @@ class SchemaParserV3(SchemaParserV22):
31933189

31943190
_SELECT_KEYSPACES = "SELECT * FROM system_schema.keyspaces"
31953191
_SELECT_TABLES = "SELECT * FROM system_schema.tables"
3192+
# Only fetch the columns used by _build_column_metadata / _build_table_columns.
3193+
# Keep this list in sync with those methods; subclasses that need extra
3194+
# columns should override _SELECT_COLUMNS.
31963195
_SELECT_COLUMNS = "SELECT keyspace_name, table_name, column_name, clustering_order, kind, position, type FROM system_schema.columns"
31973196
_SELECT_INDEXES = "SELECT * FROM system_schema.indexes"
31983197
_SELECT_TRIGGERS = "SELECT * FROM system_schema.triggers"
@@ -4324,8 +4323,6 @@ class MaterializedViewMetadata(object):
43244323
"where_clause",
43254324
"options",
43264325
"extensions",
4327-
"__dict__",
4328-
"__weakref__",
43294326
)
43304327

43314328
def __init__(

0 commit comments

Comments
 (0)