Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
gh-132983: Minor fixes and clean up for the _zstd module#134930
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Conversation
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.
One question and one concern, but the rest looks good! Thank you for the improvements :)
Modules/_zstd/compressor.c Outdated
ret = PyDict_SetItem(self->c_dicts, level, capsule); | ||
/* Add PyCapsule object to self->c_dicts if it is not already present. */ | ||
PyObject *result; | ||
ret = PyDict_SetDefaultRef(self->c_dicts, level, capsule, &result); |
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.
Out of curiousity, isPyDict_SetDefaultRef
the "correct" way to set items in a dictionary for new code?
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 GIL is released after checking that there is no entry for level in self->c_dicts. After it acquired again, it may happen that a new entry has already been added. In that case we should use already added capsule object.
emmatypingMay 31, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
self
is guarded by a lock, so no other object can modify it while it is locked. We don't use critical sections so I believe we don't unlock when the GIL is released. (Similarly this is true below)
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.
Well, then I'll remove this change.
Although I wonder if it's possible to get rid of the lock here and if it would be of any use.
emmatypingMay 31, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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, I think it might be possible to remove the lock. The dict_buffer/len are immutable, so the only concern would likely be actually setting the c_dicts member. Perhaps this can be investigated in a follow up?
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.
For now, I removed these changes. We can investigate later if there is a benefit of removing the lock for these calls.
Modules/_zstd/decompressor.c Outdated
if (self->d_dict!= NULL) { | ||
ZSTD_freeDDict(ret); | ||
} |
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.
This check doesn't look quite right. We're already in a block whereself->d_dict == NULL
.
serhiy-storchakaMay 30, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
This is the same case as above, but simpler. After the GIL was released and acquired, we cannot already assume that self->d_dict == NULL.
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.
Similar to the above, perhaps this could be addressed in a follow-up? (or if you'd like to tackle removing locking from ZstdDict here I suppose that's fine)
if (!PyType_Check(c_parameter_type) || !PyType_Check(d_parameter_type)) { | ||
PyErr_SetString(PyExc_ValueError, | ||
"The two arguments should be CompressionParameter and " | ||
"DecompressionParameter types."); | ||
return NULL; | ||
} |
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.
Sanity check on removing the type checks here?
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 type checks are already handled by argument clinic, so these are actually redundant.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
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 changes look good! One minor wording suggestion but great otherwise. Thank you!
/* Wrong type */ | ||
PyErr_SetString(PyExc_TypeError, | ||
"zstd_dict argument should be ZstdDict object."); |
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.
"zstd_dict argument should be ZstdDict object."); | |
"zstd_dict argument should beaZstdDict object."); |
ZstdDict* | ||
_Py_parse_zstd_dict(const_zstd_state*state,PyObject*dict,int*ptype) |
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.
This is a great refactor, thank you!
Uh oh!
There was an error while loading.Please reload this page.