Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to initialize array with a byte sequence?

Tags:

c++

c++11

Let's say I have a byte sequence of some size n (which could be 1..4 elements in the "real" code), with n = 3 for the sake of this example:

char source[n] = { 'a', 'b', 'c' }

And I have a memory range of sufficient size to hold m copies of this sequence:

char * dest = new char[m*n]

(And yes, I know std::vector, and yes, it's generally to be preferred over new'ing your own memory, and no, it's not an option for the code I am currently working on -- and anyway the problem would still be the same.)

Now I want to initialize dest with those m copies of source. There are various ways to do m copies of a single value, but apparently none for doing m copies of a sequence of values. Sure, I could use a nested loop:

for ( unsigned i1 = 0; i1 < m; ++i1 )
{
    for ( unsigned i2 = 0; i2 < n; ++i2 )
    {
        dest[ i1 * n + i2 ] = source[ i1 ];
    }
}

But somehow this lacks all the finesse that usually tells me that I got the "right" solution for a problem.

Does C++ offer some more efficient way for this?

like image 759
DevSolar Avatar asked Jun 08 '26 18:06

DevSolar


2 Answers

Would this give you a right feeling? (see it live here)

auto it = dest;
while ((it = std::copy(source, source + n, it)) 
       != dest + m * n);
like image 145
Lingxi Avatar answered Jun 10 '26 07:06

Lingxi


zero-initialising is the most efficient. Where m is large and access sparse particularly, the OS may even use soft page faults to do COW of the same virtual zero-ed memory page for example, lazy allocating the requested memory when it's actually used.

Now, if you XOR every store & load from the byte arrary, with the appropriate byte from source, you can change the meaning of the NULL bit pattern.

dest[ i] = c1 ^ source[ i % n];  // store update
c2 = dest[ j] ^ source[ j % n];  // load, if dest[ j] is 0 it was never updated

In modern Out Of Order CPUs operations are not expensive compared to memory cache misses.

What you do need for this technique is to allocate the byte arrary in an OS specific way that guarantees it is zero-ed eg) mmap under Linux

like image 26
Rob11311 Avatar answered Jun 10 '26 07:06

Rob11311



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!