This is an automated email from the ASF dual-hosted git repository.
morningman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-doris.git
The following commit(s) were added to refs/heads/master by this push:
new 9dd54b8 [optimize] avoid extra memory alloc in object pool (#5871)
9dd54b8 is described below
commit 9dd54b83b81da5384be3b6c7588d3a296ad0889a
Author: stdpain <[email protected]>
AuthorDate: Wed May 26 09:58:21 2021 +0800
[optimize] avoid extra memory alloc in object pool (#5871)
---
be/src/common/object_pool.h | 33 ++++++++++++---------------------
1 file changed, 12 insertions(+), 21 deletions(-)
diff --git a/be/src/common/object_pool.h b/be/src/common/object_pool.h
index 78cde83..db82bdf 100644
--- a/be/src/common/object_pool.h
+++ b/be/src/common/object_pool.h
@@ -30,51 +30,42 @@ namespace doris {
// Thread-safe.
class ObjectPool {
public:
- ObjectPool() : _objects() {}
+ ObjectPool() = default;
~ObjectPool() { clear(); }
template <class T>
T* add(T* t) {
- // Create the object to be pushed to the shared vector outside the
critical section.
// TODO: Consider using a lock-free structure.
- SpecificElement<T>* obj = new SpecificElement<T>(t);
- DCHECK(obj != NULL);
std::lock_guard<SpinLock> l(_lock);
- _objects.push_back(obj);
+ _objects.emplace_back(Element{t, [](void* obj) { delete
reinterpret_cast<T*>(obj); }});
return t;
}
void clear() {
std::lock_guard<SpinLock> l(_lock);
- for (auto i = _objects.rbegin(); i != _objects.rend(); ++i) {
- delete *i;
- }
+ for (Element& elem : _objects) elem.delete_fn(elem.obj);
_objects.clear();
}
- // Absorb all objects from src pool
- // Note: This method is not thread safe
void acquire_data(ObjectPool* src) {
_objects.insert(_objects.end(), src->_objects.begin(),
src->_objects.end());
src->_objects.clear();
}
private:
- struct GenericElement {
- virtual ~GenericElement() {}
- };
+ DISALLOW_COPY_AND_ASSIGN(ObjectPool);
- template <class T>
- struct SpecificElement : GenericElement {
- SpecificElement(T* t) : t(t) {}
- ~SpecificElement() { delete t; }
+ /// A generic deletion function pointer. Deletes its first argument.
+ using DeleteFn = void (*)(void*);
- T* t;
+ /// For each object, a pointer to the object and a function that deletes
it.
+ struct Element {
+ void* obj;
+ DeleteFn delete_fn;
};
-
- typedef std::vector<GenericElement*> ElementVector;
- ElementVector _objects;
+
+ std::vector<Element> _objects;
SpinLock _lock;
};
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]