save_list(): Rewrote, to untangle the proto 0 from the proto 1 cases.

The code is much easier to follow now, and I bet it's faster too.
This commit is contained in:
Tim Peters 2003-01-28 01:15:46 +00:00
parent 22dc6f4f7a
commit 21c18f0bf5
1 changed files with 17 additions and 15 deletions

View File

@ -483,24 +483,26 @@ class Pickler:
if self.bin: if self.bin:
write(EMPTY_LIST) write(EMPTY_LIST)
else: self.memoize(object)
n = len(object)
if n > 1:
write(MARK)
for element in object:
save(element)
write(APPENDS)
elif n:
assert n == 1
save(object[0])
write(APPEND)
# else the list is empty, and we're already done
else: # proto 0 -- can't use EMPTY_LIST or APPENDS
write(MARK + LIST) write(MARK + LIST)
self.memoize(object)
self.memoize(object) for element in object:
save(element)
using_appends = (self.bin and (len(object) > 1))
if using_appends:
write(MARK)
for element in object:
save(element)
if not using_appends:
write(APPEND) write(APPEND)
if using_appends:
write(APPENDS)
dispatch[ListType] = save_list dispatch[ListType] = save_list
def save_dict(self, object): def save_dict(self, object):