Skip to content

Commit

Permalink
src: fix gc heuristic for external twobyte strings
Browse files Browse the repository at this point in the history
Large external two-byte strings reported their character length instead
of their byte length, throwing off the garbage collector heuristic by
a factor of two.

PR-URL: #1042
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
bnoordhuis committed Mar 5, 2015
1 parent f5b7e18 commit 826cde8
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/string_bytes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class ExternString: public ResourceType {
public:
~ExternString() override {
delete[] data_;
int64_t change_in_bytes = -static_cast<int64_t>(length_);
isolate()->AdjustAmountOfExternalAllocatedMemory(change_in_bytes);
isolate()->AdjustAmountOfExternalAllocatedMemory(-byte_length());
}

const TypeName* data() const override {
Expand All @@ -40,6 +39,10 @@ class ExternString: public ResourceType {
return length_;
}

int64_t byte_length() const {
return length() * sizeof(*data());
}

static Local<String> NewFromCopy(Isolate* isolate,
const TypeName* data,
size_t length) {
Expand Down Expand Up @@ -69,7 +72,7 @@ class ExternString: public ResourceType {
data,
length);
Local<String> str = String::NewExternal(isolate, h_str);
isolate->AdjustAmountOfExternalAllocatedMemory(length);
isolate->AdjustAmountOfExternalAllocatedMemory(h_str->byte_length());

return scope.Escape(str);
}
Expand Down

0 comments on commit 826cde8

Please sign in to comment.