Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive

Comments

Improve performance of GC.stats#2164

Closed
mihails-strasuns-sociomantic wants to merge 1 commit intodlang:masterfrom
mihails-strasuns-sociomantic:faster-stats
Closed

Improve performance of GC.stats#2164
mihails-strasuns-sociomantic wants to merge 1 commit intodlang:masterfrom
mihails-strasuns-sociomantic:faster-stats

Conversation

@mihails-strasuns-sociomantic
Copy link
Contributor

Turns GC.stats() from O(n) to O(1) call, calculating used and free memory on the fly as part of regular GC operation. That makes a very big performance difference for GC.stats() usage in apps that allocate lot of GC memory.

Right now it I only confirmed it to produce same stats as previous implementation in synthetic examples. Going to test on some real-world projects in a day or two. For now general feedback from those who know about GC internals would be appreciated.

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @mihails-strasuns-sociomantic!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub fetch digger
dub run digger -- build "master + druntime#2164"

src/gc/stats.d Outdated
/**
* Utility to simplify calculating `core.memory.GC.Stats`
*
* Copyright: Copyright Digital Mars 2018.
Copy link
Member

Choose a reason for hiding this comment

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

Should be copyright the D Language Foundation

*
* Copyright: Copyright Digital Mars 2018.
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
*/
Copy link
Member

Choose a reason for hiding this comment

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

Please add:

Source: link to github source code

src/gc/stats.d Outdated
import core.memory;
GC.Stats stats;

/// Record new memory added to the GC pool from OS
Copy link
Member

Choose a reason for hiding this comment

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

The style for Ddoc functions is:

/*********
 * Record new memory added to the GC pool from the OS.
 * Params:
 *   bytes = amount of new memory added
 */

Yes, it looks rather redundant, but tools expect this format.

GC.Stats stats;

/// Record new memory added to the GC pool from OS
void added(size_t bytes)
Copy link
Member

Choose a reason for hiding this comment

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

looks like @safe and pure can be added, too

auto p = size <= 2048 ? smallAlloc(binTable[size], alloc_size, bits)
: bigAlloc(size, alloc_size, bits);

statsTracker.allocated(alloc_size);
Copy link
Member

Choose a reason for hiding this comment

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

Since this function is performance critical, being about to turn this statement into a no-op is important.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you mean, pragma(inline)?

Copy link
Member

Choose a reason for hiding this comment

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

By no-op I mean no code is generated for the statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am afraid it is not possible - GC.stats is supposed to work for every program at any moment of time, it is not controlled by version or runtime flag.

Though considering how extremely expensive call to alloc is, I am not sure I understand concern about 2 extra integer additions either.

Copy link
Member

@WalterBright WalterBright left a comment

Choose a reason for hiding this comment

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

Approved with suggested changes.

@mihails-strasuns-sociomantic
Copy link
Contributor Author

Blocked for now, need to reconsider how this is implemented - current approach is just too fragile.

@jacob-carlborg
Copy link
Contributor

@mihails-strasuns-sociomantic can we close this while figuring out a better solution?

@mihails-strasuns-sociomantic
Copy link
Contributor Author

Yes, I was only keeping it as a reference for internal discussion.

For the record, I consider two orthogonal approaches to address this problem:

  • Modify GC.stats interface to allow caller to request specific stats to calculate (to fix bloating of GC API with new methods when someone wants to add new stats but doesn't need rest)
  • Refactor GC implementation to make all pool management happen through fixed API so that some form of this PR becomes viable again (right now it is too fragile against any GC changes to consider seriously).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants