Skip to content

[WIP] PEP 510: Specialize functions with guards #70286

Closed

Description

@vstinner
BPO 26098
Nosy @vstinner, @DimitrisJim
PRs
  • [WIP] bpo-26098: Implement PEP 510, function specialization #2354
  • Files
  • specialize.patch
  • specialize-2.patch
  • specialize-3.patch
  • specialize-4.patch
  • specialize-5.patch
  • specialize-6.patch
  • specialize-7.patch
  • specialize-8.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2017-10-17.20:12:30.911>
    created_at = <Date 2016-01-13.12:52:48.051>
    labels = []
    title = '[WIP] PEP 510: Specialize functions with guards'
    updated_at = <Date 2017-10-17.20:12:30.909>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2017-10-17.20:12:30.909>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-10-17.20:12:30.911>
    closer = 'vstinner'
    components = []
    creation = <Date 2016-01-13.12:52:48.051>
    creator = 'vstinner'
    dependencies = []
    files = ['41601', '41602', '41661', '41702', '41724', '41786', '41817', '41818']
    hgrepos = []
    issue_num = 26098
    keywords = ['patch']
    message_count = 13.0
    messages = ['258141', '258143', '258147', '258591', '258868', '259012', '259068', '259447', '259450', '259653', '259654', '296703', '304525']
    nosy_count = 2.0
    nosy_names = ['vstinner', 'Jim Fasarakis-Hilliard']
    pr_nums = ['2354']
    priority = 'normal'
    resolution = 'rejected'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue26098'
    versions = ['Python 3.6']

    Activity

    1. vstinner commented on Jan 13, 2016

      @vstinner
      MemberAuthor

      Attached patch implements the PEP-510 "Specialize functions with guards".

      Changes on the C API are described in the PEP:
      https://www.python.org/dev/peps/pep-0510/#changes

      Additions of the patch:

      • Add func_specialize() and func_get_specialized() to _testcapi
      • Add _testcapi.PyGuard: Python wrapper to the Guard C API
      • Add Lib/test/test_pep510.py
    2. vstinner commented on Jan 13, 2016

      @vstinner
      MemberAuthor

      Patch version 2 fixes some bugs and add more tests.

      More notes about the patch:

      • RuntimeError is raised if guard check() result is greater than 2
      • RuntimeError is raised if guard init() result is greater than 1
      • (hum, maybe 'res < 0' check must be replaced with 'res == -1', but I'm not sure that that it's worth it.)
      • If PyFunction_Specialize() is called with a code object or a Python code, it creates a new code object and copies the code name and first line number in the new code object to ease debugging

      TODO: keywords are currently not supported in PyGuard.__call__().

    3. vstinner commented on Jan 13, 2016

      @vstinner
      MemberAuthor

      An unit test is needed on pickle serialization to ensure that the specialize code and guards are ignored.

    4. vstinner commented on Jan 19, 2016

      @vstinner
      MemberAuthor

      Patch version 3:

      • guards are now tracked by the garbage collector. That's a very important requirements to not change the Python semantics at exit, when a guard keeps a strong reference to the global namespace:

      https://mail.python.org/pipermail/python-dev/2016-January/142792.html

      • add more tests: call specialize() with invalid types, set __code__
    5. vstinner commented on Jan 23, 2016

      @vstinner
      MemberAuthor

      Patch version 4:

      • Keywords are now supported everywhere and tested by unit tests
      • Inline specode_check() into PyFunction_GetSpecializedCode()
    6. vstinner commented on Jan 27, 2016

      @vstinner
      MemberAuthor

      Patch version 5: implement PyFunction_RemoveSpecialized() and PyFunction_RemoveAllSpecialized() functions (with unit tests!).

      I'm not sure that PyFunction_RemoveSpecialized() must return 0 (success) if the removed specialized code doesn't exist (invalid index).

    7. vstinner commented on Jan 27, 2016

      @vstinner
      MemberAuthor

      FIXME: sys.getsizecode(func) doesn't include specialized code and guards.

    8. vstinner commented on Feb 3, 2016

      @vstinner
      MemberAuthor

      Patch version 6: I inlined PyFunction_GetSpecializedCode() into fast_function() of Python/ceval.c. It reduces *a little bit* the overhead of the patch when specialization is not used, but it also avoids to expose this function. I don't think that it's worth to expose PyFunction_GetSpecializedCode(): it was only used in ceval.c. For example, I don't use it for unit tests. I prefer to write tests calling the function and checking the results (see test_pep510.py).

      *Raw* overhead of specialized-6.patch on calling "def f(): pass": 1.7 nanoseconds. I computed the overhead using timeit:

      ./python -m timeit -s 'def f(): pass' 'f()'

      • Original: 71.7 ns
      • specialize-6.patch: 73.4 ns (+1.7 ns, +2.4%)
      • specialize-5.patch: 74.3 ns (+2.6 ns, +3.6%)

      I will run perf.py to see the overhead on a macro benchmark.

    9. vstinner commented on Feb 3, 2016

      @vstinner
      MemberAuthor

      Results of the "The Grand Unified Python Benchmark Suite" on specialize-6.patch.

      I'm skeptical, I don't understand how my patch can make a benchmark faster :-) The result of regex_v8 is bad :-/

      $ python3 -u perf.py --rigorous ../default/python.orig ../default/python
      (...)
      Report on Linux smithers 4.3.3-300.fc23.x86_64 #1 SMP Tue Jan 5 23:31:01 UTC 2016 x86_64 x86_64
      Total CPU cores: 8

      ### chameleon_v2 ###
      Min: 5.558607 -> 5.831682: 1.05x slower
      Avg: 5.613403 -> 5.902949: 1.05x slower
      Significant (t=-27.95)
      Stddev: 0.06994 -> 0.07640: 1.0924x larger

      ### django_v3 ###
      Min: 0.582356 -> 0.573327: 1.02x faster
      Avg: 0.604402 -> 0.582197: 1.04x faster
      Significant (t=3.43)
      Stddev: 0.05618 -> 0.03215: 1.7474x smaller

      ### regex_v8 ###
      Min: 0.043784 -> 0.049854: 1.14x slower
      Avg: 0.044270 -> 0.050521: 1.14x slower
      Significant (t=-19.87)
      Stddev: 0.00200 -> 0.00243: 1.2105x larger

      The following not significant results are hidden, use -v to show them:
      2to3, fastpickle, fastunpickle, json_dump_v2, json_load, nbody, tornado_http.

    10. vstinner commented on Feb 5, 2016

      @vstinner
      MemberAuthor

      Patch version 7:

      • Fix a random crash related to _testcapi.PyGuard: implement tp_traverse on PyFuncGuard and "inherit" tp_traverse on PyGuard
      • Fix a typo Include/funcobject.h
      • (rebase the patch)
    11. vstinner commented on Feb 5, 2016

      @vstinner
      MemberAuthor

      Oh, I missed comments on the code review. Fixed on patch version 8.

    12. vstinner commented on Jun 23, 2017

      @vstinner
      MemberAuthor

      Recently, some people asked me for an update for my FAT Python project. So I rebased this change I wrote 1 year 1/2 and adapted it for the new code base:

      • I renamed test_pep510.py to test_func_specialize.py
      • I removed the useless PyFunction_Check() macro
      • I changed the guard check prototype to use the new FASTCALL calling convention: (PyObject **args, Py_ssize_t nargs, PyObject *kwnames: tuple)
      • I patched _PyFunction_FastCallDict() *and* PyFunction_FastCallKeywords() to check guards and call specified code if guards succeeded

      The PEP-510 is not accepted, so the implementation is still a work-in-progress (WIP) and must not be merged.

    13. changed the title [-]PEP 510: Specialize functions with guards[/-] [+][WIP] PEP 510: Specialize functions with guards[/+] on Jun 28, 2017
    14. vstinner commented on Oct 17, 2017

      @vstinner
      MemberAuthor

      I rejected my own PEP-510, so I reject this issue as well.
      https://mail.python.org/pipermail/python-dev/2017-October/149901.html

    15. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      No labels
      No labels

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions