summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2014-08-29 13:11:52 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2014-08-29 13:11:52 -0400
commit9d25eae8eec8c090339a01c03445bdf04a0840f5 (patch)
treed2184236c405081be62635264719b2971415adf9
parenta95b0a5b6e132e44bcc9cc4429526533ab964787 (diff)
downloadsqlalchemy-9d25eae8eec8c090339a01c03445bdf04a0840f5.tar.gz
- use a faster discard when loading
- don't do a bool on identity map since it calls __len__
-rw-r--r--lib/sqlalchemy/orm/identity.py13
-rw-r--r--lib/sqlalchemy/orm/session.py4
-rw-r--r--lib/sqlalchemy/orm/state.py3
3 files changed, 16 insertions, 4 deletions
diff --git a/lib/sqlalchemy/orm/identity.py b/lib/sqlalchemy/orm/identity.py
index 0fa541194..24dd47859 100644
--- a/lib/sqlalchemy/orm/identity.py
+++ b/lib/sqlalchemy/orm/identity.py
@@ -187,6 +187,12 @@ class WeakInstanceDict(IdentityMap):
return list(self._dict.values())
def discard(self, state):
+ st = self._dict.pop(state.key, None)
+ if st:
+ assert st is state
+ self._manage_removed_state(state)
+
+ def safe_discard(self, state):
if state.key in self._dict:
st = self._dict[state.key]
if st is state:
@@ -259,6 +265,13 @@ class StrongInstanceDict(IdentityMap):
state._instance_dict = self._wr
def discard(self, state):
+ obj = self._dict.pop(state.key, None)
+ if obj is not None:
+ self._manage_removed_state(state)
+ st = attributes.instance_state(obj)
+ assert st is state
+
+ def safe_discard(self, state):
if state.key in self._dict:
obj = self._dict[state.key]
st = attributes.instance_state(obj)
diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py
index 036045dba..52873eb34 100644
--- a/lib/sqlalchemy/orm/session.py
+++ b/lib/sqlalchemy/orm/session.py
@@ -1427,10 +1427,10 @@ class Session(_SessionClassMethods):
if state.key is None:
state.key = instance_key
elif state.key != instance_key:
- # primary key switch. use discard() in case another
+ # primary key switch. use safe_discard() in case another
# state has already replaced this one in the identity
# map (see test/orm/test_naturalpks.py ReversePKsTest)
- self.identity_map.discard(state)
+ self.identity_map.safe_discard(state)
if state in self.transaction._key_switches:
orig_key = self.transaction._key_switches[state][0]
else:
diff --git a/lib/sqlalchemy/orm/state.py b/lib/sqlalchemy/orm/state.py
index fe8ccd222..17872a9b4 100644
--- a/lib/sqlalchemy/orm/state.py
+++ b/lib/sqlalchemy/orm/state.py
@@ -58,7 +58,6 @@ class InstanceState(interfaces.InspectionAttr):
expired = False
deleted = False
_load_pending = False
-
is_instance = True
def __init__(self, obj, manager):
@@ -221,7 +220,7 @@ class InstanceState(interfaces.InspectionAttr):
def _cleanup(self, ref):
instance_dict = self._instance_dict()
- if instance_dict:
+ if instance_dict is not None:
instance_dict.discard(self)
self.callables.clear()