Skip to content

Commit 721cf5a

Browse files
committed
Optimize: optimize memory copy in MemoryBlock by move constructor and move assignment.
1 parent e628502 commit 721cf5a

File tree

3 files changed

+449
-424
lines changed

3 files changed

+449
-424
lines changed

src/common/dataBlock.cpp

Lines changed: 190 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -13,177 +13,193 @@
1313
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
16-
*/
17-
#include "dataBlock.h"
18-
#include <algorithm>
19-
20-
namespace rocketmq {
21-
MemoryBlock::MemoryBlock() : size(0), data(NULL) {}
22-
23-
MemoryBlock::MemoryBlock(const int initialSize, const bool initialiseToZero)
24-
: size(0), data(NULL) {
25-
if (initialSize > 0) {
26-
size = initialSize;
27-
data = static_cast<char*>(initialiseToZero
28-
? std::calloc(initialSize, sizeof(char))
29-
: std::malloc(initialSize * sizeof(char)));
30-
}
31-
}
32-
33-
MemoryBlock::MemoryBlock(const void* const dataToInitialiseFrom,
34-
const size_t sizeInBytes)
35-
: size(sizeInBytes), data(NULL) {
36-
if (size > 0) {
37-
data = static_cast<char*>(std::malloc(size * sizeof(char)));
38-
39-
if (dataToInitialiseFrom != NULL) memcpy(data, dataToInitialiseFrom, size);
40-
}
41-
}
42-
43-
MemoryBlock::MemoryBlock(const MemoryBlock& other)
44-
: size(other.size), data(NULL) {
45-
if (size > 0) {
46-
data = static_cast<char*>(std::malloc(size * sizeof(char)));
47-
memcpy(data, other.data, size);
48-
}
49-
}
50-
51-
MemoryBlock::~MemoryBlock() { std::free(data); }
52-
53-
MemoryBlock& MemoryBlock::operator=(const MemoryBlock& other) {
54-
if (this != &other) {
55-
setSize(other.size, false);
56-
memcpy(data, other.data, size);
57-
}
58-
59-
return *this;
60-
}
61-
62-
//==============================================================================
63-
bool MemoryBlock::operator==(const MemoryBlock& other) const {
64-
return matches(other.data, other.size);
65-
}
66-
67-
bool MemoryBlock::operator!=(const MemoryBlock& other) const {
68-
return !operator==(other);
69-
}
70-
71-
bool MemoryBlock::matches(const void* dataToCompare, int dataSize) const {
72-
return size == dataSize && memcmp(data, dataToCompare, size) == 0;
73-
}
74-
75-
//==============================================================================
76-
// this will resize the block to this size
77-
void MemoryBlock::setSize(const int newSize, const bool initialiseToZero) {
78-
if (size != newSize) {
79-
if (newSize <= 0) {
80-
reset();
81-
} else {
82-
if (data != NULL) {
83-
data = static_cast<char*>(
84-
data == NULL ? std::malloc(newSize * sizeof(char))
85-
: std::realloc(data, newSize * sizeof(char)));
86-
87-
if (initialiseToZero && (newSize > size))
88-
memset(data + size, 0, newSize - size);
89-
} else {
90-
std::free(data);
91-
data = static_cast<char*>(initialiseToZero
92-
? std::calloc(newSize, sizeof(char))
93-
: std::malloc(newSize * sizeof(char)));
94-
}
95-
96-
size = newSize;
97-
}
98-
}
99-
}
100-
101-
void MemoryBlock::reset() {
102-
std::free(data);
103-
data = NULL;
104-
size = 0;
105-
}
106-
107-
void MemoryBlock::ensureSize(const int minimumSize,
108-
const bool initialiseToZero) {
109-
if (size < minimumSize) setSize(minimumSize, initialiseToZero);
110-
}
111-
112-
//==============================================================================
113-
void MemoryBlock::fillWith(const int value) { memset(data, (int)value, size); }
114-
115-
void MemoryBlock::append(const void* const srcData, const int numBytes) {
116-
if (numBytes > 0) {
117-
const int oldSize = size;
118-
setSize(size + numBytes);
119-
memcpy(data + oldSize, srcData, numBytes);
120-
}
121-
}
122-
123-
void MemoryBlock::replaceWith(const void* const srcData, const int numBytes) {
124-
if (numBytes > 0) {
125-
setSize(numBytes);
126-
memcpy(data, srcData, numBytes);
127-
}
128-
}
129-
130-
void MemoryBlock::insert(const void* const srcData, const int numBytes,
131-
int insertPosition) {
132-
if (numBytes > 0) {
133-
insertPosition = std::min(insertPosition, size);
134-
const int trailingDataSize = size - insertPosition;
135-
setSize(size + numBytes, false);
136-
137-
if (trailingDataSize > 0)
138-
memmove(data + insertPosition + numBytes, data + insertPosition,
139-
trailingDataSize);
140-
141-
memcpy(data + insertPosition, srcData, numBytes);
142-
}
143-
}
144-
145-
void MemoryBlock::removeSection(const int startByte,
146-
const int numBytesToRemove) {
147-
if (startByte + numBytesToRemove >= size) {
148-
setSize(startByte);
149-
} else if (numBytesToRemove > 0) {
150-
memmove(data + startByte, data + startByte + numBytesToRemove,
151-
size - (startByte + numBytesToRemove));
152-
153-
setSize(size - numBytesToRemove);
154-
}
155-
}
156-
157-
void MemoryBlock::copyFrom(const void* const src, int offset, int num) {
158-
const char* d = static_cast<const char*>(src);
159-
160-
if (offset < 0) {
161-
d -= offset;
162-
num += (size_t)-offset;
163-
offset = 0;
164-
}
165-
166-
if ((size_t)offset + num > (unsigned int)size) num = size - (size_t)offset;
167-
168-
if (num > 0) memcpy(data + offset, d, num);
169-
}
170-
171-
void MemoryBlock::copyTo(void* const dst, int offset, int num) const {
172-
char* d = static_cast<char*>(dst);
173-
174-
if (offset < 0) {
175-
memset(d, 0, (size_t)-offset);
176-
d -= offset;
177-
num -= (size_t)-offset;
178-
offset = 0;
179-
}
180-
181-
if ((size_t)offset + num > (unsigned int)size) {
182-
const int newNum = (size_t)size - (size_t)offset;
183-
memset(d + newNum, 0, num - newNum);
184-
num = newNum;
185-
}
186-
187-
if (num > 0) memcpy(d, data + offset, num);
188-
}
189-
}
16+
*/
17+
#include "dataBlock.h"
18+
#include <algorithm>
19+
20+
namespace rocketmq {
21+
22+
MemoryBlock::MemoryBlock() : size(0), data(NULL) {}
23+
24+
MemoryBlock::MemoryBlock(const int initialSize, const bool initialiseToZero)
25+
: size(0), data(NULL) {
26+
if (initialSize > 0) {
27+
size = initialSize;
28+
data = static_cast<char *>(initialiseToZero
29+
? std::calloc(initialSize, sizeof(char))
30+
: std::malloc(initialSize * sizeof(char)));
31+
}
32+
}
33+
34+
MemoryBlock::MemoryBlock(const void *const dataToInitialiseFrom, const size_t sizeInBytes)
35+
: size(sizeInBytes), data(NULL) {
36+
if (size > 0) {
37+
data = static_cast<char *>(std::malloc(size * sizeof(char)));
38+
39+
if (dataToInitialiseFrom != NULL) memcpy(data, dataToInitialiseFrom, size);
40+
}
41+
}
42+
43+
MemoryBlock::MemoryBlock(const MemoryBlock &other) : size(other.size), data(NULL) {
44+
if (size > 0) {
45+
data = static_cast<char *>(std::malloc(size * sizeof(char)));
46+
memcpy(data, other.data, size);
47+
}
48+
}
49+
50+
MemoryBlock::MemoryBlock(MemoryBlock &&other) : size(other.size), data(other.data) {
51+
other.size = 0;
52+
other.data = NULL;
53+
}
54+
55+
MemoryBlock::~MemoryBlock() { std::free(data); }
56+
57+
MemoryBlock &MemoryBlock::operator=(const MemoryBlock &other) {
58+
if (this != &other) {
59+
setSize(other.size, false);
60+
memcpy(data, other.data, size);
61+
}
62+
63+
return *this;
64+
}
65+
66+
MemoryBlock &MemoryBlock::operator=(MemoryBlock &&other) {
67+
if (this != &other) {
68+
std::free(data);
69+
70+
size = other.size;
71+
data = other.data;
72+
73+
other.size = 0;
74+
other.data = NULL;
75+
}
76+
77+
return *this;
78+
}
79+
80+
//==============================================================================
81+
bool MemoryBlock::operator==(const MemoryBlock &other) const {
82+
return matches(other.data, other.size);
83+
}
84+
85+
bool MemoryBlock::operator!=(const MemoryBlock &other) const {
86+
return !operator==(other);
87+
}
88+
89+
bool MemoryBlock::matches(const void *dataToCompare, int dataSize) const {
90+
return size == dataSize && memcmp(data, dataToCompare, size) == 0;
91+
}
92+
93+
//==============================================================================
94+
// this will resize the block to this size
95+
void MemoryBlock::setSize(const int newSize, const bool initialiseToZero) {
96+
if (size != newSize) {
97+
if (newSize <= 0) {
98+
reset();
99+
} else {
100+
if (data != NULL) {
101+
data = static_cast<char *>(
102+
data == NULL ? std::malloc(newSize * sizeof(char))
103+
: std::realloc(data, newSize * sizeof(char)));
104+
105+
if (initialiseToZero && (newSize > size))
106+
memset(data + size, 0, newSize - size);
107+
} else {
108+
std::free(data);
109+
data = static_cast<char *>(initialiseToZero
110+
? std::calloc(newSize, sizeof(char))
111+
: std::malloc(newSize * sizeof(char)));
112+
}
113+
114+
size = newSize;
115+
}
116+
}
117+
}
118+
119+
void MemoryBlock::reset() {
120+
std::free(data);
121+
data = NULL;
122+
size = 0;
123+
}
124+
125+
void MemoryBlock::ensureSize(const int minimumSize, const bool initialiseToZero) {
126+
if (size < minimumSize) setSize(minimumSize, initialiseToZero);
127+
}
128+
129+
//==============================================================================
130+
void MemoryBlock::fillWith(const int value) {
131+
memset(data, (int) value, size);
132+
}
133+
134+
void MemoryBlock::append(const void *const srcData, const int numBytes) {
135+
if (numBytes > 0) {
136+
const int oldSize = size;
137+
setSize(size + numBytes);
138+
memcpy(data + oldSize, srcData, numBytes);
139+
}
140+
}
141+
142+
void MemoryBlock::replaceWith(const void *const srcData, const int numBytes) {
143+
if (numBytes > 0) {
144+
setSize(numBytes);
145+
memcpy(data, srcData, numBytes);
146+
}
147+
}
148+
149+
void MemoryBlock::insert(const void *const srcData, const int numBytes, int insertPosition) {
150+
if (numBytes > 0) {
151+
insertPosition = std::min(insertPosition, size);
152+
const int trailingDataSize = size - insertPosition;
153+
setSize(size + numBytes, false);
154+
155+
if (trailingDataSize > 0)
156+
memmove(data + insertPosition + numBytes, data + insertPosition,
157+
trailingDataSize);
158+
159+
memcpy(data + insertPosition, srcData, numBytes);
160+
}
161+
}
162+
163+
void MemoryBlock::removeSection(const int startByte, const int numBytesToRemove) {
164+
if (startByte + numBytesToRemove >= size) {
165+
setSize(startByte);
166+
} else if (numBytesToRemove > 0) {
167+
memmove(data + startByte, data + startByte + numBytesToRemove, size - (startByte + numBytesToRemove));
168+
169+
setSize(size - numBytesToRemove);
170+
}
171+
}
172+
173+
void MemoryBlock::copyFrom(const void *const src, int offset, int num) {
174+
const char *d = static_cast<const char *>(src);
175+
176+
if (offset < 0) {
177+
d -= offset;
178+
num += (size_t) -offset;
179+
offset = 0;
180+
}
181+
182+
if ((size_t) offset + num > (unsigned int) size) num = size - (size_t) offset;
183+
184+
if (num > 0) memcpy(data + offset, d, num);
185+
}
186+
187+
void MemoryBlock::copyTo(void *const dst, int offset, int num) const {
188+
char *d = static_cast<char *>(dst);
189+
190+
if (offset < 0) {
191+
memset(d, 0, (size_t) -offset);
192+
d -= offset;
193+
num -= (size_t) -offset;
194+
offset = 0;
195+
}
196+
197+
if ((size_t) offset + num > (unsigned int) size) {
198+
const int newNum = (size_t) size - (size_t) offset;
199+
memset(d + newNum, 0, num - newNum);
200+
num = newNum;
201+
}
202+
203+
if (num > 0) memcpy(d, data + offset, num);
204+
}
205+
}

0 commit comments

Comments
 (0)