Skip to content

pybind11_add_module(): OPT_SIZE argument for size-based optimization #2463

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

Merged
merged 2 commits into from
Sep 6, 2020

Conversation

wjakob
Copy link
Member

@wjakob wjakob commented Sep 4, 2020

This PR adds way of switching this specific target over to “optimize size” flags (-Os) no matter what the project itself uses (except in debug mode, where optimizations remain disabled). Performance within bindings is usually not that critical, and this can make a quite substantial difference in projects that are weighted down by heavy (10-100MB) python extension libraries. I’m seeing ~25% size reduction just from that.

The PR also contains two other very minor changes:

  1. cmake appends a dummy define -D${target_name}_EXPORTS when compiling shared libraries. These are often not needed, especially not for binding code. I've added directives that remove them.

  2. pybind11 prints two redundant lines when included by a parent project (via add_subdirectory), I've deleted one of them.

@wjakob wjakob requested a review from henryiii September 4, 2020 21:34
Copy link
Collaborator

@henryiii henryiii left a comment

Choose a reason for hiding this comment

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

Looks good to me!

target_link_libraries(${target_name} PRIVATE pybind11::opt_size)
endif()

# Avoid the -D${target_name}_EXPORTS definition that CMake adds by default
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are these harmful, though? I would assume they do nothing if never used, and someone might expect them to be there?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, fair enough -- I'll delete them again.

@henryiii
Copy link
Collaborator

henryiii commented Sep 5, 2020

This branch has conflicts that must be resolved

This is why changlog entries in PRs can be a bit of a pain. I've never come up with a reasonable solution, though.

Since both of my merged PRs have affected this, I've rebased for you.

(If you need to update, git fetch && git reset --hard will sync back up - make sure you don't have local changes (if so, leave of the --hard).

Copy link
Collaborator

@YannickJadoul YannickJadoul left a comment

Choose a reason for hiding this comment

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

For what it's worth, adding my checkmark :-)

@wjakob
Copy link
Member Author

wjakob commented Sep 6, 2020

Merged with some documentation improvements following @YannickJadoul's question. I got rid of the originally proposed change to remove the -D..._EXPORTS flag.

@wjakob wjakob merged commit 064a03a into master Sep 6, 2020
@wjakob wjakob deleted the opt_size_pr branch September 6, 2020 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants