From c2059bddf091e84309c39a978b20eb25ee3bfad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 13 Jun 2020 09:48:03 +0200 Subject: [PATCH] Use custom class to store record data By using a class with slots instead of standard dict, we can reduce a lot the memory usage to store the record data in the caches and values. Also we can rely on the data in the cache to exist only for real field which can be used to create a fast path in ModelStorage.__getattr__. As now storing data in the cache has no extra cost, we can store data from the transactional cache in the local. This allow also to have a shorter path in ModelStorage.__getattr__. For more memory efficiency, we remove the record from the cache instead of clearing them. It was better to clear a dict but a record is less expensive to re-instantiate than clearing. issue9369 issue9225 review297861002 --- party.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/party.py b/party.py index 896c8b2a24..a383ff6b9c 100644 --- a/party.py +++ b/party.py @@ -163,17 +163,15 @@ class RelationAll(Relation, ModelView): # Clean local cache for record in all_records: for record_id in (record.id, record.reverse_id): - local_cache = record._local_cache.get(record_id) - if local_cache: - local_cache.clear() + record._local_cache.pop(record_id, None) # Clean cursor cache for cache in Transaction().cache.values(): if cls.__name__ in cache: + cache_cls = cache[cls.__name__] for record in all_records: for record_id in (record.id, record.reverse_id): - if record_id in cache[cls.__name__]: - cache[cls.__name__][record_id].clear() + cache_cls.pop(record_id, None) actions = iter(args) args = [] @@ -213,10 +211,10 @@ class RelationAll(Relation, ModelView): for cache in (cache, list(cache.get('_language_cache', {}).values())): if cls.__name__ in cache: + cache_cls = cache[cls.__name__] for record in relations: for record_id in (record.id, record.reverse_id): - if record_id in cache[cls.__name__]: - del cache[cls.__name__][record_id] + cache_cls.pop(record_id, None) Relation.delete(cls.convert_instances(relations))