-
-
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
Open
lukas-w
wants to merge
32
commits into
master
Choose a base branch
from
refac/memory
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 26 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
2a08909
Move aligned malloc to MemoryManager.h, port to stl allocator
lukas-w 3c73270
Rename MemoryManager.h to Memory.h
lukas-w 1340c27
Memory.h fixes
lukas-w 1cd8e15
Replace LocklessAllocator with new MemoryPool class
lukas-w 3e1a966
Replace NotePlayHandleManager implementation with MemoryPool
lukas-w 178888a
BufferManager: Use MemoryPool, rename to BufferPool
lukas-w 2cb4455
Replace AllignedAllocator implementation with rpmalloc calls
lukas-w ef7b8c6
Add naive benchmarks
lukas-w 8b122d5
Fix libcds on MinGW
lukas-w 2f8e231
Fix macOS linking problem
lukas-w 119efee
Fix tests crash due to incomplete cleanup
lukas-w 71e9b45
Memory: Fix wrong rebind
lukas-w 5349be6
NiftyCounter: Fix decrement
lukas-w d35df8e
Move BufferPool::clear to MixHelpers and rewrite
lukas-w bd1ee29
Silence int conversion warning
lukas-w e21c00e
Apply suggestions by @PhysSong
lukas-w 68d7157
Merge branch 'master' into refac/memory
lukas-w 62606b6
Try to fix CircleCI linux build
lukas-w 1cd8b7a
Fix libcds nifty counter typo
lukas-w 29df871
Add nifty counter instance to MmAllocaator
lukas-w 78c92e8
Tests: Allow specifying test suit name
lukas-w 5ae42ca
Try to fix libcds counters issues
lukas-w a64f83e
Fix libcds MSVC compilation
lukas-w 74ee635
Merge remote-tracking branch 'origin/master' into refac/memory
lukas-w ac0081d
Fix crash on exit with MSVC
lukas-w 846ca17
CMake: Build lmms shared library instead of object library
lukas-w 9df27e9
Merge branch 'master' into refac/memory
lukas-w ac5b3f0
Revert irrelevant changes
PhysSong 47e6a30
Merge branch 'master' into refac/memory
PhysSong 7ce62fe
Always build libcds as a static library
PhysSong 7782954
Fix namespace
PhysSong f54ac45
Remove leftover of mingw-std-threads
PhysSong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}") | ||
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}") | ||
INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/include") | ||
INCLUDE_DIRECTORIES("${CMAKE_BINARY_DIR}") | ||
|
||
SET(CMAKE_CXX_STANDARD 11) | ||
|
||
SET(CMAKE_AUTOMOC ON) | ||
|
||
ADD_EXECUTABLE(benchmarks | ||
EXCLUDE_FROM_ALL | ||
benchmark.cpp | ||
) | ||
TARGET_LINK_LIBRARIES(benchmarks lmmslib) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
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; | ||
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("MmAllocator", MmAllocator<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("MmAllocator", MmAllocator<T>{}, n, 4); | ||
benchmark_allocator_threaded("MemoryPool", MemoryPool<T>{n}, n, 4); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think you should make it as
constexpr
.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.
That wouldn't make any difference, would it?
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.
It evaluates at compile-time rather than run-time.