Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[optimization] free string memory after JSON is parsed. #17479

Merged
merged 5 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion native/cocos/bindings/jswrapper/v8/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,13 +401,18 @@ Object *Object::createJSONObject(const ccstd::string &jsonStr) {
}

Object *Object::createJSONObject(std::u16string &&jsonStr) {
auto v8Str = v8::String::NewExternalTwoByte(__isolate, ccnew internal::ExternalStringResource(std::move(jsonStr)));
auto *external = ccnew internal::ExternalStringResource(std::move(jsonStr));
auto v8Str = v8::String::NewExternalTwoByte(__isolate, external);
if (v8Str.IsEmpty()) {
return nullptr;
}

v8::Local<v8::Context> context = __isolate->GetCurrentContext();
v8::MaybeLocal<v8::Value> ret = v8::JSON::Parse(context, v8Str.ToLocalChecked());

// After v8::JSON::Parse, the memory of u16string could be freed.
external->freeMemory();

if (ret.IsEmpty()) {
return nullptr;
}
Expand Down
22 changes: 22 additions & 0 deletions native/cocos/bindings/jswrapper/v8/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,28 @@ void clearPrivate(v8::Isolate *isolate, ObjectWrap &wrap) {
}
}

// ExternalStringResource
ExternalStringResource::ExternalStringResource(std::u16string &&s)
: _s(std::move(s)) {
minggo marked this conversation as resolved.
Show resolved Hide resolved
}

const uint16_t* ExternalStringResource::data() const {
return reinterpret_cast<const uint16_t*>(_s.data());
}

size_t ExternalStringResource::length() const {
return _s.length();
}

void ExternalStringResource::Dispose() {
delete this;
}

void ExternalStringResource::freeMemory() {
_s.clear();
_s.shrink_to_fit();
}

} // namespace internal
} // namespace se

Expand Down
21 changes: 6 additions & 15 deletions native/cocos/bindings/jswrapper/v8/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,15 @@ void clearPrivate(v8::Isolate *isolate, ObjectWrap &wrap);

class ExternalStringResource : public v8::String::ExternalStringResource {
public:
explicit ExternalStringResource(std::u16string &&s)
: _s(std::move(s)) {

}

explicit ExternalStringResource(std::u16string &&s);
~ExternalStringResource() override = default;

const uint16_t* data() const override {
return reinterpret_cast<const uint16_t*>(_s.data());
}
const uint16_t* data() const override;
size_t length() const override;
void Dispose() override;

void freeMemory();

size_t length() const override {
return _s.length();
}

void Dispose() override {
delete this;
}
private:
std::u16string _s;
};
Expand Down
Loading