Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit f8bf83b

Browse files
authored
Skip the final GC on shutdown to improve restart times (#10712)
Use `gc.freeze()` on exit to exclude all existing objects from the final GC. In testing, this sped up shutdown by up to a few seconds. `gc.freeze()` runs in constant time, so there is little chance of performance regression. Signed-off-by: Sean Quah <[email protected]>
1 parent e2481db commit f8bf83b

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

changelog.d/10712.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Skip final GC at shutdown to improve restart performance.

synapse/app/_base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15+
import atexit
1516
import gc
1617
import logging
1718
import os
@@ -403,6 +404,12 @@ def run_sighup(*args, **kwargs):
403404
gc.collect()
404405
gc.freeze()
405406

407+
# Speed up shutdowns by freezing all allocated objects. This moves everything
408+
# into the permanent generation and excludes them from the final GC.
409+
# Unfortunately only works on Python 3.7
410+
if platform.python_implementation() == "CPython" and sys.version_info >= (3, 7):
411+
atexit.register(gc.freeze)
412+
406413

407414
def setup_sentry(hs):
408415
"""Enable sentry integration, if enabled in configuration

0 commit comments

Comments
 (0)