|
I wish gc() can free the keys of a mapping, so I consider to make the mapping WEAK_INDICES.
But should I make this mapping WEAK_VALUES too? this is a question: If the value has a reference to its key, I should make the mapping WEAK_VALUES, or the key will not be freed by gc(), because the value has a non-weak reference from the mapping, and the key has a (non-weak) reference from the value. On the other hande, if the value not has any reference to its key, I should NOT make the mapping WEAK_VALUES, becouse if I do so, the value may be free before the key. My question is: I might not know whether the value has a reference to key, because the value is returned from another module that is not write by me. What should I do? Happy New Year Guo Xuesong |
|
Regardless whether you use WEAK_INDICES or WEAK_VALUES, the keypair,
i.e. the key and the value together, is always freed in one go. The flag only controls the trigger of that freeing. So for WEAK_VALUES it isn't relevant if the value has a ref to the key or not, but rather if you have other references to the value. You should only use WEAK_VALUES if your values will have other references to them, or otherwise they'll disappear too quickly from the mapping (along with their keys). Otoh, if you have other refs to your keys and you rely on WEAK_INDICES, then you have to be careful to make the optional ref to your key from your value weak, or else the key won't run out of non-weak refs when it should. Note also that values of simple data types, i.e. integers, floats and strings, aren't affected by weak references. So if the key is a string and the value may or may not have a reference to that string, then it doesn't matter if you have a weak flag on the index. I'm only saying that in case you have strings as keys and is confused by the fact that they are refcounted. That refcounting is insignificant in this regard. 郭雪松 <[hidden email]> wrote: > I wish gc() can free the keys of a mapping, so I consider to make the mapping > WEAK_INDICES. > > But should I make this mapping WEAK_VALUES too? this is a question: > > If the value has a reference to its key, I should make the mapping > WEAK_VALUES, or the key will not be freed by gc(), > because the value has a non-weak reference from the mapping, and the key has a > (non-weak) reference from the value. > > On the other hande, if the value not has any reference to its key, I should > NOT make the mapping WEAK_VALUES, > becouse if I do so, the value may be free before the key. > > My question is: I might not know whether the value has a reference to key, > because the value is returned from another module > that is not write by me. > > What should I do? > > Happy New Year > > Guo Xuesong |
|
> From: [hidden email]
> To: [hidden email] > CC: [hidden email] > Subject: Re: A question about Pike.WEAK_INDICES > Date: Sun, 1 Jan 2012 17:25:45 +0100 > > Regardless whether you use WEAK_INDICES or WEAK_VALUES, the keypair, > i.e. the key and the value together, is always freed in one go. The flag > only controls the trigger of that freeing. I known it, What I wish is the keypair to be freed when and when only the
other references to the key become to zero. If there are references from value
to key, they should not count into the references count. > > So for WEAK_VALUES it isn't relevant if the value has a ref to the key > or not, but rather if you have other references to the value. You should > only use WEAK_VALUES if your values will have other references to them, > or otherwise they'll disappear too quickly from the mapping (along with > their keys). In my case there is no other reference to the value.
> > Otoh, if you have other refs to your keys and you rely on WEAK_INDICES, > then you have to be careful to make the optional ref to your key from > your value weak, or else the key won't run out of non-weak refs when it > should. This is the point. My problem is I passed the key to a function that is writen
by another peaple, for example function 'object f1(object key)', I don't known
whether the return value has a reference to key. And the person who wrote
f1 don't known I will use the return value of f1 in this way. so It's most likely
he would not be careful to make the optional ref to key from the return value
fo f1 weak.
I means current design of WEAK_INDICES/WEAK_VALUES is not friendly to
module split. There should be some mechanism to free the keypair when and
when only the other non-weak references run out, not count the references
from value regardless weak or not. > > Note also that values of simple data types, i.e. integers, floats and > strings, aren't affected by weak references. So if the key is a string > and the value may or may not have a reference to that string, then it > doesn't matter if you have a weak flag on the index. I'm only saying > that in case you have strings as keys and is confused by the fact that > they are refcounted. That refcounting is insignificant in this regard. > > 郭雪松 <[hidden email]> wrote: > > > I wish gc() can free the keys of a mapping, so I consider to make the mapping > > WEAK_INDICES. > > > > But should I make this mapping WEAK_VA LUES too? this is a question: > > > > If the value has a reference to its key, I should make the mapping > > WEAK_VALUES, or the key will not be freed by gc(), > > because the value has a non-weak reference from the mapping, and the key has a > > (non-weak) reference from the value. > > > > On the other hande, if the value not has any reference to its key, I should > > NOT make the mapping WEAK_VALUES, > > becouse if I do so, the value may be free before the key. > > > > My question is: I might not know whether the value has a reference to key, > > because the value is returned from another module > > that is not write by me. > > > > What should I do? > > > > Happy New Year > > > > Guo Xuesong |
|
郭雪松 <[hidden email]> wrote:
> In my case there is no other reference to the value. Then it wouldn't work to use WEAK_VALUES - your mapping would be emptied on every gc. > /.../ My problem is I passed the key to a function that is writen by > another peaple, for example function 'object f1(object key)', I don't > known whether the return value has a reference to key. And the person > who wrote f1 don't known I will use the return value of f1 in this > way. so It's most likely he would not be careful to make the optional > ref to key from the return value fo f1 weak. Maybe a possibility is to wrap the keys before you put them into the mapping. Then it's the refs to the wrappers that are significant. Of course, wrapper objects incur an extra overhead by themselves. > I means current design of WEAK_INDICES/WEAK_VALUES is not friendly to > module split. You got a point, but it's a weakness of refcounting and the weak reference paradigm in general. But one can view things like that as part of the API and document them too. It's basically no different from other implicit characterstics such as thread safety, reentrancy, lock order assumptions, destructive modification etc, which also need to be part of the API docs to ensure that different modules works together. > There should be some mechanism to free the keypair when and > when only the other non-weak references run out, not count the references > from value regardless weak or not. Ok, so you think there ought to be a mechanism so that one can say that references to some object X that originates directly or indirectly from another object Y shouldn't be counted. That sounds amazingly complicated to keep track of, and I believe it cannot be solved without a lot of overhead. No, it appears your only options are to use wrappers and/or document it. |
| Powered by Nabble | See how NAML generates this page |
