Skip to content

Commit

Permalink
src: minor updates to FastHrtime
Browse files Browse the repository at this point in the history
- Don’t use 12 as a magic number for the buffer size
- Mark the object as weak (which is conceptually the right thing to do,
  even if there is little practical impact)
- Keep a reference to the `ArrayBuffer` in question for memory tracking

PR-URL: #33851
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
addaleax committed Jun 19, 2020
1 parent 2899588 commit 52de4cb
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/node_process_methods.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "base_object-inl.h"
#include "debug_utils-inl.h"
#include "env-inl.h"
#include "memory_tracker-inl.h"
#include "node.h"
#include "node_errors.h"
#include "node_internals.h"
Expand Down Expand Up @@ -451,8 +452,10 @@ class FastHrtime : public BaseObject {

Local<Object> obj = otmpl->NewInstance(env->context()).ToLocalChecked();

Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), 12);
new FastHrtime(env, obj, ab->GetBackingStore());
Local<ArrayBuffer> ab =
ArrayBuffer::New(env->isolate(),
std::max(sizeof(uint64_t), sizeof(uint32_t) * 3));
new FastHrtime(env, obj, ab);
obj->Set(
env->context(), FIXED_ONE_BYTE_STRING(env->isolate(), "buffer"), ab)
.ToChecked();
Expand All @@ -463,11 +466,16 @@ class FastHrtime : public BaseObject {
private:
FastHrtime(Environment* env,
Local<Object> object,
std::shared_ptr<v8::BackingStore> backing_store)
: BaseObject(env, object), backing_store_(backing_store) {}

void MemoryInfo(MemoryTracker* tracker) const override {}
Local<ArrayBuffer> ab)
: BaseObject(env, object),
array_buffer_(env->isolate(), ab),
backing_store_(ab->GetBackingStore()) {
MakeWeak();
}

void MemoryInfo(MemoryTracker* tracker) const override {
tracker->TrackField("array_buffer", array_buffer_);
}
SET_MEMORY_INFO_NAME(FastHrtime)
SET_SELF_SIZE(FastHrtime)

Expand Down Expand Up @@ -502,6 +510,7 @@ class FastHrtime : public BaseObject {
FastBigInt(FromJSObject<FastHrtime>(args.Holder()));
}

v8::Global<ArrayBuffer> array_buffer_;
std::shared_ptr<BackingStore> backing_store_;
};

Expand Down

0 comments on commit 52de4cb

Please sign in to comment.