Skip to content

Optimize posixmodule.c:fill_time for typical timestamps #138535

@jbosboom

Description

@jbosboom

Feature or enhancement

Proposal:

os.stat eventually calls fill_time to initialize the timestamps in the os.stat_result. For typical timestamps, we can compute the nanoseconds using C arithmetic instead of creating and multiplying Python longs. Then we can skip creating the seconds long when s_index == -1 (for st_birthtime). Because we can immediately store the longs we do create in the stat_result, we can avoid the null checks implied in Py_XDECREF.

This improves python -m pyperf timeit -s 'import os' 'os.stat(".")' from 1.26 us +- 0.02 us to 1.15 us +- 0.01 us on Linux 6.16.2.arch1-1 and a btrfs filesystem.

I found this while implementing os.stat_result.st_birthtime on Linux and trying not to regress performance.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirperformancePerformance or resource usagetype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions