From 861a42639579d34f1f6d3b23f642f9d7bb21f5ef Mon Sep 17 00:00:00 2001 From: Mike Lang Date: Fri, 8 Nov 2024 00:55:58 +0000 Subject: [PATCH] thrimshim: Fix performance issue with getting template png Bytes returned from the database are a "memoryview" and not a bytes object. These mostly behave the same, but one difference is that Flask recognizes a bytes object as something it can send as-is, whereas a memoryview ends up using its generic "iterable" processing. This results in sending every individual byte of the result as a single part of a HTTP chunked encoding response, adding 5 bytes and a syscall to every byte returned. The solution is to explicitly convert it to bytes before returning. --- thrimshim/thrimshim/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thrimshim/thrimshim/main.py b/thrimshim/thrimshim/main.py index 99c1190..48b94f2 100644 --- a/thrimshim/thrimshim/main.py +++ b/thrimshim/thrimshim/main.py @@ -628,7 +628,7 @@ def get_template(name): image = row[0] logging.info('Thumbnail image of {} fetched'.format(name)) - return flask.Response(image, mimetype='image/png') + return flask.Response(bytes(image), mimetype='image/png') @app.route('/thrimshim/template-metadata/')