From: Mike Bayer Date: Tue, 16 Jun 2009 19:23:43 +0000 (+0000) Subject: - Trimmed the pickle format for InstanceState which should further X-Git-Tag: rel_0_5_5~13 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=8a72a5b0bc5fe5a276623e40499dede629391d1c;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Trimmed the pickle format for InstanceState which should further reduce the memory footprint of pickled instances. The format should be backwards compatible with that of 0.5.4 and previous. --- diff --git a/CHANGES b/CHANGES index e031ade3cd..1d986020a2 100644 --- a/CHANGES +++ b/CHANGES @@ -45,7 +45,10 @@ CHANGES columns, has been enhanced such that the fk->itself aspect of the relation won't be used to determine relation direction. - + - Trimmed the pickle format for InstanceState which should further + reduce the memory footprint of pickled instances. The format + should be backwards compatible with that of 0.5.4 and previous. + - sql - Removed an obscure feature of execute() (including connection, engine, Session) whereby a bindparam() construct can be sent as diff --git a/lib/sqlalchemy/orm/state.py b/lib/sqlalchemy/orm/state.py index 6894b8f85f..1b7b3fbd54 100644 --- a/lib/sqlalchemy/orm/state.py +++ b/lib/sqlalchemy/orm/state.py @@ -118,36 +118,46 @@ class InstanceState(object): self.manager.events.run('on_load', instance) def __getstate__(self): - return {'key': self.key, - 'committed_state': self.committed_state, - 'pending': self.pending, - 'parents': self.parents, - 'modified': self.modified, - 'expired':self.expired, - 'load_options':self.load_options, - 'load_path':interfaces.serialize_path(self.load_path), - 'instance': self.obj(), - 'expired_attributes':self.expired_attributes, - 'callables': self.callables, - 'mutable_dict':self.mutable_dict} - + d = { + 'instance':self.obj(), + } + + d.update( + (k, self.__dict__[k]) for k in ( + 'committed_state', 'pending', 'parents', 'modified', 'expired', + 'callables' + ) if self.__dict__[k] + ) + + d.update( + (k, self.__dict__[k]) for k in ( + 'key', 'load_options', 'expired_attributes', 'mutable_dict' + ) if k in self.__dict__ + ) + if self.load_path: + d['load_path'] = interfaces.serialize_path(self.load_path) + return d + def __setstate__(self, state): - self.committed_state = state['committed_state'] - self.parents = state['parents'] - self.key = state['key'] - self.session_id = None - self.pending = state['pending'] - self.modified = state['modified'] self.obj = weakref.ref(state['instance']) - self.load_options = state['load_options'] or EMPTY_SET - self.load_path = interfaces.deserialize_path(state['load_path']) - self.class_ = self.obj().__class__ + self.class_ = state['instance'].__class__ self.manager = manager_of_class(self.class_) - self.callables = state['callables'] - self.runid = None - self.expired = state['expired'] - self.expired_attributes = state['expired_attributes'] - self.mutable_dict = state['mutable_dict'] + + self.committed_state = state.get('committed_state', {}) + self.pending = state.get('pending', {}) + self.parents = state.get('parents', {}) + self.modified = state.get('modified', False) + self.expired = state.get('expired', False) + self.callables = state.get('callables', {}) + + self.__dict__.update( + (k, state[k]) for k in ( + 'key', 'load_options', 'expired_attributes', 'mutable_dict' + ) if k in state + ) + + if 'load_path' in state: + self.load_path = interfaces.deserialize_path(state['load_path']) def initialize(self, key): self.manager.get_impl(key).initialize(self, self.dict)