GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-29312: use METH_FASTCALL for dict.update by jdemeyer · Pull Request #14589 · python/cpython

bpo-29312: use METH_FASTCALL for dict.update #14589

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.

Sign up for GitHub

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

Closed
jdemeyer wants to merge 1 commit into python:main from jdemeyer:dict_update

Uh oh!

There was an error while loading. Please reload this page.

Closed

bpo-29312: use METH_FASTCALL for dict.update #14589

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
df4444a
bpo-29312: use METH_FASTCALL for dict.update()
jdemeyer Jul 4, 2019
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

4 changes: 3 additions & 1 deletion 4 Lib/test/test_dict.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,11 @@ def test_update(self):
d.update({2:20})
d.update({1:1, 2:2, 3:3})
self.assertEqual(d, {1:1, 2:2, 3:3})
d.update(([i,i] for i in range(4,6)), key="value")
self.assertEqual(d, {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 'key': 'value'})

d.update()
self.assertEqual(d, {1:1, 2:2, 3:3})
self.assertEqual(d, {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 'key': 'value'})

self.assertRaises((TypeError, AttributeError), d.update, None)

Expand Down
106 changes: 95 additions & 11 deletions 106 Objects/dictobject.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2340,16 +2340,6 @@ dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
return result;
}

/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
slower, see the issue #29312. */
static PyObject *
dict_update(PyObject *self, PyObject *args, PyObject *kwds)
{
if (dict_update_common(self, args, kwds, "update") != -1)
Py_RETURN_NONE;
return NULL;
}

/* Update unconditionally replaces existing items.
Merge has a 3rd argument 'override'; if set, it acts like Update,
Expand Down Expand Up @@ -2591,6 +2581,100 @@ dict_merge(PyObject *a, PyObject *b, int override)
return 0;
}

/* Implementation of d.update(obj). There are two cases:
* - if obj has a "keys" method, assume that it is a mapping
* - otherwise, obj must be an iterable of (key, value) pairs
*/
static int
dict_merge_from_any(PyObject *self, PyObject *obj)
{
/* Fast path for dict subclasses */
if (PyDict_Check(obj)) {
return dict_merge(self, obj, 1);
}

_Py_IDENTIFIER(keys);
Comment thread
methane marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

PyObject *func;
if (_PyObject_LookupAttrId(obj, &PyId_keys, &func) < 0) {
Comment thread Outdated

@vstinner vstinner Jul 9, 2019

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds redundant with dict_merge() which calls PyMapping_Keys(). Maybe dict_merge() could be modified to dispatch to PyDict_MergeFromSeq2() if PyMapping_Keys() keys with an attribute error, rather than trying to get the attribute twice?

It's not directly related to your change, it can be done/discussed in a separated PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jdemeyer jdemeyer Jul 9, 2019

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I also noticed this. There is some room for improvement here. Let's do it in a new PR after this is merged.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
return -1;
}
if (func != NULL) {
Py_DECREF(func);
return dict_merge(self, obj, 1);
}
else {
return PyDict_MergeFromSeq2(self, obj, 1);
}
}

/* Insert the key:value pairs from the arrays keys and values (of length n).
* Return 0 if successful, -1 if an exception was raised */
static int
dict_merge_from_arrays(PyDictObject *mp, PyObject *const *keys, PyObject *const *values, Py_ssize_t n)
{
/* Do one big resize at the start, rather than
* incrementally resizing as we insert new items. Expect
* that there will be no (or few) overlapping keys.
*/
if (USABLE_FRACTION(mp->ma_keys->dk_size) < n) {
if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + n))) {
return -1;
}
}

for (Py_ssize_t i = 0; i < n; i++) {
PyObject *key = keys[i];
Comment thread
jdemeyer marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

PyObject *value = values[i];
assert(PyUnicode_Check(key));
Py_hash_t hash = ((PyASCIIObject *)key)->hash;
if (hash == -1) {
hash = PyObject_Hash(key);
if (hash == -1) {
return -1;
}
}
if (insertdict(mp, key, hash, value)) {
return -1;
}
}
ASSERT_CONSISTENT(mp);
return 0;
}


static int
dict_update_impl(PyObject *self, PyObject *const *args,
Py_ssize_t nargs, PyObject *kwnames,
const char *methname)
{
if (nargs) {
if (!_PyArg_CheckPositional(methname, nargs, 0, 1)) {
return -1;
}
if (dict_merge_from_any(self, args[0])) {
return -1;
}
args += 1;
}
if (kwnames) {
Py_ssize_t nkw = PyTuple_GET_SIZE(kwnames);
PyObject *const *names = &PyTuple_GET_ITEM(kwnames, 0);
return dict_merge_from_arrays((PyDictObject*)self, names, args, nkw);
}
return 0;
}

static PyObject *
dict_update(PyObject *self, PyObject *const *args,
Py_ssize_t nargs, PyObject *kwnames)
{
if (dict_update_impl(self, args, nargs, kwnames, "update")) {
return NULL;
}
Py_RETURN_NONE;
}


int
PyDict_Update(PyObject *a, PyObject *b)
{
Expand Down Expand Up @@ -3193,7 +3277,7 @@ static PyMethodDef mapp_methods[] = {
items__doc__},
{"values", dictvalues_new, METH_NOARGS,
values__doc__},
{"update", (PyCFunction)(void(*)(void))dict_update, METH_VARARGS | METH_KEYWORDS,
{"update", (PyCFunction)(void(*)(void))dict_update, METH_FASTCALL | METH_KEYWORDS,
update__doc__},
DICT_FROMKEYS_METHODDEF
{"clear", (PyCFunction)dict_clear, METH_NOARGS,
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.