Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Replace if xxx is None: with POP_JUMP_IF_NOT_NONE ? #115

Closed
penguin-wwy opened this issue Nov 18, 2021 · 5 comments
Closed

Replace if xxx is None: with POP_JUMP_IF_NOT_NONE ? #115

penguin-wwy opened this issue Nov 18, 2021 · 5 comments

Comments

@penguin-wwy
Copy link

if xxx is None:

These codes will be compiled to

LOAD_FAST                xxx
LOAD_CONST               NONE
IS_OP                    0
POP_JUMP_IF_FALSE        target

I add new instr POP_JUMP_IF_NOT_NONE and POP_JUMP_IF_NONE to replace these:

LOAD_FAST               xxx
POP_JUMP_IF_NOT_NONE    target

This is very effective for such a sample:

def is_none_test(data):
    if data is None:
        return 1
    return 0

Call this function 1, 000, 000 times

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
1000000    0.091    0.000    0.091    0.000 is_none_test.py:4(is_none_test)

After optimization, 10%+ speedup.

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
1000000    0.080    0.000    0.080    0.000 is_none_test.py:4(is_none_test)

Result on pyperformance:

+-------------------------+---------+-----------------------+
| Benchmark               | main    | opt                   |
+=========================+=========+=======================+
| richards                | 82.9 ms | 79.7 ms: 1.04x faster |
+-------------------------+---------+-----------------------+
| json_loads              | 41.1 us | 39.8 us: 1.03x faster |
+-------------------------+---------+-----------------------+
| meteor_contest          | 138 ms  | 134 ms: 1.03x faster  |
+-------------------------+---------+-----------------------+
| raytrace                | 523 ms  | 507 ms: 1.03x faster  |
+-------------------------+---------+-----------------------+
| crypto_pyaes            | 131 ms  | 127 ms: 1.03x faster  |
+-------------------------+---------+-----------------------+
| logging_silent          | 171 ns  | 167 ns: 1.03x faster  |
+-------------------------+---------+-----------------------+
| sympy_str               | 476 ms  | 466 ms: 1.02x faster  |
+-------------------------+---------+-----------------------+
| telco                   | 12.1 ms | 11.9 ms: 1.02x faster |
+-------------------------+---------+-----------------------+
| sympy_sum               | 250 ms  | 245 ms: 1.02x faster  |
+-------------------------+---------+-----------------------+
| logging_format          | 11.0 us | 10.8 us: 1.02x faster |
+-------------------------+---------+-----------------------+
| sympy_integrate         | 31.1 ms | 30.6 ms: 1.02x faster |
+-------------------------+---------+-----------------------+
| mako                    | 17.5 ms | 17.2 ms: 1.02x faster |
+-------------------------+---------+-----------------------+
| sympy_expand            | 795 ms  | 782 ms: 1.02x faster  |
+-------------------------+---------+-----------------------+
| logging_simple          | 9.95 us | 9.80 us: 1.02x faster |
+-------------------------+---------+-----------------------+
| sqlite_synth            | 5.49 us | 5.41 us: 1.01x faster |
+-------------------------+---------+-----------------------+
| pathlib                 | 27.9 ms | 27.5 ms: 1.01x faster |
+-------------------------+---------+-----------------------+
| 2to3                    | 377 ms  | 372 ms: 1.01x faster  |
+-------------------------+---------+-----------------------+
| deltablue               | 6.70 ms | 6.61 ms: 1.01x faster |
+-------------------------+---------+-----------------------+
| nbody                   | 153 ms  | 151 ms: 1.01x faster  |
+-------------------------+---------+-----------------------+
| dulwich_log             | 97.4 ms | 96.2 ms: 1.01x faster |
+-------------------------+---------+-----------------------+
| pyflate                 | 697 ms  | 690 ms: 1.01x faster  |
+-------------------------+---------+-----------------------+
| chaos                   | 125 ms  | 123 ms: 1.01x faster  |
+-------------------------+---------+-----------------------+
| tornado_http            | 142 ms  | 141 ms: 1.01x faster  |
+-------------------------+---------+-----------------------+
| pickle_pure_python      | 544 us  | 540 us: 1.01x faster  |
+-------------------------+---------+-----------------------+
| chameleon               | 12.0 ms | 11.9 ms: 1.01x faster |
+-------------------------+---------+-----------------------+
| django_template         | 63.6 ms | 63.1 ms: 1.01x faster |
+-------------------------+---------+-----------------------+
| go                      | 220 ms  | 219 ms: 1.01x faster  |
+-------------------------+---------+-----------------------+
| pidigits                | 219 ms  | 218 ms: 1.00x faster  |
+-------------------------+---------+-----------------------+
| nqueens                 | 141 ms  | 142 ms: 1.00x slower  |
+-------------------------+---------+-----------------------+
| python_startup_no_site  | 6.00 ms | 6.02 ms: 1.00x slower |
+-------------------------+---------+-----------------------+
| xml_etree_process       | 95.0 ms | 95.6 ms: 1.01x slower |
+-------------------------+---------+-----------------------+
| regex_compile           | 212 ms  | 213 ms: 1.01x slower  |
+-------------------------+---------+-----------------------+
| unpickle                | 22.6 us | 22.8 us: 1.01x slower |
+-------------------------+---------+-----------------------+
| scimark_sor             | 230 ms  | 233 ms: 1.01x slower  |
+-------------------------+---------+-----------------------+
| unpickle_list           | 6.55 us | 6.67 us: 1.02x slower |
+-------------------------+---------+-----------------------+
| pickle_list             | 4.71 us | 4.81 us: 1.02x slower |
+-------------------------+---------+-----------------------+
| xml_etree_parse         | 203 ms  | 209 ms: 1.03x slower  |
+-------------------------+---------+-----------------------+
| regex_v8                | 33.6 ms | 34.7 ms: 1.03x slower |
+-------------------------+---------+-----------------------+
| scimark_sparse_mat_mult | 7.73 ms | 8.01 ms: 1.04x slower |
+-------------------------+---------+-----------------------+
| regex_effbot            | 3.89 ms | 4.09 ms: 1.05x slower |
+-------------------------+---------+-----------------------+
| spectral_norm           | 166 ms  | 175 ms: 1.05x slower  |
+-------------------------+---------+-----------------------+
| scimark_lu              | 217 ms  | 229 ms: 1.06x slower  |
+-------------------------+---------+-----------------------+
| hexiom                  | 10.2 ms | 10.7 ms: 1.06x slower |
+-------------------------+---------+-----------------------+
| pickle_dict             | 31.2 us | 33.8 us: 1.09x slower |
+-------------------------+---------+-----------------------+
| Geometric mean          | (ref)   | 1.00x faster          |
+-------------------------+---------+-----------------------+

