Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Optimize :meth:`set.intersection_update` by replacing the general-purpose
``set_swap_bodies()`` with a specialized ``set_replace_body()`` that skips
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

set_replace_body is a function internal to cpython so I don't think you need to mention it in the news entry as this won't affect user programs.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done, removed the NEWS entry in 100c491.

unnecessary atomic operations and checks for the temporary set argument.
102 changes: 48 additions & 54 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1475,74 +1475,68 @@ copy_small_table(setentry *dest, setentry *src)
}
#endif

/* set_swap_bodies() switches the contents of any two sets by moving their
internal data pointers and, if needed, copying the internal smalltables.
Semantically equivalent to:
/* set_replace_body() replaces the contents of dst with those of src,
moving dst's old contents into src for proper cleanup on Py_DECREF.

t=set(a); a.clear(); a.update(b); b.clear(); b.update(t); del t
The caller guarantees that src is a uniquely-referenced temporary set
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If the called is required to cleanup src immediately afterwards, maybe it is cleaner to let the set_replace_body steal the reference and do the decref.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

In set_intersection_update_multi_impl, I placed Py_DECREF(tmp) after Py_END_CRITICAL_SECTION().

My reasoning was that moving the decref inside set_replace_body would run the deallocation (and decref of all keys) while still holding the critical section lock, which I thought might be undesirable.

By keeping it outside, the caller has control over when deallocation happens.

This is my first PR though and I'm still getting familiar with the project, so if I'm wrong here - happy to rework it.

that will be discarded immediately afterward. This allows us to skip
atomic operations and shared-marking on src's fields, and to skip the
frozenset hash swap (neither argument is ever a frozenset here).

The function always succeeds and it leaves both objects in a stable state.
Useful for operations that update in-place (by allowing an intermediate
result to be swapped into one of the original inputs).
*/

