From c84953a7d43599425e4a8d0d4f340615e2f3bb03 Mon Sep 17 00:00:00 2001 From: James Stone Date: Wed, 3 May 2023 17:19:39 -0700 Subject: [PATCH 1/2] Reduce the memory footprint of an automatic reset The sync history can be short circuited because it will be discarded right after `transfer_group()` --- CHANGELOG.md | 2 +- src/realm/sync/noinst/client_reset.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e924e028376..1d8e8108d82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,7 @@ ----------- ### Internals -* None. +* Reduce the memory footprint of an automatic (discard or recover) client reset when there are large incoming changes from the server. ([#6567](https://github.com/realm/realm-core/issues/6567)) ---------------------------------------------- diff --git a/src/realm/sync/noinst/client_reset.cpp b/src/realm/sync/noinst/client_reset.cpp index 17c1cb7c12b..a801bcdbb80 100644 --- a/src/realm/sync/noinst/client_reset.cpp +++ b/src/realm/sync/noinst/client_reset.cpp @@ -74,6 +74,15 @@ void transfer_group(const Transaction& group_src, Transaction& group_dst, util:: { logger.debug("transfer_group, src size = %1, dst size = %2", group_src.size(), group_dst.size()); + // Turn off the sync history tracking during state transfer since it will be thrown + // away immediately after anyways. This reduces the memory footprint of a client reset. + ClientReplication* client_repl = dynamic_cast(group_dst.get_replication()); + REALM_ASSERT_RELEASE(client_repl); + client_repl->set_short_circuit(true); + util::ScopeExit sync_history_guard([client_repl]() noexcept { + client_repl->set_short_circuit(false); + }); + // Find all tables in dst that should be removed. std::set tables_to_remove; for (auto table_key : group_dst.get_table_keys()) { From e1573f7a50d1ee7ee0b95884a49ffc3eb3f03744 Mon Sep 17 00:00:00 2001 From: James Stone Date: Wed, 3 May 2023 17:42:30 -0700 Subject: [PATCH 2/2] use TempShortCircuitReplication --- src/realm/sync/noinst/client_reset.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/realm/sync/noinst/client_reset.cpp b/src/realm/sync/noinst/client_reset.cpp index a801bcdbb80..6d5fee9f280 100644 --- a/src/realm/sync/noinst/client_reset.cpp +++ b/src/realm/sync/noinst/client_reset.cpp @@ -78,10 +78,7 @@ void transfer_group(const Transaction& group_src, Transaction& group_dst, util:: // away immediately after anyways. This reduces the memory footprint of a client reset. ClientReplication* client_repl = dynamic_cast(group_dst.get_replication()); REALM_ASSERT_RELEASE(client_repl); - client_repl->set_short_circuit(true); - util::ScopeExit sync_history_guard([client_repl]() noexcept { - client_repl->set_short_circuit(false); - }); + TempShortCircuitReplication sync_history_guard(*client_repl); // Find all tables in dst that should be removed. std::set tables_to_remove;