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

bpo-40521: [subinterpreters] Make dtoa bigint free list per-interpreter #24821

Merged
merged 1 commit into from
Mar 13, 2021
Merged

bpo-40521: [subinterpreters] Make dtoa bigint free list per-interpreter #24821

merged 1 commit into from
Mar 13, 2021

Conversation

JunyiXie
Copy link
Contributor

@JunyiXie JunyiXie commented Mar 11, 2021

https://bugs.python.org/issue40521
Make dtoa bigint free list per-interpreter

https://bugs.python.org/issue40521

@JunyiXie JunyiXie changed the title issue40521: [subinterpreters] Make dtoa bigint free list per-interpreter bpo-40521: [subinterpreters] Make dtoa bigint free list per-interpreter Mar 11, 2021

typedef uint32_t ULong;
typedef int32_t Long;
typedef uint64_t ULLong;
Copy link
Member

Choose a reason for hiding this comment

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

Many C files include directly or indirectly pycore_interp.h which includes pycore_dtoa.h. I would prefer to not pollule their namespaces with names like Kmax, ULong, Long, UULong which can clash with other names.

I see different options:

  • Don't fix dtoa.c for now
  • Add a prefix to all these names in the header file (ex: "PyDtoa") and create aliases in dtoa.c (ex: #define Kmax _PyDtoa_Kmax).
  • Add an API like PyInterpreterState.dict to have per-interpreter variables: the freelist would be dynamically allocated on the heap memory, and stored as a raw pointer in PyInterpreterState. Or if you don't want to add a whole API, just expose the cache as a "void *" in the structure, and add init/fini functions to allocate/free this structure (free list).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for your review. I chose the second option to fix this problem.

@bedevere-bot
Copy link

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.

@JunyiXie
Copy link
Contributor Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@vstinner: please review the changes made to this pull request.

@bedevere-bot bedevere-bot requested a review from vstinner March 12, 2021 03:59
@JunyiXie
Copy link
Contributor Author

I have removed the news. But I don't know how to make bedevere/news — No news entry in Misc/NEWS.d/next/ or "skip news" label found check success.

_PyDtoa_ULong x[1];
};

typedef struct Bigint Bigint;
Copy link
Member

Choose a reason for hiding this comment

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

This typedef is not needed in pycore_interp.h, please move it into dtoa.c.

Comment on lines 28 to 30
struct
Bigint {
struct Bigint *next;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
struct
Bigint {
struct Bigint *next;
struct _PyDtoa_Bigint {
struct _PyDtoa_Bigint *next;

@@ -321,6 +322,7 @@ struct _is {

struct ast_state ast;
struct type_cache type_cache;
Bigint *bigint_freelist[_PyDtoa_Kmax+1];
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Bigint *bigint_freelist[_PyDtoa_Kmax+1];
struct _PyDtoa_Bigint *dtoa_freelist[_PyDtoa_Kmax + 1];

Python/dtoa.c Outdated

if (k <= Kmax && (rv = freelist[k]))
freelist[k] = rv->next;
PyInterpreterState *is = PyInterpreterState_Get();
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer to add a helper function get_freelist().

I suggest to use _PyInterpreterState_GET() which is faster than PyInterpreterState_Get().

Python/dtoa.c Outdated
Comment on lines 124 to 127
#define Kmax _PyDtoa_Kmax
#define ULong _PyDtoa_ULong
#define Long _PyDtoa_Long
#define ULLong _PyDtoa_ULLong
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#define Kmax _PyDtoa_Kmax
#define ULong _PyDtoa_ULong
#define Long _PyDtoa_Long
#define ULLong _PyDtoa_ULLong
#define ULong _PyDtoa_ULong
#define Long _PyDtoa_Long
#define ULLong _PyDtoa_ULLong
#define Kmax _PyDtoa_Kmax
typedef struct _PyDtoa_Bigint Bigint;

@@ -1,4 +1,6 @@
#ifndef PY_NO_SHORT_FLOAT_REPR
#ifndef Py_CORE_DTOA_H
Copy link
Member

Choose a reason for hiding this comment

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

To follow other files, you should use "INTERNAL" instead: #ifndef Py_INTERNAL_DTOA_H.

@@ -321,6 +322,7 @@ struct _is {

struct ast_state ast;
struct type_cache type_cache;
Bigint *bigint_freelist[_PyDtoa_Kmax+1];
Copy link
Member

Choose a reason for hiding this comment

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

You need to add:

#ifndef PY_NO_SHORT_FLOAT_REPR
...
#endif 

If the PY_NO_SHORT_FLOAT_REPR macro is defined, dtoa.c is not used.

@JunyiXie
Copy link
Contributor Author

Thank vstinner, for your very detailed review. I did not consider these details before.

@JunyiXie
Copy link
Contributor Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@vstinner: please review the changes made to this pull request.

@bedevere-bot bedevere-bot requested a review from vstinner March 12, 2021 13:15
Python/dtoa.c Outdated

/* Get Bigint freelist from interpreter */
static Bigint **
get_freelist() {
Copy link
Member

Choose a reason for hiding this comment

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

See "function declaration isn’t a prototype [-Wstrict-prototypes]" compiler warning:

Suggested change
get_freelist() {
get_freelist(void) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your review, sorry I didn't pay attention to the warning.

@JunyiXie
Copy link
Contributor Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@vstinner: please review the changes made to this pull request.

@bedevere-bot bedevere-bot requested a review from vstinner March 12, 2021 16:47
@vstinner
Copy link
Member

Can you please build Python with PY_NO_SHORT_FLOAT_REPR macro defined? For example, add -DPY_NO_SHORT_FLOAT_REPR=1 flag in CFLAGS.

@JunyiXie
Copy link
Contributor Author

JunyiXie commented Mar 13, 2021

@vstinner Thank you for your reminder, I added -DPY_NO_SHORT_FLOAT_REPR=1 to run the unit test.

11 tests failed again:
     test_builtin test_configparser test_ctypes test_difflib test_float
     test_format test_fstring test_optparse test_statistics test_types
     test_unicode
======================================================================
FAIL: test_defaults_keyword (test.test_configparser.ConfigParserTestCase)
[bpo-23835](https://bugs.python.org/issue23835) fix for ConfigParser
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/xiejunyi/bdcpython/Lib/test/test_configparser.py", line 979, in test_defaults_keyword
    self.assertEqual(cf[self.default_section]['1'], '2.4')
AssertionError: '2.3999999999999999' != '2.4'
- 2.3999999999999999
+ 2.4
======================================================================
FAIL: test_defaults_keyword (test.test_configparser.ConfigParserTestCaseLegacyInterpolation)
[bpo-23835](https://bugs.python.org/issue23835) fix for ConfigParser
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/xiejunyi/bdcpython/Lib/test/test_configparser.py", line 979, in test_defaults_keyword
    self.assertEqual(cf[self.default_section]['1'], '2.4')
AssertionError: '2.3999999999999999' != '2.4'
- 2.3999999999999999
+ 2.4

After I revert my commit and run the unit test as well, there will still be these errors.
I guess defining PY_NO_SHORT_FLOAT_REPR=1 will cause these errors. not my commit cause those test faileds.

@vstinner vstinner merged commit 5bd1059 into python:master Mar 13, 2021
@vstinner
Copy link
Member

I merged your PR thanks.

@vstinner Thank you for your reminder, I added -DPY_NO_SHORT_FLOAT_REPR=1 to run the unit test.

I just wanted to check if it's possible to build Python in the PY_NO_SHORT_FLOAT_REPR macro defined: it's the case, thanks. I'm not interetesed by test_configparser failures. Platforms where PY_NO_SHORT_FLOAT_REPR macro is defined are rare. If an user of one of such platform is impacted, they can report an issue.

vstinner added a commit that referenced this pull request Mar 22, 2021
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.

4 participants