Skip to content

Commit 3fdb3f0

Browse files
feat: user c++ strsteam to make it faster
1 parent 77806a9 commit 3fdb3f0

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

cpp/msgpack.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <vector>
66
#include <jsi/jsi.h>
77
#include "cmp.h"
8+
#include <iostream>
9+
#include <sstream>
810

911
using namespace facebook;
1012

@@ -97,13 +99,11 @@ static bool mp_reader(cmp_ctx_t *ctx, void *data, size_t limit)
9799

98100
struct MessagePackWriter
99101
{
100-
std::vector<uint8_t> data;
102+
std::stringstream stream;
101103

102104
size_t write(void *data, size_t count)
103105
{
104-
size_t start = this->data.size();
105-
this->data.resize(start + count);
106-
std::memcpy(this->data.data() + start, data, count);
106+
stream.write(static_cast<const char *>(data), count);
107107
return count;
108108
}
109109
};
@@ -120,7 +120,8 @@ std::vector<uint8_t> write(jsi::Runtime &rt, const jsi::Value &value)
120120
std::unique_ptr<cmp_ctx_s> ctx(new cmp_ctx_s);
121121
cmp_init(ctx.get(), &writer, mp_reader, NULL, mp_writer);
122122
writeValue(ctx.get(), rt, value);
123-
return std::move(writer.data);
123+
const std::string &data = writer.stream.str();
124+
return std::vector<uint8_t>(data.begin(), data.end());
124125
}
125126

126127
jsi::Value readValue(cmp_ctx_t *ctx, jsi::Runtime &rt)

example/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const obj = {
6363
online_status: false,
6464
};
6565

66-
const RUN_CYCLES = 100;
66+
const RUN_CYCLES = 10000;
6767
const n = () => {
6868
console.log('comuting with react-native-msgpack');
6969
const startTime = Date.now();

0 commit comments

Comments
 (0)