-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Reduce memory arena contention #8714
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
Open
kddnewton
wants to merge
8
commits into
python-pillow:main
Choose a base branch
from
kddnewton:reduce-contention
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+293
−71
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9b130e6
Reduce memory arena contention
kddnewton cff2141
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e2a96a5
Only import distutils when type checking
radarhere 19154a5
Merge branch 'main' into reduce-contention
radarhere 7d2ea76
Fix iterations through arenas
lysnikolaou 10104eb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 308e1fc
Avoid race on reading alignment in for-loop
lysnikolaou f91fcf3
Merge branch 'main' into reduce-contention
radarhere File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,12 +14,17 @@ | |||||||||||||||||||||||||||||||||||||
| import struct | ||||||||||||||||||||||||||||||||||||||
| import subprocess | ||||||||||||||||||||||||||||||||||||||
| import sys | ||||||||||||||||||||||||||||||||||||||
| import tempfile | ||||||||||||||||||||||||||||||||||||||
| import warnings | ||||||||||||||||||||||||||||||||||||||
| from collections.abc import Iterator | ||||||||||||||||||||||||||||||||||||||
| from typing import Any | ||||||||||||||||||||||||||||||||||||||
| from typing import TYPE_CHECKING, Any | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| from setuptools import Extension, setup | ||||||||||||||||||||||||||||||||||||||
| from setuptools.command.build_ext import build_ext | ||||||||||||||||||||||||||||||||||||||
| from setuptools.errors import CompileError | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if TYPE_CHECKING: | ||||||||||||||||||||||||||||||||||||||
| import distutils.ccompiler | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+20
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's avoid some type-checking only imports:
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def get_version() -> str: | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -292,6 +297,47 @@ def _pkg_config(name: str) -> tuple[list[str], list[str]] | None: | |||||||||||||||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def _try_compile(compiler: distutils.ccompiler.CCompiler, code: str) -> bool: | ||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||
| with tempfile.TemporaryDirectory() as d: | ||||||||||||||||||||||||||||||||||||||
| fn = os.path.join(d, "test.c") | ||||||||||||||||||||||||||||||||||||||
| with open(fn, "w") as f: | ||||||||||||||||||||||||||||||||||||||
| f.write(code) | ||||||||||||||||||||||||||||||||||||||
| compiler.compile([fn], output_dir=d, extra_preargs=["-Werror"]) | ||||||||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||||||||
| except CompileError: | ||||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def _try_compile_attr(compiler: distutils.ccompiler.CCompiler, attr: str) -> bool: | ||||||||||||||||||||||||||||||||||||||
| code = f""" | ||||||||||||||||||||||||||||||||||||||
| #pragma GCC diagnostic error "-Wattributes" | ||||||||||||||||||||||||||||||||||||||
| #pragma clang diagnostic error "-Wattributes" | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| int {attr} foo; | ||||||||||||||||||||||||||||||||||||||
| int main() {{ | ||||||||||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| return _try_compile(compiler, code) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def _try_compile_tls_define_macros( | ||||||||||||||||||||||||||||||||||||||
| compiler: distutils.ccompiler.CCompiler, | ||||||||||||||||||||||||||||||||||||||
| ) -> list[tuple[str, str | None]]: | ||||||||||||||||||||||||||||||||||||||
| if _try_compile_attr(compiler, "thread_local"): # C23 | ||||||||||||||||||||||||||||||||||||||
| return [("HAVE_THREAD_LOCAL", None)] | ||||||||||||||||||||||||||||||||||||||
| elif _try_compile_attr(compiler, "_Thread_local"): # C11/C17 | ||||||||||||||||||||||||||||||||||||||
| return [("HAVE__THREAD_LOCAL", None)] | ||||||||||||||||||||||||||||||||||||||
| elif _try_compile_attr(compiler, "__thread"): # GCC/clang | ||||||||||||||||||||||||||||||||||||||
| return [("HAVE___THREAD", None)] | ||||||||||||||||||||||||||||||||||||||
| elif _try_compile_attr(compiler, "__declspec(thread)"): # MSVC | ||||||||||||||||||||||||||||||||||||||
| return [("HAVE___DECLSPEC_THREAD_", None)] | ||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||
| return [] | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| class pil_build_ext(build_ext): | ||||||||||||||||||||||||||||||||||||||
| class ext_feature: | ||||||||||||||||||||||||||||||||||||||
| features = [ | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -426,13 +472,14 @@ def finalize_options(self) -> None: | |||||||||||||||||||||||||||||||||||||
| def _update_extension( | ||||||||||||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||||||||||||
| name: str, | ||||||||||||||||||||||||||||||||||||||
| libraries: list[str] | list[str | bool | None], | ||||||||||||||||||||||||||||||||||||||
| libraries: list[str] | list[str | bool | None] | None = None, | ||||||||||||||||||||||||||||||||||||||
| define_macros: list[tuple[str, str | None]] | None = None, | ||||||||||||||||||||||||||||||||||||||
| sources: list[str] | None = None, | ||||||||||||||||||||||||||||||||||||||
| ) -> None: | ||||||||||||||||||||||||||||||||||||||
| for extension in self.extensions: | ||||||||||||||||||||||||||||||||||||||
| if extension.name == name: | ||||||||||||||||||||||||||||||||||||||
| extension.libraries += libraries | ||||||||||||||||||||||||||||||||||||||
| if libraries is not None: | ||||||||||||||||||||||||||||||||||||||
| extension.libraries += libraries | ||||||||||||||||||||||||||||||||||||||
| if define_macros is not None: | ||||||||||||||||||||||||||||||||||||||
| extension.define_macros += define_macros | ||||||||||||||||||||||||||||||||||||||
| if sources is not None: | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -890,7 +937,10 @@ def build_extensions(self) -> None: | |||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| defs.append(("PILLOW_VERSION", f'"{PILLOW_VERSION}"')) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| self._update_extension("PIL._imaging", libs, defs) | ||||||||||||||||||||||||||||||||||||||
| tls_define_macros = _try_compile_tls_define_macros(self.compiler) | ||||||||||||||||||||||||||||||||||||||
| self._update_extension("PIL._imaging", libs, defs + tls_define_macros) | ||||||||||||||||||||||||||||||||||||||
| self._update_extension("PIL._imagingmath", define_macros=tls_define_macros) | ||||||||||||||||||||||||||||||||||||||
| self._update_extension("PIL._imagingmorph", define_macros=tls_define_macros) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||||||
| # additional libraries | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -913,7 +963,9 @@ def build_extensions(self) -> None: | |||||||||||||||||||||||||||||||||||||
| libs.append(feature.get("fribidi")) | ||||||||||||||||||||||||||||||||||||||
| else: # building FriBiDi shim from src/thirdparty | ||||||||||||||||||||||||||||||||||||||
| srcs.append("src/thirdparty/fribidi-shim/fribidi.c") | ||||||||||||||||||||||||||||||||||||||
| self._update_extension("PIL._imagingft", libs, defs, srcs) | ||||||||||||||||||||||||||||||||||||||
| self._update_extension( | ||||||||||||||||||||||||||||||||||||||
| "PIL._imagingft", libs, defs + tls_define_macros, srcs | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||
| self._remove_extension("PIL._imagingft") | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -922,19 +974,19 @@ def build_extensions(self) -> None: | |||||||||||||||||||||||||||||||||||||
| libs = [feature.get("lcms")] | ||||||||||||||||||||||||||||||||||||||
| if sys.platform == "win32": | ||||||||||||||||||||||||||||||||||||||
| libs.extend(["user32", "gdi32"]) | ||||||||||||||||||||||||||||||||||||||
| self._update_extension("PIL._imagingcms", libs) | ||||||||||||||||||||||||||||||||||||||
| self._update_extension("PIL._imagingcms", libs, tls_define_macros) | ||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||
| self._remove_extension("PIL._imagingcms") | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| webp = feature.get("webp") | ||||||||||||||||||||||||||||||||||||||
| if isinstance(webp, str): | ||||||||||||||||||||||||||||||||||||||
| libs = [webp, webp + "mux", webp + "demux"] | ||||||||||||||||||||||||||||||||||||||
| self._update_extension("PIL._webp", libs) | ||||||||||||||||||||||||||||||||||||||
| self._update_extension("PIL._webp", libs, tls_define_macros) | ||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||
| self._remove_extension("PIL._webp") | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| tk_libs = ["psapi"] if sys.platform in ("win32", "cygwin") else [] | ||||||||||||||||||||||||||||||||||||||
| self._update_extension("PIL._imagingtk", tk_libs) | ||||||||||||||||||||||||||||||||||||||
| self._update_extension("PIL._imagingtk", tk_libs, tls_define_macros) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| build_ext.build_extensions(self) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
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.