Trying optimize CCMutableArray

Trying optimize CCMutableArray
0.0 0


Sometimes i’m like to use CCAnimate action, so when i get free time i’ve looked at CCAnimate::update and noticed that it use count() method of mutable array, which very ugly from my point of view (iterate over all elements of vector to count them? kill me please).

Since i can’t fully understand logic of man who write CCMutableArray code, i wan’t discuss this pullrequest . I think that code must be rewritten this way. If there are reasons which make it unpossible, please write here, and we discuss what can be changed to reach sufficient performance for count and addObject methods.


You’re always welcome!
But I have some personal affairs to deal with in this Saturday (in fact my wife is pregnant, she had to go to hospital for every-week-detection).
Can I check your pull tomorrow or next Monday?


Yes, we can optimize the count method, record the size of it. But I am not sure if we can optimize for addObject. I thought you want to use push_back to add the object. But I think we can not do it like this. Because there is a function insertObjectAtIndex, it may cause hole of the array, for example:

CCMutableArray *p1 = new CCMutableArray();
p1->addObject(new CCObject());
p1->insertObjectAtIndex(new CCObject(), 5);

// We want to add the object at index 1, not 5.
p1->addObject(new CCObject());


Walzer, my congratulations =) Wishing you and your wife a happy and healthy new baby. Of course you can always do what you like, i’m not hurry, just want make cocos2d-x better :slight_smile:

Minggo, i’m understand, but when i examine code, i can’t find any example of this situation. This is why i suggest discuss changes.

And, theoretically if i meet somewhere situation you described… Is it correct logic? We insert in array three objects, but count() returns that here only two objects…

P.S. Whe have one call of insertObjectAtIndex in CCTouchDispatcher::forceAddHandler but code insert handler between of existing handlers or adds it to end of list, so i just add assert assert(uIndex <= count()); to method. Or i’ve missed something?


Marat Yakupov, I agree with you. I should check the usage of insertObjectAtIndex, and do the optimization.