Weak QHash per value? what do you think about QHash<MyElement, QWeakPointer<MyElement>> ?
-
Hi,
I'm porting a java program in C++/QT and I have to migrate a cache designed as a
WeakHashMap<MyElement, WeakReference<MyElement>>I think what I'd really try to achieve is a QHash<MyElement, QWeakPointer<MyElement>> that removes the entries from the QHash when their value (the weakptr) gets null.
This structure is kind of looking strange to me and I'm not really sure if this could work as expected...
Basically the app has several main objects (let's call them MainObject) that can manipulate some MyElements : create some, do some operations with them to eventually store in a stack (the MainObjects have each their own different stack) one particular MyElement and delete the other ones. It is those saved value that I want to share between the MainObjects.
When no more MainObject uses a shared MyElement, we would like to remove it from the cache.As it sounds around sharing an object, I'm thinking maybe there is more QT way doing that using QSharedDataPtr but I don't really see how...
The MyElement contains itself a QMap and has a provided hash function that I'm supposed to reuse but collisions may happen, that is why they're using a copy of the object itself as a key.
Would it be more efficient maybe to generate a unique QString from a particular MyElement and use rather that as a key?
The number of elements of the cache may grow huge. On a mini test of 1min with only 2 MainObjects, it went up to 35k, some bigger test can run hours with 100s of objects, so I think we might have quite a lot of entries. I'll try to get a figure, must be several 100k, might be more...
Any recommendations for such design?
Cheers -
@mbruel "that removes the entries from the QHash when their value (the weakptr) gets null" - QHash does not remove anything, its you who needs to remove elements which are not needed anymore.
"the MainObjects have each their own different stack" - what do you mean by that? Objects do not have own stacks.
"create some, do some operations with them to eventually store in a stack" - be careful with pointers to objects allocated on the stack! -
@jsulm said:
@mbruel "that removes the entries from the QHash when their value (the weakptr) gets null" - QHash does not remove anything, its you who needs to remove elements which are not needed anymore.
yes I'm planing to implement it myself. Either directly from the destructor of the MyElement or with a cleaning thread....
but I'd like some feedback on such structure, I'm not so fond of it and not sure it would be the way to go..."the MainObjects have each their own different stack" - what do you mean by that? Objects do not have own stacks.
well by stack I was referring to std::stack or any kind of container. basically the MainObject will have 2 "stacks" of input/ouput MyElements. As I need to save the timestamp of those, it will probably be 2 MultiMaps.
So I'm thinking to have something like this:class MainObjet { QMultiMap<double, QSharedPtr<MyElement >> inputs; QMultiMap<double, QSharedPtr<MyElement >> outputs; };
The QWeakPointer in the cache I wish to implement would refer to one (or more) QSharedPtr.
Sometime I remove a QSharedPtr<MyElement > from the inputs (QMultiMap) and delete it. I wish that in that case, if it wasn't shared, so if the WeakPtr is null, I could remove it from the cache.
What do you think? -
@kshegunov
MyElement doesn't have to inherit from QObject. I guess I could make it if there is a good reason. Are you thinking about connecting to the destroyed signal on deletion?
it's structure is simple, it would be mainly this:class MyElement{ public: enum Properties : ushort {p1,... p500}; private: double _mass; QMap<Properties, double> _composition; };
its hashing function would be a numerical computation using both the _mass and every entry of _composition (key and value).
The goal is to limit the memory usage, the current simulation in Java can eat up to more than 8GB of ram. I suppose they used this approach to decrease this usage but I don't know how I could check at one point some MyElement are shared.
The problem I see with using a QHash<MyElement, QWeakPointer<MyElement>> is that I will store 2 instances of the object, one as a key and the other one in the heap shared by the other objects....
-
@mbruel said in Weak QHash per value? what do you think about QHash<MyElement, QWeakPointer<MyElement>> ?:
The goal is to limit the memory usage, the current simulation in Java can eat up to more than 8GB of ram.
Java is a memory hog to begin with.
MyElement doesn't have to inherit from QObject. I guess I could make it if there is a good reason. Are you thinking about connecting to the destroyed signal on deletion?
Yes, that's what I was thinking about, but not needed in this case.
Use implict sharing for theMyElement
class and pass it by value everywhere.
... and please, for the love of god, storeQMap<Properties, double> _composition;
as a vector or at the very least as a hash. Do you really want a tree rotation with each insert?PS.
With that a simple memory requirement you don't even need any sharing, the containers are implicitly shared by default so you're okay just passing the object around by value. -
@kshegunov said in Weak QHash per value? what do you think about QHash<MyElement, QWeakPointer<MyElement>> ?:
Java is a memory hog to begin with.
well I hope to have better performance in C++, I don't know yet how much better I will get...
Use implict sharing for the MyElement class and pass it by value everywhere.
so you mean make MyElement derive from QSharedData. What do I store in the Cache? still a QHash<MyElement, QWeakPointer<MyElement>> ? this wouldn't be by value...
The thing is that the MainObjects would not necessarily pass the MyElement to each other, they may create two similar instance and the goal of the Cache is to give them the same one. When they store in their "stack" (inputs or outputs multimap) a MyElement, they ask to the cache if it has one already, to get the shared handle.
I don't know if I'm really clear, do you see what I mean and the potential issue with implicit sharing. it seems to me what I really want is to share a Pointer on a particular instance.
Plus with implicit sharing, how could I take out an entry from the cache if no MainObjects are using it anymore?
PS:
and please, for the love of god, store QMap<Properties, double> _composition; as a vector or at the very least as a hash. Do you really want a tree rotation with each insert?
well I don't have many properties in general in the map, maybe between 3 and 10 max. but I don't know which ones, there are around 500 possibilities. I won't do any insertion once it is set up. I guess I could use a Hash instead, is it really worth it? I may need to iterate it in a sorted manner. a QVector of QPair would be a bit an hassle...
-
after some discussion with colleagues I think to reproduce the java WeakHashMap, I need to do something use a wrapper on a QWeakPointer<MyElement> as the key of my QHash that will have a hash function on the value of the object and the equal operator too on the value.
Something like this:class MyElementWeakPtrWrapper{ QWeakPointer<MyElement> _weakPtr; public: bool operator==(const MyElementWeakPtrWrapper& other) const { // check if weakPtr are null (same state) bool isNull = _weakPtr.isNull(), otherIsNull = other._weakPtr.isNull(); if ( (isNull && !otherIsNull) || (!isNull && otherIsNull)) return false; if (isNull && otherIsNull) return true; // both are not null, lets get a sharedPtr QSharedPointer ptr = _weakPtr.toStrongRef(), otherPtr = other._weakPtr.toStrongRef(); isNull = ptr.isNull(), otherIsNull = otherPtr.isNull(); if ( (isNull && !otherIsNull) || (!isNull && otherIsNull)) return false; if (isNull && otherIsNull) return true; // Both sharedPtr are not null return *ptr == *otherPtr; } };
So my QHash would be:
QHash<MyElementWeakPtrWrapper, QWeakPointer<MyElement> > WeakHashTable;
I can then store the key of the Hash in the MyElement (value) inside the table so when it will be destroyed, it emit a signal with that key (MyElementWeakPtrWrapper) that my WeakHashTable will catch to remove the corresponding values that are null.
What do you think of this approach?
-
Under what circumstances would a MainObject share a MyElement instance?
The way I understand, the intention is that if a MainObject needs a certain MyElement, it first checks in the cache whether it exist already, otherwise it creates it and adds it to the cache.If I got this right, my caching approach (off the top of my head) would be something like this:
class Cache { public: // Return item from cache if it exists, otherwise create, add it to cache and return // MyElementKey is some way to uniquely identify the MyElement the caller wants std::shared_ptr<MyElement> obtainElement(const MyElementKey& key); // Call periodically to eliminate elements which have turned nullptr void collectGarbage(); private: QHash<MyElementKey, std::weak_ptr<MyElement>>; };
-
@Asperamanca
You got the approach right. My MainObject creates and uses some instances of MyElement and when they need to store one that is interesting, we pass through the CACHE in order to centralize only one Instance between all the MainObjects that would need to store the same value.
The problem is that the Key is the value of the MyElement itself.
That is why I thinking to use a wrapper on QWeakPointer that would have its qHash based on the value of the weakpointer and idem for the equal operation.
You see what I mean?Yeah I thought to have a periodical cleaning function, but it would force me to iterate through all the items (key, values) of the QHash.
It sounds more efficient to connect to the destructor signal and get back the specific keys that would have some null values. -
What do you think of this approach?
You're overengineering a solution to a problem that you don't have. Instead of doing a copy of a double, a pointer and an integer (what your class has), you are going to invent a million pointers to share data that's already either too small to be shared effectively or already shared. That's what I think.
The point of rewriting code is not do duplicate it in another language, but rather redesign the parts that were badly designed due to the language specifics, legacy or other reasons.
-
@kshegunov
Well I guess this design in Java has been made to drop the memory use in a drastic manner.
In a small test, the WeakHashMap grows its size to 35146 Items with 33117 different keys (so not so many collisions).
Still in the same test, there are 158719 calls to the getter of the cache. Which means I'm saving 158719 copies.
My object is small yeah: 16 bytes (8 for the double, 8 for the pointer of the QMap).
So I'm saving 2480 kB, i.e: 2.42Mo
we can agree that this is not so much...
but I imagine that in the long run, the app can run several large simulations and keep all those results so if we could arrive to 1000 times more this number of objects which make us reach 2.4GB...
Well I guess I really need to have the proper worst scenario conditions but it could evolve with time so maybe even small saving of memory is a good thing to do....Another thing is that this is a scientific application done in Java, Java only uses handles so like copy of pointers, never the object itself, so it seems to me that it is kind of the same that QT implicit sharing is offering. They decided to introduce this WeakHashTable in order to reduce the memory usage that was too huge.
I think when you translate an app and need to have better performances (in term of speed of execution and memory usage) it is maybe better to take those kind of improvement in a first step and then check if it was really useful.
If not, I'll be easily able to remove it. I will win in speed cause there won't be anymore any hashing to compute (which is quite complex in my case) but I'll see how worst the memory usage become...I'm going to try implementing the solution I proposed more up and let you know the results.
Some kind of self centralizing Cache that weakly share pointers between objects doesn't sound such a bad thing to have especially when it is quite easy and fast to implement and test. -
A lot of guesses and assumptions. Let me crunch up some numbers for you then.
QWeakPointer
is at least one pointer in size (in actuality it is two pointers) so you have 16 bytes there. AQSharedPointer
is one pointer + one heap allocation on a small structure to hold the external reference count, so you have yet another pointer and an atomic integer (at the very least), so 12 bytes. Say all shared pointers are copied as such and none are created from a raw pointer - that's 8 bytes perQSharedPointer
object + 12 bytes for the shared structure. And this is overhead only! And that's not accounting for the cost of the heap allocation itself if you create it out of a raw pointer ...The cost of a lookup in the map is
log(N)
whereN
is the number of elements ~15 for a 35k elements, while the amortized cost for a hash or a simple vector is justO(1)
.Taking in mind that your object is 20 bytes in size, contiguous in memory, and by passing it by value you can skip any costly calls to the heap manager, what exactly do you think you're saving?
It's not memory for sure and it ain't CPU time either. -
@kshegunov
well I gonna think a bit more about how I could use Implicit Sharing. You're right, I didn't count my pointer overheads... :$
For "the fun of it", I've implemented it with a simple example using only a QString as a data. Here is the code
You can see the behaviour I wish from the main:class MainObject{ public: MainObject(SharedObject *sharedObj):_sharedPtr(cache.getCentralizedValue(QSharedPointer<SharedObject>(sharedObj))) {} private: QSharedPointer<SharedObject> _sharedPtr; }; #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); MainObject *obj1 = new MainObject(new SharedObject("Object 1")), *obj1bis = new MainObject(new SharedObject("Object 1")); qDebug() << "[main] cache size after inserting two same instance: " << cache.size(); delete obj1; qDebug() << "[main] cache size after deleting 1 instance: " << cache.size(); delete obj1bis; qDebug() << "[main] cache size after deleting both instance: " << cache.size(); return a.exec(); }
This outputs:
[CentralizingWeakCache::getCentralizedValue] adding new value in cache : "Object 1" [CentralizingWeakCache::getCentralizedValue] getting centralized value for : "Object 1" [SharedObject::~SharedObject] destroying "Object 1" [main] cache size after inserting two same instance: 1 [main] cache size after deleting 1 instance: 1 [SharedObject::~SharedObject] destroying "Object 1" [CentralizingWeakCache::handleSharedObjectDestruction] removing centralized value: 1 [main] cache size after deleting both instance: 0
Anyway I'll give it more thought tomorrow. I'm still confuse how to merge different instances of my objects and thus use the implicit sharing.
I guess I still some kind of a Cache in the middle so each MainObject can get a copy of the one it stores (the Cache) and delete the one it had created. Do you see what I mean?PS: indeed if I could avoid the hashing of my SharedObject (previously MyElement) this would be a great improvement in CPU usage... but I really don't see yet how to achieve it... I guess I'll make you a drawing tomorrow to illustrate what is blocking me.
Thanks for your replies anyway ;)
-
I think it may be worthwhile to step back a bit and take a look at the boundary conditions:
- How costly is it to create a MyElement?
- What data is used to create a MyElement, and where is it stored?
- How often are MyElement-Classes shared between MainObjects, and what are typical numbers for sharing (i.e. is the same object shared twice or 100 times)?
- Is there a meaningful way to combine multiple MyElement classes into a bigger group of object, where caching would make more sense (i.e. will it often happen that certain groups of MyElement are used together)?
- Will properties of MyElement change after construction?
- Do MyElements with the exact same mass have the same properties?
Also, I'm not sure using a hash as a key is smart. What happens if you get multiple MyElements with the same hash value? Will your cache give a "wrong" MyElement to a MainObject, or won't it matter, as long as the hash value is the same? Or would the MainObject have to create the MyElement it wants, and compare it to the one in the cache?
EDIT:
If the likelihood is high that different MyElements have a different mass (even if only ever so slightly), then you may have the option of creating a heap that is sorted by mass. The heap would allow you to quickly find all MyElements with a known mass. If there are multiple entries, you would have to iterate through them one by one until you find the one you want (or don't), but the same applies to the hash (although it is going to be less likely). -
@Asperamanca said in Weak QHash per value? what do you think about QHash<MyElement, QWeakPointer<MyElement>> ?:
I think it may be worthwhile to step back a bit and take a look at the boundary conditions:
Indeed I think that's wise :)
How costly is it to create a MyElement?
Not much really: MyElement is only storing a QMap of (ushort, double). As the key is from an enum, I know there is a max number of entries which is less 500. In practice, I think most of them would have between 2 and 10 entries max. (I'll have to verify that with the business...)
The mass in fact is not really needed, it is just a shortcut that represent the sum of the values of the Map.What data is used to create a MyElement, and where is it stored?
Typically, the MyElement are creating by cloning an existing one and using an external app (in Fortran) that will do some heavy computations on it to at the end produce a new MyElement different from the one in Entry.
All MyElements are stored in the Heap.How often are MyElement-Classes shared between MainObjects, and what are typical numbers for sharing (i.e. is the same object shared twice or 100 times)?
I don't have access to that information. I'm not sure how I could hack the java WeakHashMap in order to get it. The only thing I made was to dump periodically the size of the cache, its number of distinct keys and finally increment a number each time an entry is found in the cache and thus shared.
On a simple exemple, the figures are that:Max Nb MyElements in cache : 35146 (reused: 158719, nb keys: 33117)
So I just know that there is 158719 objects that are shared among 35146 but I have no clue about the distribution.
Is there a meaningful way to combine multiple MyElement classes into a bigger group of object, where caching would make more sense (i.e. will it often happen that certain groups of MyElement are used together)?
This is something also implemented in the Java code but it wasn't used in production at the end. What they did was to use exactly the same principle of cache sharing on another object that encapsulate several MyElements.
For what I saw, I think it is really worth it to keep the caching on MyElement and then maybe add the other caching on the bigger structure on top. This doesn't seem to me to be incompatible.Will properties of MyElement change after construction?
No, they are final when stored in the cache to be shared. If they are in the cache, it means that at least 1 MainObject is referencing it. If finally the last MainObject stop using it, then the MyElement is automatically removed from the cache (cleaned up)
This is done automatically by the Java WeakHashMap, and I've implemented that by making the MyElement a QObject and connecting a destroyed signal to the Map where I send the Key.Do MyElements with the exact same mass have the same properties?
As I said, the mass is not really relevant. It is the sum of the properties. It is used for a quick access and a first fast way also to compare two MyElements
Also, I'm not sure using a hash as a key is smart. What happens if you get multiple MyElements with the same hash value? Will your cache give a "wrong" MyElement to a MainObject, or won't it matter, as long as the hash value is the same? Or would the MainObject have to create the MyElement it wants, and compare it to the one in the cache?
Well I'm not using a hash as a key. If you look at the code (it's here), the key is a QSharedPointer<WeakCacheKey>.
WeakCacheKey being a wrapper on a QWeakPointer<MyElement>.
The hashing function of a WeakCacheKey is storing the hashCode just to be able to remove an entry from the CentralizingWeakCache when an MyElement is destroyed. Indeed, at this point we can't dereference the MyElement to do its hash.
In the normal insertion in the CentralizingWeakCache, I'm returning:qHash(*(sharedPtr.data()));
that will ends up in the hashing function of the MyElement (called SharedObject in my implementation)
inline uint qHash(const SharedObject & sharedObject) { return qHash(sharedObject._value); }
If I've some collisions on the hash key, then it is operator== that is used on my keys which I've reimplement to derefence the weakPointers if possible
inline bool operator ==(const QSharedPointer<WeakCacheKey> &left, const QSharedPointer<WeakCacheKey> &right) { // check if weakPtrs are null (same state) bool isNull = left->_weakPtr.isNull(), otherIsNull = right->_weakPtr.isNull(); if ( (isNull && !otherIsNull) || (!isNull && otherIsNull)) return false; if (isNull && otherIsNull) return true; // both weakPtrs are not null, lets get sharedPtrs QSharedPointer<SharedObject> ptr = left->_weakPtr.toStrongRef(), otherPtr = right->_weakPtr.toStrongRef(); isNull = ptr.isNull(), otherIsNull = otherPtr.isNull(); if ( (isNull && !otherIsNull) || (!isNull && otherIsNull)) return false; if (isNull && otherIsNull) return true; // Both sharedPtrs are not null, compare the values of the objects return *ptr == *otherPtr; }
So I may have mistaken somewhere but for what I've debugged it looks it does what I want: The key of my cache is QWeakPointer<MyELement> but the hashing and comparison function are made on the value itself (if it still exists)
If the likelihood is high that different MyElements have a different mass (even if only ever so slightly), then you may have the option of creating a heap that is sorted by mass. The heap would allow you to quickly find all MyElements with a known mass. If there are multiple entries, you would have to iterate through them one by one until you find the one you want (or don't), but the same applies to the hash (although it is going to be less likely).
I'm not so familiar with heaps. I don't really need my cache to be sorted, I just need a quick access to the elements. I can't rely on the mass, but rather on the map of properties. So I imagine I would also need to kind of hash the map... so if I have to do that operation, it sounds more natural to use a QHash no?
Thanks for your help :)
-
I got a "real" use case. Just by running it until 16% of the simulation, here are the figures I got:
[MB_TRACE] Max Nb elem in cache : 593051 (reused: 5752723, nb keys: 586598
so we're probably talking about millions of entries in the QHash. The hashing function is quite good, the collision rate is only 1.09%
The sharing is quite huge, I've 5.75 millions of copies are avoidedThe thing I'm missing is to have an information on the number of MyElement that are automatically purged from the Hash because they're not used anymore by any MainObjects
PS: I'll probably get some figures for the whole simulation tomorrow or the day after...
-
Thanks for the clarifications.
In that case, your approach looks feasible. A few minor points:
- Why use QSharedPtr<WeakCacheKey> instead of WeakCacheKey directly?
- Deriving SharedObject from QObject makes it pretty heavyweight. You can get the same done by having a single, mutex-protected notification object, giving each SharedObject a reference or pointer to it, and calling a method on the notification object in the destructor of SharedObject
- If you have class members that are only changed at construction time, make them const. If you build your algorithm on the decision that these cannot change, then you should make that intention explicit (possible compiler optimizations come as a bonus)
-
@Asperamanca said in Weak QHash per value? what do you think about QHash<MyElement, QWeakPointer<MyElement>> ?:
Why use QSharedPtr<WeakCacheKey> instead of WeakCacheKey directly?
Well this is just to avoid circular header dependency and not include WeakCacheKey header in SharedObject.h but just a forward declaration. So SharedObject holds a pointer on its key on the Map. I made it a sharedPointer so I don't deal with the deletion. (I was also using it to test if the key wasn't nullptr and thus know that the SharedObject is stored in the Cache)
Can I get a pointer on the key of a QHash? I don't think so right? that is why I've put directly the key of the QHash to be a pointer.Deriving SharedObject from QObject makes it pretty heavyweight. You can get the same done by having a single, mutex-protected notification object, giving each SharedObject a reference or pointer to it, and calling a method on the notification object in the destructor of SharedObject
You're totally right. I inspired myself by some code I've read before... (it's in french but I the code isn't, you can find it here an implementation of a WeakHashTable)
I've updated the code in github, no need indeed to have neither the SharedObject nor the CentralizingWeakCache inheriting from QObject and to rely on signal/slot communication.
When the Cache put a SharedObject in its QHash, it can just give the key and an handle on itself to the SharedObject.
Then the destructor of the SharedObject looks like this:SharedObject::~SharedObject() { qDebug() << "[SharedObject::~SharedObject] destroying " << _value; if (_cache) _cache->remove(_key); }
If you have class members that are only changed at construction time, make them const. If you build your algorithm on the decision that these cannot change, then you should make that intention explicit (possible compiler optimizations come as a bonus)
well that is true but I said the property map is constant but it is only the case for the interesting instances we share.
There are some temporary MyElement that are used in the app that can evolve before deciding to make it kind of constant. You see what I mean?
So I'm not sure it would worth it to create 2 distinct class: the const MyElement and then TmpMyElement without the constness.
I would need a copy to pass from the TmpMyElemnt to the const MyElement when I want to save it no?