Skip to content

Commit 7d37970

Browse files
committed
Rewrite profiles on object
1 parent 2cfaf74 commit 7d37970

File tree

5 files changed

+21
-53
lines changed

5 files changed

+21
-53
lines changed

src/cpu_profile.cc

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace nodex {
1717
using v8::Value;
1818

1919
Nan::Persistent<ObjectTemplate> Profile::profile_template_;
20-
Nan::Persistent<Array> Profile::profiles;
20+
Nan::Persistent<Object> Profile::profiles;
2121
uint32_t Profile::uid_counter = 0;
2222

2323
NAN_METHOD(Profile_EmptyMethod) {
@@ -36,19 +36,10 @@ namespace nodex {
3636
NAN_METHOD(Profile::Delete) {
3737
Local<Object> self = info.This();
3838
void* ptr = Nan::GetInternalFieldPointer(self, 0);
39-
Local<Array> profiles = Nan::New<Array>(Profile::profiles);
40-
41-
uint32_t count = profiles->Length();
42-
for (uint32_t index = 0; index < count; index++) {
43-
if (profiles->Get(index) == info.This()) {
44-
Local<Value> argv[2] = {
45-
Nan::New<Integer>(index),
46-
Nan::New<Integer>(1)
47-
};
48-
Local<Function>::Cast(profiles->Get(Nan::New<String>("splice").ToLocalChecked()))->Call(profiles, 2, argv);
49-
break;
50-
}
51-
}
39+
Local<Object> profiles = Nan::New<Object>(Profile::profiles);
40+
Local<Value> _uid = info.This()->Get(Nan::New<String>("uid").ToLocalChecked());
41+
Local<String> uid = Nan::To<String>(_uid).ToLocalChecked();
42+
profiles->Delete(uid);
5243
static_cast<CpuProfile*>(ptr)->Delete();
5344
}
5445

@@ -64,15 +55,19 @@ namespace nodex {
6455
Local<Object> profile = Nan::New(profile_template_)->NewInstance();
6556
Nan::SetInternalFieldPointer(profile, 0, const_cast<CpuProfile*>(node));
6657

58+
uint32_t uid_length = (((sizeof uid_counter) * 8) + 2)/3 + 2;
59+
char _uid[uid_length];
60+
sprintf(_uid, "%d", uid_counter);
61+
6762
Local<Value> CPU = Nan::New<String>("CPU").ToLocalChecked();
68-
Local<Value> uid = Nan::New<Integer>(uid_counter);
63+
Local<Value> uid = Nan::New<String>(_uid).ToLocalChecked();
6964
#if (NODE_MODULE_VERSION >= 45)
7065
Local<String> title = node->GetTitle();
7166
#else
7267
Local<String> title = Nan::New(node->GetTitle());
7368
#endif
7469
if (!title->Length()) {
75-
char _title[32];
70+
char _title[8 + uid_length];
7671
sprintf(_title, "Profile %i", uid_counter);
7772
title = Nan::New<String>(_title).ToLocalChecked();
7873
}
@@ -101,8 +96,8 @@ namespace nodex {
10196
profile->Set(Nan::New<String>("timestamps").ToLocalChecked(), timestamps);
10297
#endif
10398

104-
Local<Array> profiles = Nan::New<Array>(Profile::profiles);
105-
profiles->Set(profiles->Length(), profile);
99+
Local<Object> profiles = Nan::New<Object>(Profile::profiles);
100+
profiles->Set(uid, profile);
106101

107102
return scope.Escape(profile);
108103
}

src/cpu_profile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace nodex {
99
class Profile {
1010
public:
1111
static v8::Local<v8::Value> New(const v8::CpuProfile* node);
12-
static Nan::Persistent<v8::Array> profiles;
12+
static Nan::Persistent<v8::Object> profiles;
1313
private:
1414
static NAN_METHOD(Delete);
1515
static void Initialize();

test/binding.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ describe('binding', function() {
5050
it('should delete itself from profiler cache', function() {
5151
cpu.startProfiling('', true);
5252
var profile = cpu.stopProfiling();
53-
var oldProfilesLength = cpu.profiles.length;
53+
var oldProfilesLength = Object.keys(cpu.profiles).length;
5454
profile.delete();
55-
expect(cpu.profiles.length == oldProfilesLength - 1).to.equal(true);
55+
expect(oldProfilesLength - Object.keys(cpu.profiles).length).to.equal(1);
5656
});
5757
});
5858

test/v8-profiler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('v8-profiler', function() {
1818
});
1919

2020
it('should cache profiles', function() {
21-
expect(profiler.profiles).to.have.length(1);
21+
expect(Object.keys(profiler.profiles)).to.have.length(1);
2222
});
2323

2424
it('should replace profile title, if started with name argument', function() {
@@ -73,8 +73,8 @@ describe('v8-profiler', function() {
7373
});
7474

7575
function deleteAllProfiles() {
76-
profiler.profiles.slice().forEach(function(profile) {
77-
profile.delete();
76+
Object.keys(profiler.profiles).forEach(function(key) {
77+
profiler.profiles[key].delete();
7878
});
7979
}
8080
});

v8-profiler.js

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -152,22 +152,6 @@ var profiler = {
152152
return snapshot;
153153
},
154154

155-
getSnapshot: function(index) {
156-
var snapshot = binding.heap.snapshots[index];
157-
if (!snapshot) return;
158-
snapshot.__proto__ = Snapshot.prototype;
159-
return snapshot;
160-
},
161-
162-
findSnapshot: function(uid) {
163-
var snapshot = binding.heap.snapshots.filter(function(snapshot) {
164-
return snapshot.uid == uid;
165-
})[0];
166-
if (!snapshot) return;
167-
snapshot.__proto__ = Snapshot.prototype;
168-
return snapshot;
169-
},
170-
171155
deleteAllSnapshots: function () {
172156
Object.keys(binding.heap.snapshots).forEach(function(key) {
173157
binding.heap.snapshots[key].delete();
@@ -258,20 +242,9 @@ var profiler = {
258242
binding.cpu.setSamplingInterval(num);
259243
},
260244

261-
getProfile: function(index) {
262-
return binding.cpu.profiles[index];
263-
},
264-
265-
findProfile: function(uid) {
266-
var profile = binding.cpu.profiles.filter(function(profile) {
267-
return profile.uid == uid;
268-
})[0];
269-
return profile;
270-
},
271-
272245
deleteAllProfiles: function() {
273-
binding.cpu.profiles.forEach(function(profile) {
274-
profile.delete();
246+
Object.keys(binding.cpu.profiles).forEach(function(key) {
247+
binding.cpu.profiles[key].delete();
275248
});
276249
}
277250
};

0 commit comments

Comments
 (0)