However, I am confused about, why pickle_dict is so slow ? This case is just calling the CFunction in the loop !

@penguin-wwy penguin-wwy changed the title Replace if xxx is None: with POP_JUMP_IF_NOT_NONE ? Replace if xxx is None: with POP_JUMP_IF_NOT_NONE ? Nov 18, 2021
@markshannon
Copy link
Member

pickle_dict, like many microbenchmarks, is very sensitive to code placement, so unrelated changes can impact it significantly.

@markshannon
Copy link
Member

See also #36

@Fidget-Spinner
Copy link
Collaborator

Thanks for writing the code and conducting the benchmarks! Unfortunately, 10% speedup on a microbenchmark usually means 0 real-world speedup, so depending on how complex the code is, I don't know if it's worth it :(.

@markshannon
Copy link
Member

@penguin-wwy Is the code on a publicly visible branch?

@gvanrossum
Copy link
Collaborator

Moving to a discussion. @penguin-wwy if you want to contribute your code please open a PR and reference this issue or the resulting GitHub Discussion.

@faster-cpython faster-cpython locked and limited conversation to collaborators Dec 2, 2021
@gramster gramster moved this to Todo in Fancy CPython Board Jan 10, 2022
@gramster gramster moved this from Todo to Other in Fancy CPython Board Jan 10, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants