Skip to content

Commit

Permalink
Improve performance for Globals
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoopa committed Oct 12, 2015
1 parent c469041 commit 78fa1c4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
23 changes: 12 additions & 11 deletions nan_persistent_12_inl.h
Expand Up @@ -84,8 +84,9 @@ template<typename T> class PersistentBase {
private:
NAN_INLINE PersistentBase() :
persistent() {}
NAN_INLINE explicit PersistentBase(v8::Persistent<T> that) :
persistent(v8::Isolate::GetCurrent(), that) { }
NAN_INLINE explicit PersistentBase(v8::Persistent<T> that) {
std::memcpy(&that, &persistent, sizeof (v8::Persistent<T>));
}
NAN_INLINE explicit PersistentBase(v8::Local<T> that) :
persistent(v8::Isolate::GetCurrent(), that) { }
template<typename S, typename M> friend class Persistent;
Expand Down Expand Up @@ -181,12 +182,12 @@ class Global : public PersistentBase<T> {

template <class S>
NAN_INLINE Global(const PersistentBase<S>& that)
: PersistentBase<T>(that) {
: PersistentBase<T>(v8::Persistent<T>(v8::Isolate::GetCurrent(), that.persistent)) {
TYPE_CHECK(T, S);
}

NAN_INLINE Global(Global&& other) : PersistentBase<T>(other.persistent) {
other.Reset();
other.Empty();
}

NAN_INLINE ~Global() { this->Reset(); }
Expand All @@ -195,8 +196,8 @@ class Global : public PersistentBase<T> {
NAN_INLINE Global& operator=(Global<S>&& rhs) {
TYPE_CHECK(T, S);
if (this != &rhs) {
this->Reset(rhs.persistent);
rhs.Reset();
std::memcpy(&rhs.persistent, &this->persistent, sizeof (v8::PersistentBase<S>));
rhs.Empty();
}
return *this;
}
Expand Down Expand Up @@ -226,16 +227,16 @@ class Global : public PersistentBase<T> {

template <typename S>
NAN_INLINE Global(const PersistentBase<S> &that)
: PersistentBase<T>(that) {
: PersistentBase<T>(v8::Persistent<T>(v8::Isolate::GetCurrent(), that.persistent)) {
TYPE_CHECK(T, S);
}

/**
* Move constructor.
*/
NAN_INLINE Global(RValue rvalue)
: PersistentBase<T>(rvalue.object.persistent) {
rvalue.object->Reset();
: PersistentBase<T>(rvalue.object->persistent) {
rvalue.object->Empty();
}

NAN_INLINE ~Global() { this->Reset(); }
Expand All @@ -246,8 +247,8 @@ class Global : public PersistentBase<T> {
template<typename S>
NAN_INLINE Global &operator=(Global<S> rhs) {
TYPE_CHECK(T, S);
this->Reset(rhs.persistent);
rhs.Reset();
std::memcpy(&rhs.persistent, &this->persistent, sizeof (v8::Persistent<T>));
rhs.Empty();
return *this;
}

Expand Down
8 changes: 4 additions & 4 deletions nan_persistent_pre_12_inl.h
Expand Up @@ -207,15 +207,15 @@ class Global : public PersistentBase<T> {

template <typename S>
NAN_INLINE Global(const PersistentBase<S> &that)
: PersistentBase<T>(that) {
: PersistentBase<T>(v8::Persistent<T>::New(that.persistent)) {
TYPE_CHECK(T, S);
}
/**
* Move constructor.
*/
NAN_INLINE Global(RValue rvalue)
: PersistentBase<T>(rvalue.object.persistent) {
rvalue.object->Reset();
rvalue.object->Clear();
}
NAN_INLINE ~Global() { this->Reset(); }
/**
Expand All @@ -224,8 +224,8 @@ class Global : public PersistentBase<T> {
template<typename S>
NAN_INLINE Global &operator=(Global<S> rhs) {
TYPE_CHECK(T, S);
this->Reset(rhs.persistent);
rhs.Reset();
this->persistent = rhs.persistent;
rhs.Clear();
return *this;
}
/**
Expand Down

0 comments on commit 78fa1c4

Please sign in to comment.