GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-1635741: port _elementtree to multi-phase init (PEP 489) by koubaa · Pull Request #23535 · python/cpython

Conversation

@koubaa

@koubaa koubaa commented Nov 28, 2020 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

https://bugs.python.org/issue1635741

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@koubaa
post _elementtree to multi-phase init
d5b1fb6
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Nov 28, 2020
@bedevere-bot bedevere-bot added the awaiting review label Nov 28, 2020
@koubaa

koubaa commented Nov 28, 2020

Copy link
Copy Markdown
Contributor Author

@vstinner @shihai1991 @corona10 please review

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

tiran
tiran suggested changes Nov 28, 2020
View reviewed changes

@tiran tiran left a comment

Copy link
Copy Markdown
Member

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.

The patch looks like it was a lot of work. Thanks for investing your time to port _elementtree.

I'm a bit worried that the module state getter is going to affect performance negatively. The code has to use the slow _PyType_GetModuleByDef() API. In my patch for the _ssl module I added a module state pointer to instance structs. For example you could add state to typedef struct ElementObject and then reference the state with self->state. This would reduce the amount of get state calls and simplify the code.

What do you think? Is this a safe apprach?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Modules/_elementtree.c
#define ET_STATE_GLOBAL \
((elementtreestate *) PyModule_GetState(PyState_FindModule(&elementtreemodule)))
static inline elementtreestate *
get_elementtree_module_state_by_object(void *object)

@tiran tiran Nov 28, 2020

Copy link
Copy Markdown
Member

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.

The qualifier object is not self-explanatory. Every Python object is an object. This includes module objects and type objects. How about:

Suggested change
get_elementtree_module_state_by_object(void *object)
get_elementtree_module_state_by_instance(PyObject *self)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@shihai1991 shihai1991 Nov 28, 2020

Copy link
Copy Markdown
Member

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.

How about expend get_element_module_state() to receive self object or type object?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@koubaa koubaa Nov 28, 2020

Copy link
Copy Markdown
Contributor Author

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.

@tiran I tried using PyObject * and that gives a lot of compiler warnings (see somem below):

C:\Dev\OSS\cpython\cpython\Modules_elementtree.c(3749,74): warning C4133: 'function': incompatible types - from 'XMLParserObject *' to 'PyObject *' [C:\Dev\OSS\cpython\cpython\PCbuild_elementtree.vcxproj]
C:\Dev\OSS\cpython\cpython\Modules\clinic_elementtree.c.h(26,52): warning C4133: 'function': incompatible types - from 'ElementObject *' to 'PyObject *' [C:\Dev\OSS\cpython\cpython\PCbuild_elementtree.vcxproj]

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@koubaa koubaa Nov 28, 2020

Copy link
Copy Markdown
Contributor Author

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.

@shihai1991 do you mean two methods, one for each?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@shihai1991 shihai1991 Nov 30, 2020

Copy link
Copy Markdown
Member

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.

yes. just my personal suggestion :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Modules/_elementtree.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_elementtree.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_elementtree.c
Comment on lines +384 to +386
class _elementtree.Element "ElementObject *" "get_elementtree_module_state_by_object(self)->element_type"
class _elementtree.TreeBuilder "TreeBuilderObject *" "get_elementtree_module_state_by_object(self)->tree_builder_type"
class _elementtree.XMLParser "XMLParserObject *" "get_elementtree_module_state_by_object(self)->xml_parser_type"

@tiran tiran Nov 28, 2020

Copy link
Copy Markdown
Member

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.

Is this correct? AFAIK the argument clinic code has only access to type, not to self.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@koubaa koubaa Nov 28, 2020

Copy link
Copy Markdown
Contributor Author

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.

@tiran This is where I got from trial and error, see the implementation of _elementtree_Element_append in Modules/clinic/_elementtree.c.h

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added awaiting changes and removed awaiting review labels Nov 28, 2020
@bedevere-bot

bedevere-bot commented Nov 28, 2020

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@koubaa

koubaa commented Nov 28, 2020

Copy link
Copy Markdown
Contributor Author