static void
set_swap_bodies(PySetObject *a, PySetObject *b)
set_replace_body(PySetObject *dst, PySetObject *src)
{
Py_ssize_t t;
setentry *u;
setentry tab[PySet_MINSIZE];
Py_hash_t h;

setentry *a_table = a->table;
setentry *b_table = b->table;
FT_ATOMIC_STORE_PTR_RELEASE(a->table, NULL);
FT_ATOMIC_STORE_PTR_RELEASE(b->table, NULL);

t = a->fill; a->fill = b->fill; b->fill = t;
t = a->used;
FT_ATOMIC_STORE_SSIZE_RELAXED(a->used, b->used);
FT_ATOMIC_STORE_SSIZE_RELAXED(b->used, t);
t = a->mask;
FT_ATOMIC_STORE_SSIZE_RELEASE(a->mask, b->mask);
FT_ATOMIC_STORE_SSIZE_RELEASE(b->mask, t);

u = a_table;
if (a_table == a->smalltable)
u = b->smalltable;
a_table = b_table;
if (b_table == b->smalltable)
a_table = a->smalltable;
b_table = u;

if (a_table == a->smalltable || b_table == b->smalltable) {
memcpy(tab, a->smalltable, sizeof(tab));

assert(!PyType_IsSubtype(Py_TYPE(dst), &PyFrozenSet_Type));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you replace these two asserts with

assert(PySet_CheckExact((PyObject *) dst));
assert(PySet_CheckExact((PyObject *) src));

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'd prefer to keep the current assertions. dst can be a subclass of set (e.g. class S(set): pass; S([1,2]).intersection_update({2})), so PySet_CheckExact(dst) would fire on that legitimate case. make_new_set_basetype() normalizes the type for src, but dst is the original caller object.

The current form (!PyType_IsSubtype(..., &PyFrozenSet_Type)) checks exactly what we need: neither argument is a frozenset.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you use PySet_Check instead?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good point, switched to PySet_Check in 0c0a00a.

assert(!PyType_IsSubtype(Py_TYPE(src), &PyFrozenSet_Type));
assert(_PyObject_IsUniquelyReferenced((PyObject *)src));

setentry *dst_table = dst->table;
setentry *src_table = src->table;
FT_ATOMIC_STORE_PTR_RELEASE(dst->table, NULL);
src->table = NULL;

t = dst->fill; dst->fill = src->fill; src->fill = t;
t = dst->used;
FT_ATOMIC_STORE_SSIZE_RELAXED(dst->used, src->used);
src->used = t;
t = dst->mask;
FT_ATOMIC_STORE_SSIZE_RELEASE(dst->mask, src->mask);
src->mask = t;

u = dst_table;
if (dst_table == dst->smalltable)
u = src->smalltable;
dst_table = src_table;
if (src_table == src->smalltable)
dst_table = dst->smalltable;
src_table = u;

if (dst_table == dst->smalltable || src_table == src->smalltable) {
memcpy(tab, dst->smalltable, sizeof(tab));
#ifndef Py_GIL_DISABLED
memcpy(a->smalltable, b->smalltable, sizeof(tab));
memcpy(b->smalltable, tab, sizeof(tab));
memcpy(dst->smalltable, src->smalltable, sizeof(tab));
memcpy(src->smalltable, tab, sizeof(tab));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This memcpy and the one below it on line 1528 can be pulled out of the macro.

Also, I find this entire section for copying the smalltable a bit awkward. Right now, this section is the only time copy_small_table is called so we could just move the for loop in copy_small_table into this function. Additionally, we could make copy_small_table exist for GIL builds and have it use memcpy as the default unless the GIL is disabled. Something roughly like this:

static void
copy_small_table(setentry *dest, setentry *src)
{
#ifdef Py_GIL_DISABLED
    for (Py_ssize_t i = 0; i < PySet_MINSIZE; i++) {
        _Py_atomic_store_ptr_release(&dest[i].key, src[i].key);
        _Py_atomic_store_ssize_relaxed(&dest[i].hash, src[i].hash);
    }
#else
    memcpy(dest, src, PySet_MINSIZE * sizeof(setentry));
#endif
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good suggestion, done in 100c491. Moved the #ifdef Py_GIL_DISABLED branch inside copy_small_table so it uses memcpy in GIL builds and atomic stores in free-threaded builds. The callsite is clean now - no preprocessor conditionals.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry if I was not clear. There were two options that I suggested:

  1. Never use copy_small_table and move the logic into set_replace_body
  2. Never use memcpy in set_replace_body and instead call copy_small table

I'm not sure which option is best. However, if you want to implement the second option, I would make sure to no longer call memcpy in set_replace_body.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Wait actually on second thought I realized that you probably should not make that change. Never mind.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

No worries. Thanks for the thorough review, really appreciate it!

#else
copy_small_table(a->smalltable, b->smalltable);
copy_small_table(b->smalltable, tab);
copy_small_table(dst->smalltable, src->smalltable);
memcpy(src->smalltable, tab, sizeof(tab));
#endif
}

if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type) &&
PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
h = FT_ATOMIC_LOAD_SSIZE_RELAXED(a->hash);
FT_ATOMIC_STORE_SSIZE_RELAXED(a->hash, FT_ATOMIC_LOAD_SSIZE_RELAXED(b->hash));
FT_ATOMIC_STORE_SSIZE_RELAXED(b->hash, h);
} else {
FT_ATOMIC_STORE_SSIZE_RELAXED(a->hash, -1);
FT_ATOMIC_STORE_SSIZE_RELAXED(b->hash, -1);
}
if (!SET_IS_SHARED(b) && SET_IS_SHARED(a)) {
SET_MARK_SHARED(b);
}
if (!SET_IS_SHARED(a) && SET_IS_SHARED(b)) {
SET_MARK_SHARED(a);
FT_ATOMIC_STORE_SSIZE_RELAXED(dst->hash, -1);

if (SET_IS_SHARED(dst)) {
SET_MARK_SHARED(src);
}
FT_ATOMIC_STORE_PTR_RELEASE(a->table, a_table);
FT_ATOMIC_STORE_PTR_RELEASE(b->table, b_table);

FT_ATOMIC_STORE_PTR_RELEASE(dst->table, dst_table);
src->table = src_table;
}

/*[clinic input]
Expand Down Expand Up @@ -1797,7 +1791,7 @@ set_intersection_update(PySetObject *so, PyObject *other)
tmp = set_intersection(so, other);
if (tmp == NULL)
return NULL;
set_swap_bodies(so, (PySetObject *)tmp);
set_replace_body(so, (PySetObject *)tmp);
Py_DECREF(tmp);
Py_RETURN_NONE;
}
Expand All @@ -1821,7 +1815,7 @@ set_intersection_update_multi_impl(PySetObject *so, PyObject * const *others,
if (tmp == NULL)
return NULL;
Py_BEGIN_CRITICAL_SECTION(so);
set_swap_bodies(so, (PySetObject *)tmp);
set_replace_body(so, (PySetObject *)tmp);
Py_END_CRITICAL_SECTION();
Py_DECREF(tmp);
Py_RETURN_NONE;
Expand Down
Loading