Efficient class movement with POD

How to efficiently move a class with a large set of POD elements? Example:

struct{ int a1; int a2; int a3; ... ... ... }; 

By "move" I mean that the behavior is similar to the semantics of movement (std :: move).

+6
source share
4 answers

PODs do not move, they are simply copied. Because there is no indirection. Therefore, just use the normal task and ask the compiler to optimize any performance that you have in mind. Remember to measure, systematically, before (what you do differently on Earth) and after. Also think about whether the programmerโ€™s time, that is, money, is worth the micro-optimization.

+10
source

You must make an engineering decision to play. Imagine, for example, that you are talking about whether to use std::array<int, N> or std::vector<int> elements with N , where N is the compile-time constant.

When N very small, std::array<int, N> is the clear winner because there is no heap. And copies (and moves equivalent to copies) are cheap.

When N very large, std::vector<int> is a clear winner, because although there is a heap distribution, you can move around vector with the cost of only moving 3 words, regardless of the size of N

When N == 3 , there is no doubt that this is the best choice. When N == 3,000,000 there is no doubt that this is the best choice.

Your job as a designer is to enter that gray area between these two extremes and make the right decision. Not always the right decision. Measuring performance for your expected use cases goes a long way. Use <chrono> for these performance measurements. If you do not know what <chrono> , do a stackoverflow search.

+8
source

This question raises a lack of understanding of what movement is in C ++ 11.

When you copy an object with pointers or otherwise own resources, there are two ways to make this copy. You can either copy the links of pointers / resources, or you can select new objects / resources and copy the value of the original to new ones.

In the first case, you are left with two objects that have links to the same object. Qt does it a lot. If you use one object to change what it refers to, you also modify another. Usually you need some kind of reference counter so as not to double delete the pointer or double release the resource.

In the second case, you are left with two completely separate objects. This is commonly referred to as "semantics of values" because if you copy one POD to another, you have two completely separate objects. Changing one does not change the other.

Moving semantics is a C ++ 11 mechanism that allows objects that usually have value semantics to have reference semantics under certain conditions. This essentially allows the object to steal pointers / resources referenced by another instance of the object.

For example, take std::vector ; it's just a wrapper around a dynamically allocated and resized array. As with most standard C ++ library objects, vector implements the semantics of values. If you copy vector , the new vector should select a new array and copy each element from the old to the new. Once you're done, you have two completely different arrays.

Moving semantics is a way of transferring an array contained in vector to another. After the operation, the displacement source object is "empty" (technically in the undefined state, but it is effectively separated from the data that it allocated). The new vector now has the same pointer as the old vector ; the new vector will delete the memory that it did not allocate.

As you can see, all this is based on the concept of an object that owns resources. vector allocates and owns an array; this destructor will destroy it. This is how you determine ownership. The motion concerns the transfer of ownership.

PODs cannot express ownership of pointers or resources because PODs must have trivial destructors (i.e., destructors that do nothing). Since PODs cannot express their property, there is nothing to move. You cannot transfer ownership between objects if they do not own anything.

+7
source

Make a class pointing to the class with POD, and use std :: move on the first.

+1
source

Source: https://habr.com/ru/post/921009/


All Articles