Skip to content
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
wants to merge 32 commits into
base: master
Choose a base branch
from
Open

Refactoring: Memory management #4443

wants to merge 32 commits into from

Conversation

lukas-w
Copy link
Member

@lukas-w lukas-w commented Jun 21, 2018

Copying description from #4311, which for some reason GitHub won't let me reopen. I made these changes in November and April, but didn't have time to fix them on all platforms back then.


Some refactorings regarding memory management and containers.

  • MemoryManager.h renamed to Memory.h
  • MemoryHelper::alignedMalloc moved to Memory.h, ported to an stl-compatible allocator called AlignedAllocator, implementation replaced by a single rpmalloc call.
  • LocklessAllocator implementation replaced by a new MemoryPool class, which now also replaces the implementations of BufferPool (formerly BufferManager) and NotePlayHandlePool (formerly NotePlayHandleManager). Instead of a custom-written lockless stack, MemoryPool uses a 3rdparty lockless queue implementation which yields much better performance.
  • Add some tests and naïve micro-benchmarks.

This includes a a new submodule, libcds, which is a collection of concurrent and lock-free data structures.

Starting with this PR, I'd like to license my contributions as MIT, while the project as a whole is still licensed as GPL. Hope that doesn't cause any trouble.

@zonkmachine
Copy link
Member

...which for some reason GitHub won't let me reopen.

Because you force pushed to the branch while it was closed?

@lukas-w
Copy link
Member Author

lukas-w commented Jun 22, 2018

That's probably it. Didn't know it would cause that.

include/Memory.h Outdated Show resolved Hide resolved

using Stack = LocklessList<size_t>;
{
size_t n = 100 * 1000 * 1000;
Copy link
Member

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.

Copy link
Member Author

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?

Copy link
Contributor

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.

src/core/BufferPool.cpp Outdated Show resolved Hide resolved
include/NiftyCounter.h Outdated Show resolved Hide resolved
@lukas-w
Copy link
Member Author

lukas-w commented Aug 3, 2018

The merge conflicts grew a bit out of hand so I rebased on top of master another time.

@PhysSong PhysSong mentioned this pull request Aug 6, 2018
16 tasks
@tresf tresf mentioned this pull request Feb 9, 2019
@jasp00
Copy link
Member

jasp00 commented Feb 22, 2019

I'd like to license my contributions as MIT, while the project as a whole is still licensed as GPL.

I think you are wrong, but I respect your decision regarding your contributions. You can even license all your past contributions under MIT. However, there is a problem when you add MIT license headers to files because you are affecting future contributions of others. In this case, you are touching the core of LMMS; in my humble opinion, you are breaking project consensus.

Solution: state your permission in LICENSE.txt and use GPL headers for the other files.

@lukas-w
Copy link
Member Author

lukas-w commented Feb 26, 2019

I already stated my permission to re-license all my past contributions in #3412 (comment), so did a handful of other contributors.

in my humble opinion, you are breaking project consensus

I understand your concern, but I don't think this is the case here, because (a) as can be seen in discussions like #3412, there is no clear consensus on the GPL, and (b) developers who don't want to license their contributions to my files under the MIT can simply change the file's license to GPL.

@jasp00
Copy link
Member

jasp00 commented Feb 26, 2019

I already stated my permission

If you do not want to change LICENSE.txt, that is fine.

developers who don't want to license their contributions to my files under the MIT can simply change the file's license to GPL.

Do you mind if I change those headers now?

@lukas-w
Copy link
Member Author

lukas-w commented Feb 26, 2019

If you do not want to change LICENSE.txt, that is fine.

LICENSE.txt is the file that contains the license the project as a whole is published under. It's not the place for this.

developers who don't want to license their contributions to my files under the MIT can simply change the file's license to GPL.

Do you mind if I change those headers now?

Yes I do, because I wouldn't call it a contribution to my files, but an attempt to enforce your opinion on others.

@jasp00
Copy link
Member

jasp00 commented Feb 26, 2019

Code is authoritative. Your headers state that files are under MIT, thus contributions must be under MIT. Please be unambiguous in code, not just on GitHub issues.

@lukas-w
Copy link
Member Author

lukas-w commented Feb 26, 2019

What exactly is ambiguous in your eyes?

@jasp00
Copy link
Member

jasp00 commented Feb 26, 2019

Again: your headers state that files are under MIT, thus contributions must be under MIT. Is this your intention?

@lukas-w
Copy link
Member Author

lukas-w commented Feb 26, 2019

Yes, the files are under MIT and any contributions to them will therefore be under MIT as well, unless the contributor decides to sublicense the file because he doesn't want his contributions to be under MIT.

@jasp00
Copy link
Member

jasp00 commented Feb 26, 2019

unless the contributor decides to sublicense the file

This is ambiguous: sublicensing has nothing to do with contributions. You are forcing MIT onto contributors. This is a GPL project.

Hope that doesn't cause any trouble.

It does. I have told you a way to add your permission; if you do not like to use LICENSE.txt, use another file.

@lukas-w
Copy link
Member Author

lukas-w commented Feb 27, 2019

I'm not forcing the MIT onto anyone. It's compatible with the GPL and thus MIT files can be relicensed under the GPL. Please explain why you think this wouldn't be possible.

I have told you a way to add your permission

Yes but I don't wish to add permission, I wish to license the files under MIT. This makes future versions of them MIT as well as long as contributors are fine with it.

@jasp00
Copy link
Member

jasp00 commented Feb 27, 2019

This makes future versions of them MIT as well as long as contributors are fine with it.

You still want to remain ambiguous, but your code and your intention are clear: you are forcing MIT in those files.

I object to your refactoring. If you go ahead, I would like a configure option to use the current implementation instead.

@lukas-w
Copy link
Member Author

lukas-w commented Feb 28, 2019

You still want to remain ambiguous

I tried to explain what I'm doing and why it's not forcing the MIT onto anyone. Licensing a file as MIT doesn't force contributions to that file to be MIT, it just makes it the default option. If I'm being unclear about something, just ask specifically and I will gladly try to clarify it. If I'm wrong about the legal implications of MIT licensing, feel free to correct me, but please at least try explaining why you think I'm wrong. I don't know what else is left to say when you insist on reiterating your statements without actually responding to what I write.

your code and your intention are clear: you are forcing MIT in those files.

Please refrain from making such claims about my intentions when I've just told you differently. I prefer not to be accused of lying or of intentionally obscuring the truth.

I would like a configure option to use the current implementation instead.

This is absurd. No such thing will happen.

@jasp00
Copy link
Member

jasp00 commented Feb 28, 2019

 * This file is licensed under the MIT license. See LICENSE.MIT.txt file in the
 * project root for details.

This is what matters, not whatever you are writing in this pull request. If you want to be unambiguous, just add your explanation: "Contributions to this file are not required to be under MIT, but I request so".

@tresf
Copy link
Member

tresf commented Mar 12, 2019

I'm not forcing the MIT onto anyone. It's compatible with the GPL and thus MIT files can be relicensed under the GPL.

@jasp00 is correct. Future contributions to this file are forced under MIT unless the project removes the license.

This is fine though. We do it for CMake files. @jasp00 please let this issue rest. These files you're attacking are written by one person that's chosen to make them:

  1. Scalable to other projects, not just LMMS
  2. Compatible with our current license
  3. Isolated enough from our source code that they won't cause the ambiguity that you speak of

"Contributions to this file are not required to be under MIT, but I request so".

No, they're MIT. That's where you're right @jasp00. But that's fine. If the project chooses to downgrade to GPL we can do so at any time. @lukas-w has given us the freedom to do that.

@jasp00
Copy link
Member

jasp00 commented Mar 12, 2019

  1. Compatible with our current license

Thanks. Double standard will make my life easier.

@tresf
Copy link
Member

tresf commented Mar 12, 2019

Thanks. Double standard will make my life easier.

As indicated, we already have this exception on CMake files. Assuming "double standard" means "two standards", and not the proverbial English slang for "not being treated fairly".

src/CMakeLists.txt Outdated Show resolved Hide resolved
Resolves some inexplicable linking errors on Windows. Saves us from
working around incomplete CMake support of object libraries.
@lukas-w
Copy link
Member Author

lukas-w commented May 22, 2020

Ok, builds should pass now. Thank you for the great help with the MSVC build @PhysSong, I applied your steps in a64f83e. Sorry for the repeated force-pushing which is something I usually try to avoid doing, but this has been a lot of trial-and-error.

@JohannesLorenz
Copy link
Contributor

What's missing in this PR to be merged?

@lukas-w
Copy link
Member Author

lukas-w commented Sep 5, 2020

@JohannesLorenz Just review and testing I guess.

include/Memory.h Outdated
};

