Skip to content

Commit f80bbb8

Browse files
authored
Optimize append data to buffer (#1)
1 parent eae005b commit f80bbb8

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

Buffer.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ void Buffer::clear() noexcept {
2121
buffer.clear();
2222
}
2323

24+
void Buffer::write(char * str, long length) noexcept {
25+
buffer.insert(buffer.end(), str, str + length);
26+
}
27+
2428
void Buffer::discard(unsigned long long n) {
2529
if (buffer.size() < n)
2630
throw BufferOverflow();
@@ -33,7 +37,7 @@ unsigned long long Buffer::size() const noexcept {
3337
}
3438

3539
bool Buffer::empty() const noexcept {
36-
return buffer.size() == 0;
40+
return buffer.empty();
3741
}
3842

3943
void Buffer::merge(const Buffer &other) noexcept {

Buffer.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Buffer {
2020
Buffer(const std::vector<unsigned char>&) noexcept;
2121

2222
void clear() noexcept;
23+
void write(char * str, long length) noexcept;
2324
void discard(unsigned long long n);
2425

2526
unsigned long long size() const noexcept;

buffer.cc

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,6 @@ static zend_class_entry *buffer_object_ce = NULL;
2121
static zend_class_entry *buffer_exception_ce = NULL;
2222
static zend_object_handlers buffer_object_handlers;
2323

24-
static void copy_zend_string_to_buffer(zend_string *str, Buffer *data)
25-
{
26-
char *input = ZSTR_VAL(str);
27-
zend_long input_len = ZSTR_LEN(str);
28-
29-
for (int i = 0; i < input_len; i++)
30-
data->appendInt8(input[i]);
31-
}
32-
3324
static zend_object* buffer_object_to_zend_object(buffer_object *objval)
3425
{
3526
return ((zend_object*)(objval + 1)) - 1;
@@ -68,7 +59,7 @@ PHP_METHOD(ByteBuffer, __construct)
6859
}
6960

7061
if (Z_TYPE_P(val) == IS_STRING) {
71-
copy_zend_string_to_buffer(Z_STR_P(val), objval->data);
62+
objval->data->write(Z_STRVAL_P(val), Z_STRLEN_P(val));
7263
} else if (Z_TYPE_P(val) == IS_OBJECT && instanceof_function(Z_OBJCE_P(val), buffer_object_ce) != 0) {
7364
buffer_object *appval = buffer_object_from_zend_object(Z_OBJ_P(val));
7465

@@ -127,7 +118,7 @@ PHP_METHOD(ByteBuffer, append)
127118
}
128119

129120
if (Z_TYPE_P(val) == IS_STRING) {
130-
copy_zend_string_to_buffer(Z_STR_P(val), objval->data);
121+
objval->data->write(Z_STRVAL_P(val), Z_STRLEN_P(val));
131122
} else if (Z_TYPE_P(val) == IS_OBJECT && instanceof_function(Z_OBJCE_P(val), buffer_object_ce) != 0) {
132123
buffer_object *appval = buffer_object_from_zend_object(Z_OBJ_P(val));
133124

0 commit comments

Comments
 (0)