gh-110850: Cleanup PyTime API: PyTime_t are nanoseconds (#115753)

PyTime_t no longer uses an arbitrary unit, it's always a number of
nanoseconds (64-bit signed integer).

* Rename _PyTime_FromNanosecondsObject() to _PyTime_FromLong().
* Rename _PyTime_AsNanosecondsObject() to _PyTime_AsLong().
* Remove pytime_from_nanoseconds().
* Remove pytime_as_nanoseconds().
* Remove _PyTime_FromNanoseconds().
This commit is contained in:
Victor Stinner 2024-02-21 11:46:00 +01:00 • committed by GitHub
parent 69ab93082d
commit e4c34f04a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 57 additions and 100 deletions

View file

@ -6,7 +6,7 @@
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_ceval.h" // _PyEval_SetProfile()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_time.h" // _PyTime_FromNanosecondsObject()
#include "pycore_time.h" // _PyTime_FromLong()
#include "rotatingtree.h"
@ -98,7 +98,7 @@ static PyTime_t CallExternalTimer(ProfilerObject *pObj)
if (pObj->externalTimerUnit > 0.0) {
/* interpret the result as an integer that will be scaled
in profiler_getstats() */
err = _PyTime_FromNanosecondsObject(&result, o);
err = _PyTime_FromLong(&result, o);
}
else {
/* interpret the result as a double measured in seconds.

View file

@ -17,7 +17,7 @@ test_pytime_fromseconds(PyObject *self, PyObject *args)
return NULL;
}
PyTime_t ts = _PyTime_FromSeconds(seconds);
return _PyTime_AsNanosecondsObject(ts);
return _PyTime_AsLong(ts);
}
static int
@ -49,7 +49,7 @@ test_pytime_fromsecondsobject(PyObject *self, PyObject *args)
if (_PyTime_FromSecondsObject(&ts, obj, round) == -1) {
return NULL;
}
return _PyTime_AsNanosecondsObject(ts);
return _PyTime_AsLong(ts);
}
static PyObject *
@ -64,7 +64,7 @@ test_PyTime_AsTimeval(PyObject *self, PyObject *args)
return NULL;
}
PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
struct timeval tv;
@ -91,7 +91,7 @@ test_PyTime_AsTimeval_clamp(PyObject *self, PyObject *args)
return NULL;
}
PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
struct timeval tv;
@ -113,7 +113,7 @@ test_PyTime_AsTimespec(PyObject *self, PyObject *args)
return NULL;
}
PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
struct timespec ts;
@ -131,7 +131,7 @@ test_PyTime_AsTimespec_clamp(PyObject *self, PyObject *args)
return NULL;
}
PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
struct timespec ts;
@ -149,15 +149,14 @@ test_PyTime_AsMilliseconds(PyObject *self, PyObject *args)
return NULL;
}
PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
PyTime_t ms = _PyTime_AsMilliseconds(t, round);
PyTime_t ns = _PyTime_FromNanoseconds(ms);
return _PyTime_AsNanosecondsObject(ns);
return _PyTime_AsLong(ms);
}
static PyObject *
@ -169,15 +168,14 @@ test_PyTime_AsMicroseconds(PyObject *self, PyObject *args)
return NULL;
}
PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
PyTime_t us = _PyTime_AsMicroseconds(t, round);
PyTime_t ns = _PyTime_FromNanoseconds(us);
return _PyTime_AsNanosecondsObject(ns);
return _PyTime_AsLong(us);
}
static PyObject *

View file

@ -127,7 +127,7 @@ time_time_ns(PyObject *self, PyObject *unused)
if (PyTime_Time(&t) < 0) {
return NULL;
}
return _PyTime_AsNanosecondsObject(t);
return _PyTime_AsLong(t);
}
PyDoc_STRVAR(time_ns_doc,
@ -164,8 +164,7 @@ py_clock(time_module_state *state, PyTime_t *tp, _Py_clock_info_t *info)
"or its value cannot be represented");
return -1;
}
PyTime_t ns = _PyTimeFraction_Mul(ticks, base);
*tp = _PyTime_FromNanoseconds(ns);
*tp = _PyTimeFraction_Mul(ticks, base);
return 0;
}
#endif /* HAVE_CLOCK */
@ -259,7 +258,7 @@ time_clock_gettime_ns_impl(PyObject *module, clockid_t clk_id)
if (_PyTime_FromTimespec(&t, &ts) < 0) {
return NULL;
}
return _PyTime_AsNanosecondsObject(t);
return _PyTime_AsLong(t);
}
#endif /* HAVE_CLOCK_GETTIME */
@ -308,7 +307,7 @@ time_clock_settime_ns(PyObject *self, PyObject *args)
return NULL;
}
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
if (_PyTime_AsTimespec(t, &ts) == -1) {
@ -1170,7 +1169,7 @@ time_monotonic_ns(PyObject *self, PyObject *unused)
if (PyTime_Monotonic(&t) < 0) {
return NULL;
}
return _PyTime_AsNanosecondsObject(t);
return _PyTime_AsLong(t);
}
PyDoc_STRVAR(monotonic_ns_doc,
@ -1202,7 +1201,7 @@ time_perf_counter_ns(PyObject *self, PyObject *unused)
if (PyTime_PerfCounter(&t) < 0) {
return NULL;
}
return _PyTime_AsNanosecondsObject(t);
return _PyTime_AsLong(t);
}
PyDoc_STRVAR(perf_counter_ns_doc,
@ -1233,7 +1232,7 @@ process_time_times(time_module_state *state, PyTime_t *tp,
PyTime_t ns;
ns = _PyTimeFraction_Mul(process.tms_utime, base);
ns += _PyTimeFraction_Mul(process.tms_stime, base);
*tp = _PyTime_FromNanoseconds(ns);
*tp = ns;
return 1;
}
#endif
@ -1247,7 +1246,7 @@ py_process_time(time_module_state *state, PyTime_t *tp,
HANDLE process;
FILETIME creation_time, exit_time, kernel_time, user_time;
ULARGE_INTEGER large;
PyTime_t ktime, utime, t;
PyTime_t ktime, utime;
BOOL ok;
process = GetCurrentProcess();
@ -1274,8 +1273,7 @@ py_process_time(time_module_state *state, PyTime_t *tp,
utime = large.QuadPart;
/* ktime and utime have a resolution of 100 nanoseconds */
t = _PyTime_FromNanoseconds((ktime + utime) * 100);
*tp = t;
*tp = (ktime + utime) * 100;
return 0;
#else
@ -1383,7 +1381,7 @@ time_process_time_ns(PyObject *module, PyObject *unused)
if (py_process_time(state, &t, NULL) < 0) {
return NULL;
}
return _PyTime_AsNanosecondsObject(t);
return _PyTime_AsLong(t);
}
PyDoc_STRVAR(process_time_ns_doc,
@ -1401,7 +1399,7 @@ _PyTime_GetThreadTimeWithInfo(PyTime_t *tp, _Py_clock_info_t *info)
HANDLE thread;
FILETIME creation_time, exit_time, kernel_time, user_time;
ULARGE_INTEGER large;
PyTime_t ktime, utime, t;
PyTime_t ktime, utime;
BOOL ok;
thread = GetCurrentThread();
@ -1428,8 +1426,7 @@ _PyTime_GetThreadTimeWithInfo(PyTime_t *tp, _Py_clock_info_t *info)
utime = large.QuadPart;
/* ktime and utime have a resolution of 100 nanoseconds */
t = _PyTime_FromNanoseconds((ktime + utime) * 100);
*tp = t;
*tp = (ktime + utime) * 100;
return 0;
}
@ -1453,7 +1450,7 @@ _PyTime_GetThreadTimeWithInfo(PyTime_t *tp, _Py_clock_info_t *info)
info->adjustable = 0;
info->resolution = 1e-9;
}
*tp = _PyTime_FromNanoseconds(tc.stime + tc.utime);
*tp = (tc.stime + tc.utime);
return 0;
}
@ -1470,7 +1467,7 @@ _PyTime_GetThreadTimeWithInfo(PyTime_t *tp, _Py_clock_info_t *info)
info->monotonic = 1;
info->adjustable = 0;
}
*tp = _PyTime_FromNanoseconds(gethrvtime());
*tp = gethrvtime();
return 0;
}
@ -1550,7 +1547,7 @@ time_thread_time_ns(PyObject *self, PyObject *unused)
if (_PyTime_GetThreadTimeWithInfo(&t, NULL) < 0) {
return NULL;
}
return _PyTime_AsNanosecondsObject(t);
return _PyTime_AsLong(t);
}
PyDoc_STRVAR(thread_time_ns_doc,