#13394: Write a WeakValueDictionary with safer key removal
-------------------------------------+-------------------------------------
       Reporter:  nbruin             |        Owner:  rlm
           Type:  enhancement        |       Status:  needs_review
       Priority:  major              |    Milestone:  sage-5.13
      Component:  memleak            |   Resolution:
       Keywords:                     |    Merged in:
        Authors:  Simon King         |    Reviewers:
Report Upstream:  None of the above  |  Work issues:
  - read trac for reasoning.         |       Commit:
         Branch:                     |  70a7b8accb2d2156f315cbae2511985936977670
  u/SimonKing/ticket/13394           |     Stopgaps:
   Dependencies:                     |
-------------------------------------+-------------------------------------

Comment (by nbruin):

 I couldn't resist trying to implement our extra primitive straight onto
 python's dict. It turns out to be fairly straightforward after reading
 `lookdict_string` and `PyDict_DelItem` in Python's `Objects/dictobjec.c`.
 As an example:
 {{{
 sage: %attach deldict_exact.pyx
 Compiling ./deldict_exact.pyx...
 sage: D={}
 sage: L=[]
 sage: for n in prime_range(2,100):
 ....:         R=Integers(n)
 ....:         k=R(1)
 ....:         L.append(k)
 ....:         D[R(1)]=n
 ....:
 sage: len(D)
 25
 sage: init_dummy()
 sage: for l in L[3:10]:
 ....:         del_dictitem_exact(D,l,hash(l))
 ....:
 sage: len(D)
 18
 sage: set(L[:3]+ L[10:]) == set(D.keys())
 True
 }}}
 So the interface is: `del_dictitem_exact(D,key,hash)`, where `hash` is
 `hash(key)` (but is separately supplied for efficiency). This routine
 removes the entry `D[key]` if `key` identically occurs as a key in `D`. We
 could match on value too if we want, but these semantics make most sense
 for a dictionary.

 Problematic/hackish things:
  - I have hardcoded PERTURB_SHIFT since that constant isn't available
 outside `dictobject.c`.
  - I had to write a programmatic routine to go looking for the `dummy`
 sentinel key value (which is used to mark deleted entries)
  - I am relying on the internals of `PyDictEntry` and `PyDictObject`.
 However, I'm getting those from `Python.h`, so we shouldn't get silent
 failure on those.

 With this extra primitive you could write a safer callback on
 `WeakValueDictionary` without needing the extra indirection. You'd call
 `init_dummy` upon instantiation of a !WeakValueDictionary, to ensure that
 it's initialized.

--
Ticket URL: <http://trac.sagemath.org/ticket/13394#comment:25>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to