The patch looks like it was a lot of work. Thanks for investing your time to port _elementtree.

I'm a bit worried that the module state getter is going to affect performance negatively. The code has to use the slow _PyType_GetModuleByDef() API. In my patch for the _ssl module I added a module state pointer to instance structs. For example you could add state to typedef struct ElementObject and then reference the state with self->state. This would reduce the amount of get state calls and simplify the code.

What do you think? Is this a safe apprach?

@tiran I'm not sure if its safe but I understand it in principle. I don't know exactly the lifetime of instance structs when pickling. @vstinner thoughts on that approach?

Is there a benchmark I could try to measure the performance impact? _PyType_GetModuleByDef seems to only be slow if there is significant subclassing so I could try to whip up an example that tries to overdo that.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@koubaa
visit/clear
8f2355a
@erlend-aasland

erlend-aasland commented Dec 1, 2020

Copy link
Copy Markdown
Contributor

This PR introduces a ref. leak (looks similar to the ref. leak I had in PR #23428):

bash$ ./python.exe -m test -F -r -j1 -R 3:5 test_xml_etree_c
Using random seed 1742818
0:00:00 load avg: 1.54 Run tests in parallel using 1 child processes
0:00:14 load avg: 1.50 [  1/1] test_xml_etree_c failed
beginning 8 repetitions
12345678
........
test_xml_etree_c leaked [147, 147, 147, 147, 147] references, sum=735
test_xml_etree_c leaked [59, 59, 59, 59, 59] memory blocks, sum=295

== Tests result: FAILURE ==

1 test failed:
    test_xml_etree_c

Total duration: 14.8 sec
Tests result: FAILURE
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@koubaa

koubaa commented Dec 11, 2020

Copy link
Copy Markdown
Contributor Author

@erlend-aasland oof sorry, I didn't realize you were working on this module too. Any tips to find the cause of the ref leak or which reference is leaking? I'm not too familiar with the diagnostics & telemetry available.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland

erlend-aasland commented Dec 11, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

oof sorry, I didn't realize you were working on this module too.

No problem, you're fine! Feel free to take a look at my PR. I haven't been able to devote any free time to this the last month or so, anyway.

Any tips to find the cause of the ref leak or which reference is leaking? I'm not too familiar with the diagnostics & telemetry available.

Me neither. It kinda stopped when I encountered the ref. leak. I use python.exe -m test -F -r -j1 -R 3:5 test_<mod> to check for reference/memory leaks. I've tried playing with some of the -X options as well, but I haven't found a good way to debug ref leaks yet. I'll keep an eye on this PR to see what you find out :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

github-actions Bot commented Jan 11, 2021

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Jan 11, 2021
@iritkatriel

iritkatriel commented Apr 11, 2022

Copy link
Copy Markdown
Member

https://bugs.python.org/issue1635741 is closed. What is that status of this PR?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Apr 19, 2022

Copy link
Copy Markdown
Member

The change is still relevant, but should use a new issue number.

Moreover, the SC asked to put the conversion of static types to heap types on hold. @encukou and @erlend-aasland wrote https://peps.python.org/pep-0687/ which may unblock the situation but it's still a draft.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland

erlend-aasland commented Jun 27, 2022

Copy link
Copy Markdown
Contributor

FYI, PEP-687 was just accepted.

koubaa reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ezio-melotti ezio-melotti removed the CLA signed label Jul 13, 2022
@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Aug 1, 2022
@kylotan kylotan mannequin mentioned this pull request Sep 19, 2022
Py_Finalize() doesn't clear all Python objects at exit #44470
Closed
@erlend-aasland

erlend-aasland commented Mar 14, 2023

Copy link
Copy Markdown
Contributor

Superseded by gh-101285

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland erlend-aasland closed this Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@shihai1991 shihai1991 shihai1991 left review comments
+1 more reviewer
@tiran tiran tiran requested changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting changes

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

9 participants

@koubaa @bedevere-bot @erlend-aasland @iritkatriel @vstinner @tiran @shihai1991 @the-knights-who-say-ni @ezio-melotti
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.