#12941: Cache should be cleared on cloning
----------------------------------------+-----------------------------------
Reporter: hivert | Owner: hivert
Type: defect | Status: new
Priority: major | Milestone: sage-5.1
Component: combinatorics | Resolution:
Keywords: Clone, cache, days38 | Work issues:
Report Upstream: N/A | Reviewers:
Authors: Florent Hivert | Merged in:
Dependencies: | Stopgaps:
----------------------------------------+-----------------------------------
Comment (by hivert):
{{{#!rst
Hi Simon,
Thanks for your clear and comprehensive explanations !
Cloning
~~~~~~~
I think I need to explain a little what we want to achieve with the clone
protocol. In Sage, we mostly models mathematical objects which are by
essence
immutable. However, in many occasions, we wants to construct a data-
structure
encoding a new mathematical object by small modifications of the data
structure encoding some already built object. For the resulting data-
structure
to correctly encode the mathematical object, some structural invariants
must
hold. One problem is that, in many cases, during the modification process,
there is no possibility but to break the invariants.
Think about applying a transposition on a permutation without atomic
parallel
assignment. In the middle of the various assignment the object is not a
permutations.
So to summarize:
- I'm starting with a valid immutable object ``a``;
- I'm cloning ``a`` to a new temporarily mutable object ``b``.
- I'm modifying ``b``; during the modification ``b`` is likely to be
**broken**. It's the user responsibility to make sure that methods calls
in
that state are correct.
- At the end of the modification, ``b`` is restored in an immutable state
and
it is semantically checked. Any methods should now work as for object
``a``.
Syntactically speaking you write the following::
with a.clone() as b:
...
# lot of invariant breaking modifications on b
...
# b is checked and immutable
Note that this is equivalent to::
b = a.__copy__()
...
# lot of invariant breaking modifications on b
...
b.set_immutable()
b.check()
Copying for Cloning
~~~~~~~~~~~~~~~~~~~
Since in code with clones there could be lot of copying on small objects,
I
don't want copy to be slow. In particular, in most current usecases (there
are
currently admittedly not that much of them), I'm performing the copy at
the
Cython/C level. Dumping the object in a string (or any temporary Python
structures) and creating a new object from that is in my opinion not an
option, as the whole purpose of clone is to avoid unnecessary copies. So
this
rule out copying by pickling and therefore your nice
``ClearCacheOnPickle``
feature.
Caching an mutability
~~~~~~~~~~~~~~~~~~~~~
I would rather have the following protocol:
- I'm Ok to implement a ``copyAndClearTheCache`` at the level of
``Element``
or ``ClonableElement``. Any support from the cache mechanism which
makes it
as fast as possible is welcome.
- On the second stage I don't want to clear the cache upon any
modification as
they are supposed to be elementary (eg: modifying an array entry). As a
consequence, I'd like to have a hook which **deactivate any caching on
mutable objects**. Caching will be be reactivated once the object is set
immutable. Do you think that's feasible on the ``CachedMethod`` side
while
keeping the overhead as low as possible ?
Currently all sage mutable objects including not only clones but matrices
and
vectors have a method ``_is_immutable``. Vectors and matrices implements
their own caching mechanism, but I think having cached method work with
them
would be good.
What do you think ?
Cheers,
Florent
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/12941#comment:14>
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 post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sage-trac?hl=en.