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

date, time and datetime alternate constructors should take fast construction path #76584

Closed
pganssle opened this issue Dec 21, 2017 · 4 comments
Labels
3.7 (EOL) end of life 3.8 (EOL) end of life type-feature A feature request or enhancement

Comments

@pganssle
Copy link
Member

BPO 32403
Nosy @abalkin, @pganssle
PRs
  • bpo-32403: Faster date and datetime constructors #4993
  • bpo-10381, bpo-32403: What's new entries for changes to datetime #5814
  • [3.7] bpo-10381, bpo-32403: What's new entries for changes to datetime (gh-5814) #5929
  • 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 2018-01-16.18:07:46.382>
    created_at = <Date 2017-12-21.21:51:14.443>
    labels = ['3.8', 'type-feature', '3.7']
    title = 'date, time and datetime alternate constructors should take fast construction path'
    updated_at = <Date 2018-02-27.19:58:30.646>
    user = 'https://github.com/pganssle'

    bugs.python.org fields:

    activity = <Date 2018-02-27.19:58:30.646>
    actor = 'belopolsky'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-01-16.18:07:46.382>
    closer = 'belopolsky'
    components = []
    creation = <Date 2017-12-21.21:51:14.443>
    creator = 'p-ganssle'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32403
    keywords = ['patch']
    message_count = 4.0
    messages = ['308907', '310099', '313019', '313021']
    nosy_count = 2.0
    nosy_names = ['belopolsky', 'p-ganssle']
    pr_nums = ['4993', '5814', '5929']
    priority = 'normal'
    resolution = None
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue32403'
    versions = ['Python 3.7', 'Python 3.8']

    @pganssle
    Copy link
    Member Author

    In the addition of the fromisoformat() alternate constructor (bpo-15873: #4699), I noted that I was able to get some significant speedup by special-casing the datetime baseclass in the C code so that it bypasses the Python constructor, by replacing code that looks like this:

    return PyObject_CallFunction(cls, "iii", year, month, day);
    

    With code that looks like this:

        PyObject *result;
        if ( (PyTypeObject *)cls == & PyDateTime_DateType ) {
            result = new_date_ex(year, month, day, (PyTypeObject *)cls);
        } else {
            result = PyObject_CallFunction(cls, "iii", year, month, day);
        }
    return result;
    

    (This is for date, but the results are even more striking for datetime).

    In my initial proof of concept implementation of a new_date_subclass_ex method, I've seen (this is not compiled with optimizations on, mind you) speedups for the other constructors as well:

    Old constructor:
    \================
    Class: date
    constructor:        940.5ns
    date.fromordinal:   1544.8ns
    date.fromtimestamp: 1941.9ns
    
    Class: DateSubclass
    constructor:        1016.6ns
    date.fromordinal:   1760.3ns
    date.fromtimestamp: 2295.3ns
    
    
    With fastpath:
    \==============
    Class: date
    constructor:        964.3ns
    date.fromordinal:   997.6ns
    date.fromtimestamp: 1130.2ns
    
    Class: DateSubclass
    constructor:        1086.9ns
    date.fromordinal:   1818.5ns
    date.fromtimestamp: 2129.9ns
    

    As you can see, this is a fairly significant speedup in the common case with no cost in the unusual case and no change in behavior. I propose that we switch over all the C constructors where it makes sense to do so in date, time and datetime.

    I'll have a PR forthcoming soon.

    @pganssle pganssle added 3.7 (EOL) end of life 3.8 (EOL) end of life type-feature A feature request or enhancement labels Dec 21, 2017
    @abalkin
    Copy link
    Member

    abalkin commented Jan 16, 2018

    New changeset 9f1b7b9 by Alexander Belopolsky (Paul Ganssle) in branch 'master':
    bpo-32403: Faster date and datetime constructors (bpo-4993)
    9f1b7b9

    @abalkin abalkin closed this as completed Jan 16, 2018
    @abalkin
    Copy link
    Member

    abalkin commented Feb 27, 2018

    New changeset 5bd04f9 by Alexander Belopolsky (Paul Ganssle) in branch 'master':
    bpo-10381, bpo-32403: What's new entries for changes to datetime (gh-5814)
    5bd04f9

    @abalkin
    Copy link
    Member

    abalkin commented Feb 27, 2018

    New changeset fff596f by Alexander Belopolsky (Miss Islington (bot)) in branch '3.7':
    bpo-10381, bpo-32403: What's new entries for changes to datetime (gh-5814) (gh-5929)
    fff596f

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 (EOL) end of life type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants