gh-112069: Do not require lock if the set has never been exposed. by corona10 · Pull Request #118069 · python/cpython

if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
return -1;

if (self->fill == 0 && Py_REFCNT(self) == 1) {

Choose a reason for hiding this comment

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

I think we should do the Py_REFCNT() check first so that we don't access self->fill without a lock in case there are other threads with references modifying self.

Also, do we need to set self->hash = -1 as we do below?

Choose a reason for hiding this comment

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

Also, do we need to set self->hash = -1 as we do below?

Well it will be more safe.