On 23/08/16 18:51, Cedric BAIL wrote:
> Hello,
>
> On Tue, Aug 23, 2016 at 3:31 AM, Tom Hacohen <t...@osg.samsung.com> wrote:
>> Callback arrays was an idea that was introduced a while back to save
>> memory. The idea came from the observation that in many cases we add a
>> few callbacks together to every object of the same type. For example,
>> for an elm widget, we may add "move, resize, hidden, mouse_down,
>> mouse_up" so instead of manually adding all of them separately and have
>> eo allocate memory for each (and a separate private data pointer for
>> each) we would just create a static array and pass that.
>> This leads (at least in theory) to quite significant savings in memory.
>> I haven't actually tested the change, as I didn't do it, Cedric, do you
>> have some numbers to share?
>
> I remember something along a few hundred kb on elementary_test. Edje
> using it was a real saver, it was ahead of any memory allocation at
> that point.

That's cool. That's why I suggested keeping them (to an extent) in my 
proposal.

>
>> However, while they provide a nice memory improvement, they have been
>> hampering many optimisation strategies that would make callback
>> invocation significantly faster. Furthermore, maybe (not sure), we can
>> automatically de-duplicate event lists internally (more on that in a
>> moment). With that being said, there is a way we can maybe keep array
>> callbacks with some limitations.
>
> Do you have a case where performance are impacted by callback today ?
> I have found that we usually have a very small number of callbacks
> (likely in an array this days) and when speed did really matter it was
> just best to not trigger the callback at all (That's why we have this
> code in many place that count if any callback has been registered).

It always showed up in callgrind. Obviously after you did your changes 
that improved things, because you essentially just don't call that code, 
but having to do this everywhere is a bit of a pain, especially if we 
can just make callbacks fast on their own.

Callback_call takes around 1.5% in the efl atm. Though if we remove the 
not-call optimisations it would be much more again. I wonder if we can 
reach good results without it.

 From my tests back when I was optimising callback invocation, we had 
around 5 callbacks on average on objects with non-zero number of 
registered callbacks with a maximum number of around 12 if my memory 
serves, so this could potentially make callback calls so fast any 
optimisations won't matter.

>
>> I discussed my ideas with Carsten on IRC and I believe we have reached
>> something that would be significantly faster and better.
>>
>> Assuming callback arrays are no more:
>> First of all we will change the internal callbacks structure from a list
>> to array. The array will be sorted based on the address of the event
>> structure first, and priority second. The array will only include the
>> pointer to the callback structure, nothing else. All of the rest of the
>> information needed will be stored in an array of the same size and the
>> same order.
>> This means that we will now be able to walk the array and stop once the
>> address of the event we are looking for is higher than the address of
>> the address of the current event. Meaning we will walk only 50% of the
>> array on average, and the array itself will most likely be in the same
>> cache line. After we find the correct one, we will also fetch the rest
>> of the data based on the index. This will lead to blazing fast
>> iterations of callbacks without the need to optimise at all.
>
> Callback array are already likely to be in the same cache line. Them
> being const means they are automatically shared with all instances. It
> does impact our memory usage, but also our cache usage. For example in
> edje, we do have object in an object that trigger a callback and get
> propagated to its parent. With array callbacks it is exactly the same
> memory used in both object. So it doesn't only save memory, but I am
> pretty sure it does result in the same kind of speed up you are
> looking at.

Yes, callback arrays are good like that, that's why I said below that it 
will essentially be the same (and the same code). I have a feeling based 
on this comment and the one above that you read my emails and reply 
immediately instead of reading the whole thing first.

>
>> We can also store a pointer to the array in a hash table with the key
>> being some sort of a hash of the array in order to do some deduplication
>> afterwards (point to the same arrays, but obviously different private
>> data, so that would still be duplicated) if we feel it's needed. It
>> probably won't save as much though and will have some running costs.
>
> For anything < 16 entries, I bet that a hash table will be slower than
> walking an array. Don't forget you need to compute the hash key, jump
> in an array, walk down a rbtree and finally iterate over a list. Hash
> are good for very large number of object, not for small number.

That was an optimisation that I just threw out there to the world, but I 
believe you misunderstood me. I didn't mean we create a hash table for 
calling events, it was for saving memory and deduplicating event 
callbacks (essentially callback arrays automatically). This is only done 
on callback add/del.

>
>> The last idea is to keep callback arrays, but kind of limit their scope.
>> The problem (or at least one of them) is that callback arrays support
>> setting a priority which means calling them needs to be in between the
>> calls to normal callbacks. This adds a lot of complexity (this is a very
>> hot path, even a simple if is complexity, but this adds more). If we
>> define that all callback arrays are always the lowest priority (called
>> last), which in practice will have almost zero impact if at all, we can
>> just keep them, and just call them after we do the normal callback calls
>> (if they exist). We can even optimise further by not making the arrays
>> constant, and thus letting us sort them and then run the same algorithm
>> mentioned above for searching. This is probably the most acceptable
>> compromise, though I'm not sure if it'll block any future optimisation
>> attempts that I'm not able to foresee.
>
> No ! Array are only useful if they are constant ! That is the only way
> to share them accross all instance of object. Their size being
> ridiculously small, I bet you won't win anything in reordering them.
> And if you really want to reorder them, you can do that once at
> creation time in the inline function that create them as defined in
> Eo.h.

That is absolutely untrue. You can reorder them where they are created 
(like you suggested), or reorder them when they are added and still 
share them. You'll only need to reorder once, after that, when they are 
in order, that's it. Const doesn't matter or help at all. Obviously 
you're expected not to change them.

>
>> I'm not a huge fan of callback arrays, but if they do save the memory
>> they claim to be saving, I see no problem with keeping a more limited
>> version of them that let us optimise everything in the manner described
>> above.
>
> I am not a huge fan of optimization without a clear real life case.
> Please share number and scenario of when it does matters. I have seen
> enough people wasting there time optimizing things that don't matters
> that I really take it with a grain of salt if you are not showing real
> life scenario. Sharing a callgrind trace or something along that line
> would really help make your point here.
>

As I said, it's ~1.5% of the efl cpu usage when scrolling around 
genlist. It also wastes our memory to have them support priority. And as 
your changes proved, there is a reason to minimise callback calls, so we 
already have a case, instead of letting everyone reimplement that 
counting, it's better to just make callback calls fast. As I said, the 
price is very small, all I'm asking for is removing priority from 
callback arrays and always assume they are the lowest priority.

--
Tom.

------------------------------------------------------------------------------
_______________________________________________
enlightenment-devel mailing list
enlightenment-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/enlightenment-devel

Reply via email to