-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactoring: Memory management #4443
base: master
Are you sure you want to change the base?
Changes from all commits
2a08909
3c73270
1340c27
1cd8e15
3e1a966
178888a
2cb4455
ef7b8c6
8b122d5
2f8e231
119efee
71e9b45
5349be6
d35df8e
bd1ee29
e21c00e
68d7157
62606b6
1cd8b7a
29df871
78c92e8
5ae42ca
a64f83e
74ee635
ac0081d
846ca17
9df27e9
ac5b3f0
47e6a30
7ce62fe
7782954
f54ac45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
The MIT License | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
SET(CMAKE_AUTOMOC ON) | ||
|
||
ADD_EXECUTABLE(benchmarks | ||
EXCLUDE_FROM_ALL | ||
benchmark.cpp | ||
) | ||
|
||
# TODO replace usages of include_directories in src/CMakeLists.txt with target_include_directories | ||
# and remove this line (also in tests/CMakeLists.txt) | ||
target_include_directories(benchmarks PRIVATE $<TARGET_PROPERTY:lmmsobjs,INCLUDE_DIRECTORIES>) | ||
|
||
target_static_libraries(benchmarks PRIVATE lmmsobjs) | ||
target_compile_features(benchmarks PRIVATE cxx_std_17) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#include <thread> | ||
|
||
#include <QtCore/QCoreApplication> | ||
#include <QtCore/QDebug> | ||
|
||
#include "libcds.h" | ||
#include <cds/container/vyukov_mpmc_cycle_queue.h> | ||
|
||
#include "Engine.h" | ||
#include "PerfLog.h" | ||
|
||
#include "LocklessList.h" | ||
#include "MemoryPool.h" | ||
|
||
#include "NotePlayHandle.h" | ||
|
||
using namespace lmms; | ||
|
||
template<typename T> | ||
using LocklessQueue = cds::container::VyukovMPMCCycleQueue<T>; | ||
|
||
template<typename Alloc> | ||
void benchmark_allocator(QString name, Alloc&& alloc, size_t n, size_t I) | ||
{ | ||
using T = typename Alloc::value_type; | ||
constexpr size_t S = sizeof(T); | ||
|
||
std::vector<T*> ptrs{n}; | ||
PerfLogTimer timer(QString("Allocate: %1 x %2 x %3 bytes, %4") | ||
.arg(I).arg(n).arg(S).arg(name)); | ||
|
||
for (size_t i=0; i < I; i++) | ||
{ | ||
for (size_t j=0; j < n; j++) { | ||
ptrs[j] = alloc.allocate(1); | ||
} | ||
for (size_t j=0; j < n; j++) { | ||
alloc.deallocate(ptrs[j], 1); | ||
} | ||
} | ||
} | ||
|
||
|
||
template<typename Alloc> | ||
void benchmark_allocator_threaded(QString name, Alloc&& alloc, size_t n, size_t t) | ||
{ | ||
using T = typename Alloc::value_type; | ||
constexpr size_t S = sizeof(T); | ||
|
||
LocklessQueue<T*> ptrs{n}; | ||
|
||
PerfLogTimer timer(QString("Allocate multi-threaded: %1 x %2 bytes using %3 threads, %4") | ||
.arg(n).arg(S).arg(t).arg(name)); | ||
|
||
std::vector<std::thread> threads; threads.reserve(t*2); | ||
|
||
std::atomic_uint_fast64_t allocated{0}; | ||
std::atomic_uint_fast64_t deallocated{0}; | ||
|
||
for (size_t i=0; i < t; i++) { | ||
threads.emplace_back([&]() { | ||
while(allocated++ < n) { | ||
auto ptr = alloc.allocate(1); | ||
ptrs.push(ptr); | ||
} | ||
}); | ||
} | ||
for (size_t i=0; i < t; i++) { | ||
threads.emplace_back([&]() { | ||
while(deallocated++ < n) { | ||
T* ptr; | ||
while (! ptrs.pop(ptr)); | ||
alloc.deallocate(ptr, 1); | ||
} | ||
}); | ||
} | ||
|
||
for (std::thread& thread : threads) { | ||
thread.join(); | ||
} | ||
} | ||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
new QCoreApplication(argc, argv); | ||
|
||
using Stack = LocklessList<size_t>; | ||
{ | ||
size_t n = 100 * 1000 * 1000; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should make it as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That wouldn't make any difference, would it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It evaluates at compile-time rather than run-time. |
||
Stack stack{n}; | ||
PerfLogTimer timer("LocklessList: Insert 100m entries, single-threaded, pre-allocated"); | ||
for (size_t i=0; i < n; i++) { | ||
stack.push(i); | ||
} | ||
} | ||
{ | ||
size_t n = 50 * 1000 * 1000; | ||
size_t t = 5; | ||
Stack stack{n}; | ||
std::vector<std::thread> threads; threads.reserve(t); | ||
PerfLogTimer timer("LocklessList: Push 50m entries, multi-threaded, pre-allocated"); | ||
|
||
for (int i=0; i < 5; i++) { | ||
threads.emplace_back([&]() { | ||
for (size_t j=0; j < n / t; j++) { | ||
stack.push(j); | ||
} | ||
}); | ||
} | ||
|
||
for (int i=0; i < 5; i++) { | ||
threads.at(i).join(); | ||
} | ||
} | ||
|
||
{ | ||
size_t n = 10 * 1000 * 1000; | ||
constexpr size_t S = 256; | ||
using T = std::array<char, S>; | ||
benchmark_allocator("std::allocator", std::allocator<T>{}, n, 1); | ||
benchmark_allocator("MemoryPool", MemoryPool<T>{n}, n, 1); | ||
} | ||
{ | ||
size_t n = 10 * 1000 * 1000; | ||
constexpr size_t S = 256; | ||
using T = std::array<char, S>; | ||
benchmark_allocator_threaded("std::allocator", std::allocator<T>{}, n, 4); | ||
benchmark_allocator_threaded("MemoryPool", MemoryPool<T>{n}, n, 4); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* BufferManager.h - A buffer caching/memory management system | ||
* BufferPool.h | ||
* | ||
* Copyright (c) 2014 Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi> | ||
* Copyright (c) 2006-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net> | ||
|
@@ -23,8 +23,7 @@ | |
* | ||
*/ | ||
|
||
#ifndef LMMS_BUFFER_MANAGER_H | ||
#define LMMS_BUFFER_MANAGER_H | ||
#pragma once | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I saw you're using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to change existing files. Personally I don't know of any relevant compilers that don't support this and have never had |
||
|
||
#include "lmms_export.h" | ||
#include "lmms_basics.h" | ||
|
@@ -34,7 +33,8 @@ namespace lmms | |
|
||
class SampleFrame; | ||
|
||
class LMMS_EXPORT BufferManager | ||
/// Legacy interface for buffer re-use. Uses MemoryPool internally now. | ||
class LMMS_EXPORT BufferPool | ||
{ | ||
public: | ||
static void init( fpp_t fpp ); | ||
|
@@ -47,5 +47,3 @@ class LMMS_EXPORT BufferManager | |
|
||
|
||
} // namespace lmms | ||
|
||
#endif // LMMS_BUFFER_MANAGER_H |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this todo can be done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or are you referring to #7255 ?