set.difference() may do one unneeded copy of the set
In some cases, set.difference() may do one unneeded copy of the set:
difference() ==copy==> descr_difference_update() => difference_update() => strategy.difference_update() => case len(set) < len(other)
In this case, it uses _difference_base()
which will only read the copy done earlier, and create the result from it.
This was found after looking at https://stackoverflow.com/questions/75701809/why-does-set-subtraction-and-difference-run-at-different-speeds/75703362#75703362, which shows another slow-down case: descr_sub() goes through a different path, in which for some reason there is no check for case len(set) >= len(other)
. It is missing that optimization. Maybe both issues could be fixed by simplifying a bit the pathways in setobject.py somehow?
Edited by Armin Rigo