- /* store index of smallest zset in variable j */
- for (i = 0, j = 0; i < zsetnum; i++) {
- if (!srcdicts[i] || dictSize(srcdicts[i]) == 0) {
- break;
- }
- if (dictSize(srcdicts[i]) < dictSize(srcdicts[j])) {
- j = i;
- }
- }
- /* skip going over all entries if at least one dict was NULL or empty */
- if (i == zsetnum) {
- /* precondition: all srcdicts are non-NULL and non-empty */
- di = dictGetIterator(srcdicts[j]);
+ /* sort sets from the smallest to largest, this will improve our
+ * algorithm's performance */
+ qsort(src,zsetnum,sizeof(zsetopsrc), qsortCompareZsetopsrcByCardinality);
+
+ /* skip going over all entries if the smallest zset is NULL or empty */
+ if (src[0].dict && dictSize(src[0].dict) > 0) {
+ /* precondition: as src[0].dict is non-empty and the zsets are ordered
+ * from small to large, all src[i > 0].dict are non-empty too */
+ di = dictGetIterator(src[0].dict);