Weak QHash per value? what do you think about QHash<MyElement, QWeakPointer<MyElement>> ?
-
@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? -
@mbruel said in Weak QHash per value? what do you think about QHash<MyElement, QWeakPointer<MyElement>> ?:
Well this is just to avoid circular header dependency and not include WeakCacheKey header in SharedObject.h but just a forward declaration.
If you give WeakCacheKey a cpp file, including a non-default destructor, you should be able to forward declare SharedObject, because after all, the WeakCacheKey only holds a pointer to SharedObject. Then you are free to include WeakCacheKey.h in SharedObject.h.
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?
If the intention is that, normally you will not want to change a SharedObject, but in some cases you do, I would suggest this approach:
class SharedObject { //...declarations... public: WeakCacheKey getCacheKey() const; QString getValue() const; private: void setValue(const QString& arg); friend class SharedObjectWriter; }; class SharedObjectWriter { public: SharedObjectWriter() = delete; SharedObjectWriter(QSharedPointer<SharedObject> sharedObject); void setValue(const QString& arg); private: QSharedPointer<SharedObject> _sharedObject;
That way, you make it pretty clear that writing to a SharedObject is supposed to be an exception, and not the rule. You could also include checks that only SharedObjects without a valid cache key can be written to, etc.
-
@Asperamanca
about using a QSharedPointer<WeakCacheKey> as a key of my QHash, in fact there is a better reason than just the circular dependency in the headers.If I put a simple WeakCacheKey, then the qHash fonction that would be called is:
uint qHash(const WeakCacheKey &cacheKey)
so I'm not able to modify cacheKey... or for efficiency I want to store the result of the qHash inside.
If I use a pointer or shared pointer, I'll end up in this one that I've just improved :)
inline uint qHash(WeakCacheKey * cacheKey) { if (!cacheKey->_hashComputed && !cacheKey->_weakPtr.isNull()) { QSharedPointer<SharedObject> sharedPtr = cacheKey->_weakPtr.toStrongRef(); if (!sharedPtr.isNull()) cacheKey->setComputedHashCode(qHash(*(sharedPtr.data()))); // save the hashCode } return cacheKey->_hashCode; }
I didn't add a cpp file to WeakCacheKey, rather I just took out the implementation of
bool operator ==(const QSharedPointer<WeakCacheKey> &left, const QSharedPointer<WeakCacheKey> &right)
and moved it into the CentralizingWeakCache. This way I can do only a forward declaration of SharedObject in WeakCacheKey.
(I'm not able to push the changes on github from work but I'll do it tonight from home, the main thing is the addition of this WeakCacheKey::_hashComputed boolean initialized to false that the WeakCacheKey::setComputedHashCode set to true at the same time it saves the hashCode.
I see your point with the SharedObjectWriter, you don't make the values const, you just don't expose them directly and return a copy... I thought you were suggesting to declare a const QString in SharedObject (or a const QMap<> in MyElement)
I'll think about it, nice suggestion.
-
@kshegunov
well I got more thought about using only implicit sharing.
So I make MyElement derive from QSharedData and the MainObjects using QSharedDataPointer<MyElement>The problem is that I will need to "merge" some instance so for this I need a QHash in the middle... Like in the solution I've implemented using WeakPointers.
I guess both the key and the value would be a QSharedDataPointer on my value I want to make unique.
so yeah I could fill the Cache like I'm doing with WeakPointers...
return also the one already shared if there is one.The issue is that my Cache has a QSharedDataPointer and not a WeakPointer. So I'm never gonna knows when no more MainObjects are using a MyElement and thus when I could delete some entries in the cache.
Do you see a way to do it?
Something else about the overhead of SharedPointers, well I suppose internally there is exactly the same within QSharedData no? there must be a uint for the reference count and some synchronization objects to make it thread safe...
-
I stand by my claim that you're doing nothing for a high price. Especially since I took a peek at the code you put up and I saw you're using a mutex to sync the access to the global object. I have only questions for you now.
- Do you know how heavy a
new
is compared to a stack allocation? - Do you realize how many bytes you're creating of overhead just to keep pointers around?
- Do you get that instead of doing a fully reentrant block-free copy of a small object you're locking a global structure that has your threads blocked for most of the time at high contention?
Just to wrap it up in a nice small bite - you could do a million copies in the time that each of the threads gets its share from that global mutex, and that doesn't even account for the pointer overhead, the refcounting and all that ... I am going to repeat myself now:
You're overengineering a solution to a problem that you don't have.In the end it is your code, it's your decision how you implement it, however what you asked me I answered already - you don't need any of that stuff above, C++ ain't Java!
- Do you know how heavy a
-
@kshegunov
How do you create dynamically elements on the stack?... if I allocate on the heap it's because I don't have the choice, I don't know in advance if I will create object and how many...Something I don't understand, if I go with implicit sharing, I'll need to use QSharedDataPointer to share my QSharedData no?
I think I can't just not factorize Items that are creating by different MainObjects. It's not only the 20Bytes of the objects, it is also the data they point to: the whole content of their QMap.
I'm not in the easy situation where MainObjects are passing to each other a shared object, they create it on their own (in their corner, somewhere in the heap) and if they create the same MyElement, it is a shame to not factorize it. You see what I mean? by default I may never be able to use implicit sharing if I don't have some kind of manager or cache in the middle to merge 2 distinct MyElements in one that the MainObjects will be able to share...I think you may not get the use case...
My MainObjects are creating dynamically some smaller objects MyElements, they don't pass them to each other, they do some computation on them and store them in a list. The thing is that those MainObjects will create many times the same instance of MyElement that another MainObject has already create. But it has no way to know about that so it can't just share it directly. I don't see how you could achieve it without an intermediate in the middle.Are you basically telling me that your way would be to never share any small objects, even if you have a billion of them?
-
I'm not attempting to be insulting (just in case): I don't think you understood what @kshegunov has been saying.
Regarding performance or anything actually. Instead of ask him/her/us this - why not just see how capable your hardware / design is, where it falls down first?Can you not do something like break your thoughts out? This feels so crazily complicated just for a couple of object types to be stored in memory?
std::vector<MainObject> mainObjects
std::vector<MyElement> elements
std::map<MainObject*, MyElement*> objectElementMaphttps://en.cppreference.com/w/cpp/container - this is the std:: lib containers - the problem of storing objects in memory is totally done you just have to decide what is appropriate
I'll reiterate @kshegunov's statement:
You're overengineering a solution to a problem that you don't have.
You are pre optimizing something you don't know. I'd argue you need to simplify your design first but who knows - maybe I just don't get it. I know I don't understand what you want / need / would be best.
Stop trying to think how to squeeze every last drop - you are in c++ - you will get your time to optimize but just choose a dam std::vector and be done with it =) when it breaks, get another and maybe even a thread.
You're missing the point just how fast c++ is - and also how little memory it uses (as opposed to frameworks and their ungodly GC .. et al. )
I'm not sure there's a clear answer but I'd recommend:
stop
trying to prematurly optimize (it likely won't be a problem from what I can best tell)
take a look at your design
simplify