From 542c8cf9ef310ea8a27ebab948f96595300dcf5a Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 6 Oct 2016 00:50:55 +0300 Subject: [PATCH] upip: save_file(): Optimize, use inplace buffer. --- upip/upip.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/upip/upip.py b/upip/upip.py index a590d2a4..34cd2052 100644 --- a/upip/upip.py +++ b/upip/upip.py @@ -11,6 +11,8 @@ DEFAULT_MICROPYPATH = "~/.micropython/lib:/usr/lib/micropython" debug = False cleanup_files = [] +file_buf = bytearray(512) + class NotFoundError(Exception): pass @@ -44,13 +46,13 @@ def _makedirs(name, mode=0o777): def save_file(fname, subf): - outf = open(fname, "wb") - while True: - buf = subf.read(1024) - if not buf: - break - outf.write(buf) - outf.close() + global file_buf + with open(fname, "wb") as outf: + while True: + sz = subf.readinto(file_buf) + if not sz: + break + outf.write(file_buf, sz) def install_tar(f, prefix): meta = {}