assert(iter != NULL);
- /* XXX: I think this function could be made faster by avoiding the
- iterator interface and fetching objects directly from list using
- PyList_GET_ITEM.
- */
-
if (self->proto == 0) {
/* APPENDS isn't available; do one at a time. */
for (;; total++) {
assert(obj != NULL);
assert(self->proto > 0);
assert(PyList_CheckExact(obj));
-
- if (PyList_GET_SIZE(obj) == 1) {
- item = PyList_GET_ITEM(obj, 0);
- Py_INCREF(item);
- int err = save(state, self, item, 0);
- Py_DECREF(item);
- if (err < 0) {
- _PyErr_FormatNote("when serializing %T item 0", obj);
- return -1;
- }
- if (_Pickler_Write(self, &append_op, 1) < 0)
- return -1;
- return 0;
- }
+ assert(PyList_GET_SIZE(obj));
/* Write in batches of BATCHSIZE. */
total = 0;
do {
+ if (PyList_GET_SIZE(obj) - total == 1) {
+ item = PyList_GET_ITEM(obj, total);
+ Py_INCREF(item);
+ int err = save(state, self, item, 0);
+ Py_DECREF(item);
+ if (err < 0) {
+ _PyErr_FormatNote("when serializing %T item %zd", obj, total);
+ return -1;
+ }
+ if (_Pickler_Write(self, &append_op, 1) < 0)
+ return -1;
+ return 0;
+ }
this_batch = 0;
if (_Pickler_Write(self, &mark_op, 1) < 0)
return -1;
assert(self->proto > 0);
dict_size = PyDict_GET_SIZE(obj);
-
- /* Special-case len(d) == 1 to save space. */
- if (dict_size == 1) {
- PyDict_Next(obj, &ppos, &key, &value);
- Py_INCREF(key);
- Py_INCREF(value);
- if (save(state, self, key, 0) < 0) {
- goto error;
- }
- if (save(state, self, value, 0) < 0) {
- _PyErr_FormatNote("when serializing %T item %R", obj, key);
- goto error;
- }
- Py_CLEAR(key);
- Py_CLEAR(value);
- if (_Pickler_Write(self, &setitem_op, 1) < 0)
- return -1;
- return 0;
- }
+ assert(dict_size);
/* Write in batches of BATCHSIZE. */
+ Py_ssize_t total = 0;
do {
+ if (dict_size - total == 1) {
+ PyDict_Next(obj, &ppos, &key, &value);
+ Py_INCREF(key);
+ Py_INCREF(value);
+ if (save(state, self, key, 0) < 0) {
+ goto error;
+ }
+ if (save(state, self, value, 0) < 0) {
+ _PyErr_FormatNote("when serializing %T item %R", obj, key);
+ goto error;
+ }
+ Py_CLEAR(key);
+ Py_CLEAR(value);
+ if (_Pickler_Write(self, &setitem_op, 1) < 0)
+ return -1;
+ return 0;
+ }
+
i = 0;
if (_Pickler_Write(self, &mark_op, 1) < 0)
return -1;
}
Py_CLEAR(key);
Py_CLEAR(value);
+ total++;
if (++i == BATCHSIZE)
break;
}
return -1;
}
- } while (i == BATCHSIZE);
+ } while (total < dict_size);
return 0;
error:
Py_XDECREF(key);
return 0; /* nothing to do */
/* Write in batches of BATCHSIZE. */
+ Py_ssize_t total = 0;
do {
i = 0;
if (_Pickler_Write(self, &mark_op, 1) < 0)
_PyErr_FormatNote("when serializing %T element", obj);
break;
}
+ total++;
if (++i == BATCHSIZE)
break;
}
"set changed size during iteration");
return -1;
}
- } while (i == BATCHSIZE);
+ } while (total < set_size);
return 0;
}