From d4d50a339d55cde8c8d0e50a1286e1e20457f4b6 Mon Sep 17 00:00:00 2001 From: Discord9 Date: Thu, 10 Nov 2022 10:39:41 +0800 Subject: [PATCH 1/3] feat: allow retry if as_mut fail --- vm/src/dictdatatype.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/vm/src/dictdatatype.rs b/vm/src/dictdatatype.rs index 2746c90e1..ad0123bda 100644 --- a/vm/src/dictdatatype.rs +++ b/vm/src/dictdatatype.rs @@ -248,9 +248,17 @@ impl Dict { if let Some(index) = entry_index.index() { // Update existing key if let Some(entry) = inner.entries.get_mut(index) { - let entry = entry - .as_mut() - .expect("The dict was changed since we did lookup."); + let entry = if let Some(entry) = entry.as_mut() { + entry + } else { + // The dict was changed since we did lookup. Let's try again. + // this is very rare to happen + // (and seems only happen with very high freq gc, and about three to four time in 200000 iters) + // but still possible + // so logging a warn is acceptable in here + warn!("The dict was changed since we did lookup. Let's try again."); + continue; + }; if entry.index == index_index { let removed = std::mem::replace(&mut entry.value, value); // defer dec RC From 4b92ff5fffa98fa8c17e7ec27a681143d12262d3 Mon Sep 17 00:00:00 2001 From: Discord9 Date: Sat, 12 Nov 2022 20:12:40 +0800 Subject: [PATCH 2/3] log: remove warning --- vm/src/dictdatatype.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/vm/src/dictdatatype.rs b/vm/src/dictdatatype.rs index ad0123bda..eae434ded 100644 --- a/vm/src/dictdatatype.rs +++ b/vm/src/dictdatatype.rs @@ -253,10 +253,8 @@ impl Dict { } else { // The dict was changed since we did lookup. Let's try again. // this is very rare to happen - // (and seems only happen with very high freq gc, and about three to four time in 200000 iters) + // (and seems only happen with very high freq gc, and about one time in 10000 iters) // but still possible - // so logging a warn is acceptable in here - warn!("The dict was changed since we did lookup. Let's try again."); continue; }; if entry.index == index_index { From 6a66ae20b59e5c9796e0bd79df8f979e57019fa6 Mon Sep 17 00:00:00 2001 From: Discord9 Date: Sat, 12 Nov 2022 20:39:20 +0800 Subject: [PATCH 3/3] refactor: use `let-else` --- vm/src/dictdatatype.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/vm/src/dictdatatype.rs b/vm/src/dictdatatype.rs index eae434ded..9cdd67a50 100644 --- a/vm/src/dictdatatype.rs +++ b/vm/src/dictdatatype.rs @@ -248,9 +248,7 @@ impl Dict { if let Some(index) = entry_index.index() { // Update existing key if let Some(entry) = inner.entries.get_mut(index) { - let entry = if let Some(entry) = entry.as_mut() { - entry - } else { + let Some(entry) = entry.as_mut() else { // The dict was changed since we did lookup. Let's try again. // this is very rare to happen // (and seems only happen with very high freq gc, and about one time in 10000 iters)