Skip to content

Commit 78fa1c4

Browse files
committed
Improve performance for Globals
1 parent c469041 commit 78fa1c4

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

nan_persistent_12_inl.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ template<typename T> class PersistentBase {
8484
private:
8585
NAN_INLINE PersistentBase() :
8686
persistent() {}
87-
NAN_INLINE explicit PersistentBase(v8::Persistent<T> that) :
88-
persistent(v8::Isolate::GetCurrent(), that) { }
87+
NAN_INLINE explicit PersistentBase(v8::Persistent<T> that) {
88+
std::memcpy(&that, &persistent, sizeof (v8::Persistent<T>));
89+
}
8990
NAN_INLINE explicit PersistentBase(v8::Local<T> that) :
9091
persistent(v8::Isolate::GetCurrent(), that) { }
9192
template<typename S, typename M> friend class Persistent;
@@ -181,12 +182,12 @@ class Global : public PersistentBase<T> {
181182

182183
template <class S>
183184
NAN_INLINE Global(const PersistentBase<S>& that)
184-
: PersistentBase<T>(that) {
185+
: PersistentBase<T>(v8::Persistent<T>(v8::Isolate::GetCurrent(), that.persistent)) {
185186
TYPE_CHECK(T, S);
186187
}
187188

188189
NAN_INLINE Global(Global&& other) : PersistentBase<T>(other.persistent) {
189-
other.Reset();
190+
other.Empty();
190191
}
191192

192193
NAN_INLINE ~Global() { this->Reset(); }
@@ -195,8 +196,8 @@ class Global : public PersistentBase<T> {
195196
NAN_INLINE Global& operator=(Global<S>&& rhs) {
196197
TYPE_CHECK(T, S);
197198
if (this != &rhs) {
198-
this->Reset(rhs.persistent);
199-
rhs.Reset();
199+
std::memcpy(&rhs.persistent, &this->persistent, sizeof (v8::PersistentBase<S>));
200+
rhs.Empty();
200201
}
201202
return *this;
202203
}
@@ -226,16 +227,16 @@ class Global : public PersistentBase<T> {
226227

227228
template <typename S>
228229
NAN_INLINE Global(const PersistentBase<S> &that)
229-
: PersistentBase<T>(that) {
230+
: PersistentBase<T>(v8::Persistent<T>(v8::Isolate::GetCurrent(), that.persistent)) {
230231
TYPE_CHECK(T, S);
231232
}
232233

233234
/**
234235
* Move constructor.
235236
*/
236237
NAN_INLINE Global(RValue rvalue)
237-
: PersistentBase<T>(rvalue.object.persistent) {
238-
rvalue.object->Reset();
238+
: PersistentBase<T>(rvalue.object->persistent) {
239+
rvalue.object->Empty();
239240
}
240241

241242
NAN_INLINE ~Global() { this->Reset(); }
@@ -246,8 +247,8 @@ class Global : public PersistentBase<T> {
246247
template<typename S>
247248
NAN_INLINE Global &operator=(Global<S> rhs) {
248249
TYPE_CHECK(T, S);
249-
this->Reset(rhs.persistent);
250-
rhs.Reset();
250+
std::memcpy(&rhs.persistent, &this->persistent, sizeof (v8::Persistent<T>));
251+
rhs.Empty();
251252
return *this;
252253
}
253254

nan_persistent_pre_12_inl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,15 @@ class Global : public PersistentBase<T> {
207207

208208
template <typename S>
209209
NAN_INLINE Global(const PersistentBase<S> &that)
210-
: PersistentBase<T>(that) {
210+
: PersistentBase<T>(v8::Persistent<T>::New(that.persistent)) {
211211
TYPE_CHECK(T, S);
212212
}
213213
/**
214214
* Move constructor.
215215
*/
216216
NAN_INLINE Global(RValue rvalue)
217217
: PersistentBase<T>(rvalue.object.persistent) {
218-
rvalue.object->Reset();
218+
rvalue.object->Clear();
219219
}
220220
NAN_INLINE ~Global() { this->Reset(); }
221221
/**
@@ -224,8 +224,8 @@ class Global : public PersistentBase<T> {
224224
template<typename S>
225225
NAN_INLINE Global &operator=(Global<S> rhs) {
226226
TYPE_CHECK(T, S);
227-
this->Reset(rhs.persistent);
228-
rhs.Reset();
227+
this->persistent = rhs.persistent;
228+
rhs.Clear();
229229
return *this;
230230
}
231231
/**

0 commit comments

Comments
 (0)