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

Use WeakKeyDictionary in decorators.cached #1781

Closed
wants to merge 1 commit into from

Conversation

DanielNoord
Copy link
Collaborator

Steps

  • For new features or bug fixes, add a ChangeLog entry describing what your PR does.
  • Write a good description on what the PR does.

Description

Refs. #1780

@matusvalo would you be able to this change?

Type of Changes

Type
βœ“ πŸ› Bug fix

Details

I use the following script to check for the leakage of NodeNG objects.

from io import StringIO

from pylint.lint import Run, pylinter
from pylint.reporters.text import TextReporter
from pympler import muppy, summary

from astroid import nodes


def get_memory_info(
    should_count_nodes: bool = False, should_tracemalloc: bool = False
) -> None:
    prev_nodes = 0

    for round in range(5):
        # Note counting setup
        count_nodes = 0

        # Run program
        Run(
            ["astroid/nodes/node_ng.py"],
            exit=False,
            reporter=TextReporter(StringIO()),
        )
        # Clear cache
        pylinter.MANAGER.clear_cache()

        # Node counting
        if should_count_nodes:
            for o in muppy.get_objects():
                if isinstance(o, nodes.NodeNG):
                    count_nodes += 1

            print()
            print("Nodes INC:", count_nodes - prev_nodes)
            prev_nodes = count_nodes
            print("Nodes NEW:", prev_nodes)



get_memory_info(should_count_nodes=True)

On main this returns:

Nodes INC: 132313
Nodes NEW: 132313

Nodes INC: 93202
Nodes NEW: 225515

Nodes INC: 92207
Nodes NEW: 317722

Nodes INC: 92147
Nodes NEW: 409869

Nodes INC: 92147
Nodes NEW: 502016

On this PR this returns:

Nodes INC: 105458
Nodes NEW: 105458

Nodes INC: 63140
Nodes NEW: 168598

Nodes INC: 58253
Nodes NEW: 226851

Nodes INC: 58193
Nodes NEW: 285044

Nodes INC: 58193
Nodes NEW: 343237

We're definitely still leaking, but it seems to be better at least.

I ran the pylint test suite and the time was around as I expect and similar to a run against main just before it. So I think the caching is also still working.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 3059245240

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.03%) to 92.391%

Files with Coverage Reduction New Missed Lines %
astroid/brain/brain_typing.py 3 86.11%
Totals Coverage Status
Change from base Build 3042886225: -0.03%
Covered Lines: 9775
Relevant Lines: 10580

πŸ’› - Coveralls

@DanielNoord DanielNoord marked this pull request as draft September 15, 2022 09:38
@DanielNoord
Copy link
Collaborator Author

Closing, as I'm looking into another solution.

@matusvalo
Copy link

Great news @DanielNoord ! I will checknthe PR.

@DanielNoord DanielNoord deleted the cache-weakdict branch September 15, 2022 09:45
@DanielNoord
Copy link
Collaborator Author

@matusvalo Turns out this doesn't work... But I think I might have found another solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants