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

Commit

Permalink
Skip the final GC on shutdown to improve restart times (#10712)
Browse files Browse the repository at this point in the history
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 <seanq@element.io>
  • Loading branch information
squahtx committed Sep 1, 2021
1 parent e2481db commit f8bf83b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.d/10712.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Skip final GC at shutdown to improve restart performance.
7 changes: 7 additions & 0 deletions synapse/app/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import atexit
import gc
import logging
import os
Expand Down Expand Up @@ -403,6 +404,12 @@ def run_sighup(*args, **kwargs):
gc.collect()
gc.freeze()

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


def setup_sentry(hs):
"""Enable sentry integration, if enabled in configuration
Expand Down

0 comments on commit f8bf83b

Please sign in to comment.