473,624 Members | 2,458 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,&s rc,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 1534
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_t rivial_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_cop y<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
2372
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 location of them in the array /list. Hence the output being some list of lists, containing groups of indices of the storage array that point to the same node index. This is obviously a trivial problem, but if my storage list is large and the set of...
1
1819
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 array6 2 array7 integer1 ... ... Is this possible and how would I go about doing it efficiently?
14
2571
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 they are "union"ed
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 that has the information, but we as developers have no way to access this information. I have come up with a solution for the problem, (as I am sure many others have) using reflection. Here is some sample code that will print out the auto...
26
7063
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 the structure inside the array. When I try this, an error about late assignment appears. Is it possible to assign a value to a structure field that is in an array? I'm currently getting around the problem by creating a new structure, assign...
23
16384
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 wrong?
3
24309
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. Currently, I am doing in the following way: float fArray; int i; for( i = 0; i < 300; i++) { fArray = 0.0; }
9
7303
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 binHeader { public UInt32 Id; public UInt32 Offset;
0
1031
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
8625
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8336
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8482
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6111
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1487
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.