gh-138535: Optimize fill_time for typical timestamps (#138537)

While file timestamps can be anything the file system can store, most
lie between the recent past and the near future.  Optimize fill_time()
for typical timestamps in three ways:

- When possible, convert to nanoseconds with C arithmetic.
- When using C arithmetic and the seconds member is not required (for
  st_birthtime), avoid creating a long object.
- When using C arithmetic, reorder the code to avoid the null checks
  implied in Py_XDECREF().

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Jeffrey Bosboom 2025-09-09 02:05:54 -07:00 committed by GitHub
parent 5edfe55acf
commit d4825ac27c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 62 additions and 41 deletions

View file

@ -1064,9 +1064,15 @@ def test_large_time(self):
if self.get_file_system(self.dirname) != "NTFS":
self.skipTest("requires NTFS")
large = 5000000000 # some day in 2128
os.utime(self.fname, (large, large))
self.assertEqual(os.stat(self.fname).st_mtime, large)
times = (
5000000000, # some day in 2128
# boundaries of the fast path cutoff in posixmodule.c:fill_time
-9223372037, -9223372036, 9223372035, 9223372036,
)
for large in times:
with self.subTest(large=large):
os.utime(self.fname, (large, large))
self.assertEqual(os.stat(self.fname).st_mtime, large)
def test_utime_invalid_arguments(self):
# seconds and nanoseconds parameters are mutually exclusive