diff --git a/src/cpu_profile.cc b/src/cpu_profile.cc index d2dd4ea..fb54da3 100644 --- a/src/cpu_profile.cc +++ b/src/cpu_profile.cc @@ -17,7 +17,7 @@ namespace nodex { using v8::Value; Nan::Persistent Profile::profile_template_; - Nan::Persistent Profile::profiles; + Nan::Persistent Profile::profiles; uint32_t Profile::uid_counter = 0; NAN_METHOD(Profile_EmptyMethod) { @@ -36,19 +36,10 @@ namespace nodex { NAN_METHOD(Profile::Delete) { Local self = info.This(); void* ptr = Nan::GetInternalFieldPointer(self, 0); - Local profiles = Nan::New(Profile::profiles); - - uint32_t count = profiles->Length(); - for (uint32_t index = 0; index < count; index++) { - if (profiles->Get(index) == info.This()) { - Local argv[2] = { - Nan::New(index), - Nan::New(1) - }; - Local::Cast(profiles->Get(Nan::New("splice").ToLocalChecked()))->Call(profiles, 2, argv); - break; - } - } + Local profiles = Nan::New(Profile::profiles); + Local _uid = info.This()->Get(Nan::New("uid").ToLocalChecked()); + Local uid = Nan::To(_uid).ToLocalChecked(); + profiles->Delete(uid); static_cast(ptr)->Delete(); } @@ -64,15 +55,19 @@ namespace nodex { Local profile = Nan::New(profile_template_)->NewInstance(); Nan::SetInternalFieldPointer(profile, 0, const_cast(node)); + const uint32_t uid_length = (((sizeof uid_counter) * 8) + 2)/3 + 2; + char _uid[uid_length]; + sprintf(_uid, "%d", uid_counter); + Local CPU = Nan::New("CPU").ToLocalChecked(); - Local uid = Nan::New(uid_counter); + Local uid = Nan::New(_uid).ToLocalChecked(); #if (NODE_MODULE_VERSION >= 45) Local title = node->GetTitle(); #else Local title = Nan::New(node->GetTitle()); #endif if (!title->Length()) { - char _title[32]; + char _title[8 + uid_length]; sprintf(_title, "Profile %i", uid_counter); title = Nan::New(_title).ToLocalChecked(); } @@ -101,8 +96,8 @@ namespace nodex { profile->Set(Nan::New("timestamps").ToLocalChecked(), timestamps); #endif - Local profiles = Nan::New(Profile::profiles); - profiles->Set(profiles->Length(), profile); + Local profiles = Nan::New(Profile::profiles); + profiles->Set(uid, profile); return scope.Escape(profile); } diff --git a/src/cpu_profile.h b/src/cpu_profile.h index b6bc61d..2d10569 100644 --- a/src/cpu_profile.h +++ b/src/cpu_profile.h @@ -9,7 +9,7 @@ namespace nodex { class Profile { public: static v8::Local New(const v8::CpuProfile* node); - static Nan::Persistent profiles; + static Nan::Persistent profiles; private: static NAN_METHOD(Delete); static void Initialize(); diff --git a/test/binding.js b/test/binding.js index bcf5efb..e1b0e83 100644 --- a/test/binding.js +++ b/test/binding.js @@ -50,9 +50,9 @@ describe('binding', function() { it('should delete itself from profiler cache', function() { cpu.startProfiling('', true); var profile = cpu.stopProfiling(); - var oldProfilesLength = cpu.profiles.length; + var oldProfilesLength = Object.keys(cpu.profiles).length; profile.delete(); - expect(cpu.profiles.length == oldProfilesLength - 1).to.equal(true); + expect(oldProfilesLength - Object.keys(cpu.profiles).length).to.equal(1); }); }); diff --git a/test/v8-profiler.js b/test/v8-profiler.js index 825a7bc..ad21d16 100644 --- a/test/v8-profiler.js +++ b/test/v8-profiler.js @@ -18,7 +18,7 @@ describe('v8-profiler', function() { }); it('should cache profiles', function() { - expect(profiler.profiles).to.have.length(1); + expect(Object.keys(profiler.profiles)).to.have.length(1); }); it('should replace profile title, if started with name argument', function() { @@ -73,8 +73,8 @@ describe('v8-profiler', function() { }); function deleteAllProfiles() { - profiler.profiles.slice().forEach(function(profile) { - profile.delete(); + Object.keys(profiler.profiles).forEach(function(key) { + profiler.profiles[key].delete(); }); } }); diff --git a/v8-profiler.js b/v8-profiler.js index 2f6621d..56f0a9c 100644 --- a/v8-profiler.js +++ b/v8-profiler.js @@ -152,22 +152,6 @@ var profiler = { return snapshot; }, - getSnapshot: function(index) { - var snapshot = binding.heap.snapshots[index]; - if (!snapshot) return; - snapshot.__proto__ = Snapshot.prototype; - return snapshot; - }, - - findSnapshot: function(uid) { - var snapshot = binding.heap.snapshots.filter(function(snapshot) { - return snapshot.uid == uid; - })[0]; - if (!snapshot) return; - snapshot.__proto__ = Snapshot.prototype; - return snapshot; - }, - deleteAllSnapshots: function () { Object.keys(binding.heap.snapshots).forEach(function(key) { binding.heap.snapshots[key].delete(); @@ -258,20 +242,9 @@ var profiler = { binding.cpu.setSamplingInterval(num); }, - getProfile: function(index) { - return binding.cpu.profiles[index]; - }, - - findProfile: function(uid) { - var profile = binding.cpu.profiles.filter(function(profile) { - return profile.uid == uid; - })[0]; - return profile; - }, - deleteAllProfiles: function() { - binding.cpu.profiles.forEach(function(profile) { - profile.delete(); + Object.keys(binding.cpu.profiles).forEach(function(key) { + binding.cpu.profiles[key].delete(); }); } };