static MemoryManager::MmCounter _mm_counter;
static thread_local MemoryManager::MmCounter _mm_thread_counter;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you have thread local MmCounter here and ThreadGuard in Memory.cpp?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for missing this question. I don't remember exactly to be honest. The MmCounter was introduced in ac0081d to fix a crash in MSVC, IIRC it would call rpmalloc_finalize before rpmalloc_thread_finalize. I realize this whole Nifty Counter solution may be a bit fragile to use, so I'm open to searching a different solution if we can't make it work reliably.

@PhysSong
Copy link
Member

@lukas-w Sorry for bumping, but could you resolve conflicts again?

@lukas-w
Copy link
Member Author

lukas-w commented Dec 11, 2020

@PhysSong Sure, done.

@PhysSong
Copy link
Member

Things to check before merging:

  • Does the change from lmmsobjs to lmmslib make no regressions or additional issues?
  • The new Nifty counter logic works as expected?

@PhysSong PhysSong removed the needs style review A style review is currently required for this PR label Dec 11, 2020
@Rossmaxx
Copy link
Contributor

Rebase?

@PhysSong
Copy link
Member

Rebase?

On it.

@Rossmaxx
Copy link
Contributor

Does this PR still has a point after #7128 ?

@PhysSong
Copy link
Member

Does this PR still has a point after #7128 ?

I think so because it includes some useful changes such as memory pool, etc.

@TechnoPorg
Copy link
Contributor

The MemoryHelper::alignedMalloc function that is changed in this PR could be replaced with std::aligned_malloc now.

As far as I can tell, the single place MemoryHelper::alignedMalloc is used is in AudioEngine, for allocating output buffers, and I'm not sure why it's even needed there. The only reason I could think of would be to optimize cache misses, but that doesn't seem right.

@Rossmaxx
Copy link
Contributor

The MemoryHelper::alignedMalloc function that is changed in this PR could be replaced with std::aligned_malloc now.

I hope this is a drop in replacement. Btw it's std::aligned_alloc.

@Rossmaxx
Copy link
Contributor

Rossmaxx commented Jun 21, 2024

The MemoryHelper::alignedMalloc function that is changed in this PR could be replaced with std::aligned_malloc now.

Opened a PR at #7335 . Thanks for the heads up.

@PhysSong
Copy link
Member

PhysSong commented Oct 6, 2024

I removed some irrelevant change and then updated the branch. This PR will still require some minor changes, I guess.

benchmark.cpp
)

# TODO replace usages of include_directories in src/CMakeLists.txt with target_include_directories
Copy link
Contributor

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.

Copy link
Contributor

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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs code review A functional code review is currently required for this PR needs testing This pull request needs more testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants