Skip to content

Commit ea8f540

Browse files
committed
Move gc.collect off the critical path to reduce latency
1 parent 07c2347 commit ea8f540

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

views/utils.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import glob
2-
import gc
32
import os
43
from dataclasses import dataclass
54
from pathlib import Path
@@ -115,7 +114,6 @@ def _load_challenges() -> dict[ChallengeName, Challenge]:
115114
@staticmethod
116115
def _type_check_with_mypy(code) -> TypeCheckResult:
117116
raw_result = api.run(["--config-file", str(MYPY_CONFIG), "-c", code])
118-
gc.collect()
119117
return TypeCheckResult(
120118
stdout=raw_result[0], stderr=raw_result[1], passed=raw_result[2] == 0
121119
)

views/views.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import gc
12
import platform
23

34
import libcst as cst
4-
from flask import Blueprint, redirect, render_template, request
5+
from flask import Blueprint, redirect, render_template, request, Response
56

67
from .utils import challenge_manager
78

@@ -32,7 +33,7 @@ def get_challenge(name):
3233

3334

3435
@app_views.route("/run/<name>", methods=["POST"])
35-
def run_challenge(name) -> str:
36+
def run_challenge(name):
3637
code = request.get_data(as_text=True)
3738
try:
3839
module = cst.parse_module(code)
@@ -60,4 +61,12 @@ def run_challenge(name) -> str:
6061
" should fail type check, but it passed.</b>"
6162
)
6263

63-
return error_message
64+
response = Response(error_message)
65+
66+
# See https://twitter.com/Manjusaka_Lee/status/1720506781577937304
67+
# Call gc after returning the response, so that it's off the critical path.
68+
@response.call_on_close
69+
def cleanup_mypy_objects():
70+
gc.collect()
71+
72+
return response

0 commit comments

Comments
 (0)