Skip to content

Commit 2ebf465

Browse files
glebmxzyfer
authored andcommitted
SharedPtr: Remove explicit detached bool
`detach()` now fully releases the object, resulting in memory and performance savings. Profile before (compiling bootstrap 4): ``` flat flat% sum% cum cum% 24651348 6.97% 6.97% 24651348 6.97% [[kernel.kallsyms]] 20746241 5.87% 12.84% 20746241 5.87% Sass::SharedPtr::decRefCount 18401663 5.20% 18.04% 20420896 5.78% __libc_malloc 15205959 4.30% 22.34% 15205959 4.30% [libc-2.27.so] 12974307 3.67% 26.01% 14070189 3.98% _int_malloc 10958857 3.10% 29.11% 10958857 3.10% Sass::SharedPtr::incRefCount 9837672 2.78% 31.89% 18433250 5.21% cfree 8770779 2.48% 34.37% 8770779 2.48% Sass::Inspect::operator() ``` Profile after: ``` flat flat% sum% cum cum% 28346546 7.94% 7.94% 28346546 7.94% [[kernel.kallsyms]] 20920665 5.86% 13.80% 20920665 5.86% [libc-2.27.so] 13540151 3.79% 17.60% 14637507 4.10% __libc_malloc 13091487 3.67% 21.26% 13091487 3.67% Sass::SharedPtr::incRefCount 9864675 2.76% 24.03% 9864675 2.76% Sass::SharedPtr::decRefCount 8763387 2.46% 26.48% 8763387 2.46% _int_malloc 7676121 2.15% 28.63% 7676121 2.15% std::vector::_M_realloc_insert 6578211 1.84% 30.48% 6578211 1.84% __dynamic_cast ```
1 parent 5baf0fa commit 2ebf465

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

src/memory/SharedPtr.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ namespace Sass {
3232
bool SharedObj::taint = false;
3333

3434
SharedObj::SharedObj()
35-
: detached(false)
35+
: refcounter(0)
3636
#ifdef DEBUG_SHARED_PTR
3737
, dbg(false)
3838
#endif
3939
{
40-
refcounter = 0;
4140
#ifdef DEBUG_SHARED_PTR
4241
if (taint) all.push_back(this);
4342
#endif
@@ -63,17 +62,14 @@ namespace Sass {
6362
// AST_Node_Ptr ast = dynamic_cast<AST_Node*>(node);
6463
if (node->dbg) std::cerr << "DELETE NODE " << node << "\n";
6564
#endif
66-
if (!node->detached) {
67-
delete(node);
68-
}
65+
delete(node);
6966
}
7067
}
7168
}
7269

7370
void SharedPtr::incRefCount() {
7471
if (node) {
7572
++ node->refcounter;
76-
node->detached = false;
7773
#ifdef DEBUG_SHARED_PTR
7874
if (node->dbg) {
7975
std::cerr << "+ " << node << " X " << node->refcounter << " (" << this << ") " << "\n";
@@ -111,4 +107,4 @@ namespace Sass {
111107
incRefCount();
112108
}
113109

114-
}
110+
}

src/memory/SharedPtr.hpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ namespace Sass {
4949
#endif
5050
static bool taint;
5151
long refcounter;
52-
// long refcount;
53-
bool detached;
5452
#ifdef DEBUG_SHARED_PTR
5553
bool dbg;
5654
#endif
@@ -82,7 +80,7 @@ namespace Sass {
8280
virtual const std::string to_string() const = 0;
8381

8482
virtual ~SharedObj();
85-
long getRefCount() {
83+
long getRefCount() const {
8684
return refcounter;
8785
}
8886
};
@@ -123,11 +121,10 @@ namespace Sass {
123121
bool isNull () const {
124122
return node == NULL;
125123
};
126-
SharedObj* detach() const {
127-
if (node) {
128-
node->detached = true;
129-
}
130-
return node;
124+
SharedObj* detach() {
125+
SharedObj* result = node;
126+
node = NULL;
127+
return result;
131128
};
132129
operator bool() const {
133130
return node != NULL;
@@ -197,8 +194,7 @@ namespace Sass {
197194
T* ptr () const {
198195
return static_cast<T*>(this->obj());
199196
};
200-
T* detach() const {
201-
if (this->obj() == NULL) return NULL;
197+
T* detach() {
202198
return static_cast<T*>(SharedPtr::detach());
203199
}
204200
bool isNull() const {
@@ -214,4 +210,4 @@ namespace Sass {
214210

215211
}
216212

217-
#endif
213+
#endif

0 commit comments

Comments
 (0)