Skip to content

Commit

Permalink
core, refactor: assign the value to p directly in the constructor of …
Browse files Browse the repository at this point in the history
…obj_ptr to avoid unnecessary initialization that introduces extra load.
  • Loading branch information
xicilion committed May 27, 2023
1 parent c246353 commit 1fe90c0
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions fibjs/include/obj_ptr.h
Expand Up @@ -120,35 +120,33 @@ class obj_ptr {
}

obj_ptr(T* lp)
: p(_ref_ptr(lp))
{
operator=(lp);
}

obj_ptr(const obj_ptr<T>& lp)
: p(_ref_ptr((T*)lp))
{
operator=(lp);
}

template <class Q>
obj_ptr(const obj_ptr<Q>& lp)
: p(_ref_ptr((Q*)lp))
{
operator=(lp);
}

~obj_ptr()
{
Release();
if (p)
p->Unref();
}

T* operator=(T* lp)
{
if (lp == p)
return lp;

if (lp != NULL)
lp->Ref();

return _attach(lp);
return _attach(_ref_ptr(lp));
}

T* operator=(const obj_ptr<T>& lp)
Expand Down Expand Up @@ -193,6 +191,14 @@ class obj_ptr {
}

private:
T* _ref_ptr(T* lp)
{
if (lp != NULL)
lp->Ref();

return lp;
}

T* _attach(T* p2)
{
T* p1 = p.xchg(p2);
Expand Down

0 comments on commit 1fe90c0

Please sign in to comment.