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-44945: Specialize BINARY_ADD #27967

Merged
merged 15 commits into from
Aug 27, 2021

Conversation

markshannon
Copy link
Member

@markshannon markshannon commented Aug 26, 2021

Adds the following specializations:

  • BINARY_ADD_ADAPTIVE
  • BINARY_ADD_FLOAT
  • BINARY_ADD_INT
  • BINARY_ADD_UNICODE
  • BINARY_ADD_UNICODE_INPLACE_FAST

BINARY_ADD_UNICODE_INPLACE_FAST is needed to keep for ...: s = s + ... O(ln(n)), rather than O(n**2).

About 1% faster

https://bugs.python.org/issue44945

@markshannon markshannon added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Aug 26, 2021
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @markshannon for commit a22af9a 🤖

If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Aug 26, 2021
Copy link
Member

@Fidget-Spinner Fidget-Spinner left a comment

Choose a reason for hiding this comment

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

LGTM except for the specialization for inplace strings (it causes refleaks, see below).

}
if (left_type == &PyUnicode_Type) {
int next_opcode = _Py_OPCODE(instr[1]);
if (next_opcode == STORE_FAST) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm surprised this check alone is enough, seems pretty easy to trick it with something like s = y + ''. Then again the runtime check DEOPT_IF(var != left, BINARY_ADD); will catch that.

@@ -121,7 +121,8 @@ _Py_GetSpecializationStats(void) {
int err = 0;
err += add_stat_dict(stats, LOAD_ATTR, "load_attr");
err += add_stat_dict(stats, LOAD_GLOBAL, "load_global");
err += add_stat_dict(stats, LOAD_GLOBAL, "load_method");
err += add_stat_dict(stats, LOAD_METHOD, "load_method");
Copy link
Member

Choose a reason for hiding this comment

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

Woops, thanks for fixing this.

@markshannon markshannon added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Aug 26, 2021
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @markshannon for commit 3146f48 🤖

If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Aug 26, 2021
Copy link
Member

@Fidget-Spinner Fidget-Spinner left a comment

Choose a reason for hiding this comment

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

LGTM.

STAT_INC(BINARY_ADD, hit);
record_hit_inline(next_instr, oparg);
GETLOCAL(next_oparg) = NULL;
Py_DECREF(left);
Copy link
Member

@Fidget-Spinner Fidget-Spinner Aug 26, 2021

Choose a reason for hiding this comment

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

Feel free to ignore, but you can reorder this to

            PyUnicode_Append(&SECOND(), right);
            STACK_SHRINK(1);
            Py_DECREF(right);
            Py_DECREF(left);
            if (TOP() == NULL) {
                goto error;
            }

Then if you'd like, you can factor out the code from STACK_SHRINK(1); onwards into a BINARY_ADD_FOOTER macro, and reuse that for all BINARY_ADD_* instructions.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm inclined to avoid using macros.
IMO, macros only help readability if they are function like, and self descriptive. E.g. Py_DECREF.

@markshannon
Copy link
Member Author

Failure on buildbot/AMD64 Arch Linux Asan Debug PR is a persistent failure unrelated to this PR.

@markshannon markshannon merged commit d3eaf0c into python:main Aug 27, 2021
@markshannon markshannon deleted the specialize-binary-add branch September 15, 2021 11:51
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.

4 participants