Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-117398: Statically Allocate the Datetime C-API #119472

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

Merged
merged 6 commits into from
May 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make the zero delta a global singleton.
  • Loading branch information
ericsnowcurrently committed May 23, 2024
commit 773ddca3846e9b0d93a823ab614646ad23f9416c
27 changes: 27 additions & 0 deletions 27 Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,8 @@ new_time_subclass_fold_ex(int hour, int minute, int second, int usecond,
return t;
}

static PyDateTime_Delta * look_up_delta(int, int, int, PyTypeObject *);

/* Create a timedelta instance. Normalize the members iff normalize is
* true. Passing false is a speed optimization, if you know for sure
* that seconds and microseconds are already in their proper ranges. In any
Expand All @@ -1198,6 +1200,12 @@ new_delta_ex(int days, int seconds, int microseconds, int normalize,
if (check_delta_day_range(days) < 0)
return NULL;

self = look_up_delta(days, seconds, microseconds, type);
if (self != NULL) {
return (PyObject *)self;
}
assert(!PyErr_Occurred());

self = (PyDateTime_Delta *) (type->tp_alloc(type, 0));
if (self != NULL) {
self->hashcode = -1;
Expand Down Expand Up @@ -2892,6 +2900,25 @@ static PyTypeObject PyDateTime_DeltaType = {
0, /* tp_free */
};

// XXX Can we make this const?
static PyDateTime_Delta zero_delta = {
PyObject_HEAD_INIT(&PyDateTime_DeltaType)
/* Letting this be set lazily is a benign race. */
.hashcode = -1,
};

static PyDateTime_Delta *
look_up_delta(int days, int seconds, int microseconds, PyTypeObject *type)
{
if (days == 0 && seconds == 0 && microseconds == 0
&& type == zero_delta.ob_base.ob_type)
{
return &zero_delta;
}
return NULL;
}


/*
* PyDateTime_Date implementation.
*/
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.