Skip to content

BLD: Try using shared memory utilities in Cython to reduce wheel sizes #61384

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
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ endif
cy = meson.get_compiler('cython')
if cy.version().version_compare('>=3.1.0')
add_project_arguments('-Xfreethreading_compatible=true', language: 'cython')

# Use shared utility code to reduce wheel sizes
Copy link
Member

Choose a reason for hiding this comment

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

Cool seems useful. Is there any discussion to add this natively to Meson instead of custom-coding this ourselves?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see how that's possible, since it generates an extra extension module and projects may have their code set up (e.g., with a Meson generator) to apply Cython/C compile args to all their extension modules. They may also have opinions about where the extension module should live in the import tree.

# copied from https://github.com/scikit-learn/scikit-learn/pull/31151/files
cy = find_program(cy.cmd_array()[0])
cython_shared_src = custom_target(
install: false,
output: '_cyutility.c',
command: [
cy,
'-3',
'--fast-fail',
'--generate-shared=' + meson.current_build_dir() / '_cyutility.c',
],
)

py.extension_module(
'_cyutility',
cython_shared_src,
subdir: 'pandas/_libs',
install: true,
)
endif

# Needed by pandas.test() when it looks for the pytest ini options
Expand Down
6 changes: 6 additions & 0 deletions pandas/_libs/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ if get_option('buildtype') == 'debug'
cython_args += ['--gdb']
endif

# Use shared utility code to reduce wheel sizes
# copied from https://github.com/scikit-learn/scikit-learn/pull/31151/files
if cy.version().version_compare('>=3.1.0')
cython_args += ['--shared=pandas._libs._cyutility']
endif

foreach ext_name, ext_dict : libs_sources
py.extension_module(
ext_name,
Expand Down
6 changes: 6 additions & 0 deletions pandas/_libs/tslibs/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ if get_option('buildtype') == 'debug'
cython_args += ['--gdb']
endif

# Use shared utility code to reduce wheel sizes
# copied from https://github.com/scikit-learn/scikit-learn/pull/31151/files
if cy.version().version_compare('>=3.1.0')
cython_args += ['--shared=pandas._libs._cyutility']
endif

foreach ext_name, ext_dict : tslibs_sources
py.extension_module(
ext_name,
Expand Down
11 changes: 9 additions & 2 deletions pandas/_libs/window/meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
cy_args = ['-X always_allow_keywords=true']
# Use shared utility code to reduce wheel sizes
# copied from https://github.com/scikit-learn/scikit-learn/pull/31151/files
if cy.version().version_compare('>=3.1.0')
cython_args += ['--shared=pandas._libs._cyutility']
endif

py.extension_module(
'aggregations',
['aggregations.pyx'],
cython_args: ['-X always_allow_keywords=true'],
cython_args: cy_args,
include_directories: [inc_np, inc_pd],
subdir: 'pandas/_libs/window',
override_options: ['cython_language=cpp'],
Expand All @@ -11,7 +18,7 @@ py.extension_module(
py.extension_module(
'indexers',
['indexers.pyx'],
cython_args: ['-X always_allow_keywords=true'],
cython_args: cy_args,
include_directories: [inc_np, inc_pd],
subdir: 'pandas/_libs/window',
install: true,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ requires = [
"meson-python>=0.13.1",
"meson>=1.2.1,<2",
"wheel",
"Cython~=3.0.5", # Note: sync with setup.py, environment.yml and asv.conf.json
"Cython==3.1.0rc1", # Note: sync with setup.py, environment.yml and asv.conf.json
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't want to do this, no need to force it since 3.1.0 is brand new (users and distros may have reasons not to upgrade, and there may be bugs).

You can use this instead:

if cy.version().version_compare('>=3.1.0')
  cython_args += ['-Xfreethreading_compatible=True']

The way I did it in scipy/scipy#22796 is a bit more concise than in this PR, also with cython_args: passing in a single place.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually you do have the cy.version().version_compare('>=3.1.0') already, so just no need to pin Cython. You'll get 3.1.0 anyway by default now.

# Force numpy higher than 2.0rc1, so that built wheels are compatible
# with both numpy 1 and 2
"numpy>=2.0.0rc1",
Expand Down
Loading