#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, Nils   |    Reviewers:  Simon King
  Bruin                              |  Work issues:
Report Upstream:  None of the above  |       Commit:
  - read trac for reasoning.         |  851cc9522dde332561101f1c84182a0a84b8eed4
         Branch:                     |     Stopgaps:
  u/SimonKing/ticket/13394           |
   Dependencies:                     |
-------------------------------------+-------------------------------------

Comment (by SimonKing):

 This version of your test (using a python wrapper for the cdef function)
 needs 12.5 CPU-seconds on my laptop. I think that's fine for a long test.
 {{{
 def test_del_dictitem_by_exact_value(D, value, h):
     """
     This function helps testing some cdef function used to delete
 dictionary items.

     INPUT:

     - ``D`` -- a Python ``<dict>``.
     - ``value`` -- an object that is value ``D``.
     - ``h`` -- the hash of the key under which to find ``value`` in ``D``.

     <Further doc to be added>

     TESTS:

     See :trac:`13394` for a discussion.
     ::

         sage: from sage.misc.weak_dict import
 test_del_dictitem_by_exact_value
         sage: B=1000
         sage: L=list(range(B))
         sage: D1=dict()
         sage: D2=dict()
         sage: for i in range(100000):        # optional: long
         ....:     ki=L[floor(random()*B)]
         ....:     vi=L[floor(random()*B)]
         ....:     D1[ki]=vi
         ....:     D2[ki]=vi
         ....:     ko=L[floor(random()*B)]
         ....:     if ko in D1:
         ....:         vo=D1[ko]
         ....:         del D1[ko]
         ....:         test_del_dictitem_by_exact_value(D2,vo,hash(ko))
         ....:     assert D1 == D2

     """
     return del_dictitem_by_exact_value(<PyDictObject *>D, <PyObject
 *>value, h)
 }}}
 The following down-sized indirect test takes about 2 seconds and is thus
 fine for a non-long test:
 {{{
         sage: from sage.misc.weak_dict import WeakValueDictionary
         sage: class C(object):
         ....:     def __init__(self, n):
         ....:         self.n = n
         ....:     def __cmp__(self, other):
         ....:         return cmp(type(self),type(other)) or cmp(self.n,
 other.n)
         sage: B = 100
         sage: L = [None]*B
         sage: D1 = WeakValueDictionary()
         sage: D2 = WeakValueDictionary()
         sage: import gc
         sage: for i in range(10000):
         ....:     ki = floor(random()*B)
         ....:     vi = C(floor(random()*B))
         ....:     D1[ki] = vi
         ....:     D2[ki] = vi
         ....:     L[ki]  = vi
         ....:     del vi
         ....:     ko = floor(random()*B)
         ....:     if ko in D1:
         ....:         del D1[ko]
         ....:         L[ko] = None
         ....:     assert D1 == D2
 }}}

--
Ticket URL: <http://trac.sagemath.org/ticket/13394#comment:78>
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