473,500 Members | 1,822 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array CopyConstruct as efficiently as possible


If we want to copy an array of POD's, we can simply do:

SomePODType src[8] = { ... }, dest[8];

memcpy(&dest,&src,sizeof dest);

We can assume that this method is definitely faster than (if not at least
as fast as) the following method:

T *p = dest;
T const *const pover = dest + sizeof dest;

T const *q = src;

do *p++ = *q++;
while (pover != p);

This method won't work for class types, because the constructors and
assignment operators may acquire resources and so forth.

The following is a basic attempt to implement a universal method of copy-
constructing an array:

#include <cstddef>
#include <new>

template<class T,std::size_t len>
void CopyCstr(T const (&src)[len],void *const dest)
{
T *p = (T*)dest;
T const *const pover = p + len;

T const *q = src;

do ::new((void*)p++) T(*q++);
while (pover != p);
}
This looks grand, but what happens if we use it to copy-construct an array
of short ints? It will look like as follows:

void CopyCstr(short const (&src)[8],void *const dest)
{
short *p = (short*)dest;
short const *const pover = p + 8;

short const *q = src;

do ::new((void*)p++) short(*q++);
while (pover != p);
}

The only problem with this is that it may not be as efficient as it could
be. We'd be better off with simply:

void CopyCstr(short const (&src)[8],void *const dest)
{
memcpy(dest,src,sizeof src);
}

So I wonder how we can achieve the best of both worlds with the one sole
template function? If we had a way of knowing that the "normal
initialisation" for a particular type was a no-op, then we could take
advantage of it. Something like

template<class T,std::size_t len>
void CopyCstr(T const (&src)[len],void *const dest)
{
if ( NoOp(::new(void*) T) ) memcpy(dest,src,sizeof src);
else
{
T *p = (T*)dest;
T const *const pover = p + len;

T const *q = src;

do ::new((void*)p++) T(*q++);
while (pover != p);
}

Obviously, the "if" conditional would be known at compile-time to be either
true or false, the the opposite command path could be done away with.

Anyway, this was just a thought that went through me head...

--

Frederick Gotham
Nov 15 '06 #1
1 1523
Frederick Gotham wrote:
>
So I wonder how we can achieve the best of both worlds with the one sole
template function? If we had a way of knowing that the "normal
initialisation" for a particular type was a no-op, then we could take
advantage of it.
std::tr1::has_trivial_copy gives you that information.

// sketch, untested:

template <class Ty, bool>
struct copier
{ /* element by element copy */
static void init(Ty *tgt,
const Ty *src, unsigned count);
};

template <class Ty>
struct copier<Ty, true>
{ /* byte copy */
static void init(Ty *tgt, const Ty *src, unsigned count);
};

void init(Ty *tgt, const Ty *src, unsigned count)
{
copier<Ty, has_trivial_copy<Ty>::value::
init(tgt, src, count);
}

For a complete example (using assignment, not construction), see listing
8 in the section "Type Traits" in my article at
http://www.ddj.com/dept/cpp/184401964 (limited access, unfortunately).

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Nov 15 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2366
by: Adam Hartshorne | last post by:
As a result of a graphics based algorihtms, I have a list of indices to a set of nodes. I want to efficiently identify any node indices that are stored multiple times in the array and the...
1
1806
by: Andy Ganczak | last post by:
I want to create a data structure in which a 2x2 array contains elements that themselves are arrays and integers. array 0 1 2 ... 0 array1 array2 array3 1 array4 array5 ...
14
2561
by: vib | last post by:
Hi there, union UC32 { unsigned int L; unsigned short S; unsigned char C; } UC32; with the above structure, I am able to shift bits of C, into C, and C into C so on and so forth till C as...
2
864
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
26
7026
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
23
16338
by: Gerrit | last post by:
Hi all, I'm getting an OutOfMemoryException when I initialize a byte array in C# like this: Byte test = new Byte; I'm using ASP.NET 2.0. In ASP.Net 1.1 it works fine. So what am I doing...
3
24295
by: Rode | last post by:
Hi, I want to initialize some elements in an array at the beginning(say 300 elements) and at the ending(230 elements) to 0 and the elements between start and end to a value based on the calculation....
9
7289
by: herobeat | last post by:
Hi all, I'm having a hell of a time with declaring a struct to hold some binary data I'm trying to read from some files on disk. What I would like to do is something like this: public struct...
0
1024
by: DR | last post by:
is there any speed difference between a TVF CLR that returns an array vs yield as each result is ready? e.g. does returning the array all at once cause sql server to allocate more efficiently?
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7232
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6906
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7397
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.