xiaoyin0208 / lz4

Automatically exported from code.google.com/p/lz4
0 stars 0 forks source link

Enhancement: move dec2table #54

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
In LZ4_uncompress:

By moving:
const size_t dec2table[]={0, 0, 0, -1, 0, 1, 2, 3};

to the beginning of the function instead, the compiler will not have to 
initialize it every time the if {} is entered.

This gives a small speed boost on 64-bit platforms.

Original issue reported on code.google.com by strig...@gmail.com on 5 Dec 2012 at 8:13

GoogleCodeExporter commented 8 years ago
This one is suprising.
I would expect the compiler to be smart enough to only initialise the table 
once, since it is "const".
Placing the table at the beginning of the file, or into the if {} should make 
no difference from an allocation perspective, and, i would have expected, from 
an init perspective too.

But let's check that.

Original comment by yann.col...@gmail.com on 6 Dec 2012 at 12:58

GoogleCodeExporter commented 8 years ago
If you make it static const, it will be put into the const data segment and 
will not be initialized at all at run time.

However, that gave a small performance decrease. Putting it in the beginning of 
the function was faster for some reason, even though it needs another 20 
instructions or so to initialize it.

Original comment by strig...@gmail.com on 6 Dec 2012 at 1:07

GoogleCodeExporter commented 8 years ago
In most circumstances, data in the stack is accessed faster than data in the 
heap.
It's probably a side-effect of cache-locality.
static const moves the table into the heap.

I'm still surprised though that the table would be init at each passage into 
the if{}. Or that it would be specifically for 64-bits systems, and not 32-bits.

Well, anyway, the fix is easy enough.

Original comment by yann.col...@gmail.com on 6 Dec 2012 at 1:21

GoogleCodeExporter commented 8 years ago
The same thing happens with non static const arrays also for 32-bit.

However in this case, dec2table exists only in 64-bit mode. That's why it 
affects only 64-bit.

Original comment by strig...@gmail.com on 6 Dec 2012 at 1:23

GoogleCodeExporter commented 8 years ago
Enhancement request.

Original comment by yann.col...@gmail.com on 6 Dec 2012 at 1:35

GoogleCodeExporter commented 8 years ago
Enhancement integrated into r85.

Original comment by yann.col...@gmail.com on 11 Dec 2012 at 1:42