It seems to me this complex condition contains a repeated hidden sub-expression that’s needed later as empty_cache
.
Could we split this up to reduce duplication and simplify the expressions?
0bool should_empty_cache = (mode == FlushStateMode::FORCE_FLUSH) || fCacheLarge || fCacheCritical;
1bool fDoFullFlush = should_empty_cache || (mode == FlushStateMode::FORCE_SYNC) || fPeriodicFlush || fFlushForPrune;
2...
3// Flush the chainstate (which may refer to block index entries).
4if (should_empty_cache ? !CoinsTip().Flush() : !CoinsTip().Sync()) {
5 return FatalError(m_chainman.GetNotifications(), state, _("Failed to write to coin database."));
6}
0diff --git a/src/validation.cpp b/src/validation.cpp
1--- a/src/validation.cpp (revision 1ede4803f1f5dffa55644d908062b52bf71394e7)
2+++ b/src/validation.cpp (date 1731581531144)
3@@ -2807,7 +2807,6 @@
4 try {
5 {
6 bool fFlushForPrune = false;
7- bool fDoFullFlush = false;
8
9 CoinsCacheSizeState cache_state = GetCoinsCacheSizeState();
10 LOCK(m_blockman.cs_LastBlockFile);
11@@ -2869,7 +2868,8 @@
12 // It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.
13 bool fPeriodicFlush = mode == FlushStateMode::PERIODIC && nNow > m_last_flush + DATABASE_FLUSH_INTERVAL;
14 // Combine all conditions that result in a full cache flush.
15- fDoFullFlush = (mode == FlushStateMode::FORCE_FLUSH) || (mode == FlushStateMode::FORCE_SYNC) || fCacheLarge || fCacheCritical || fPeriodicFlush || fFlushForPrune;
16+ bool should_empty_cache = (mode == FlushStateMode::FORCE_FLUSH) || fCacheLarge || fCacheCritical;
17+ bool fDoFullFlush = should_empty_cache || (mode == FlushStateMode::FORCE_SYNC) || fPeriodicFlush || fFlushForPrune;
18 // Write blocks and block index to disk.
19 if (fDoFullFlush || fPeriodicWrite) {
20 // Ensure we can write block index
21@@ -2917,8 +2917,7 @@
22 return FatalError(m_chainman.GetNotifications(), state, _("Disk space is too low!"));
23 }
24 // Flush the chainstate (which may refer to block index entries).
25- const auto empty_cache{(mode == FlushStateMode::FORCE_FLUSH) || fCacheLarge || fCacheCritical};
26- if (empty_cache ? !CoinsTip().Flush() : !CoinsTip().Sync()) {
27+ if (should_empty_cache ? !CoinsTip().Flush() : !CoinsTip().Sync()) {
28 return FatalError(m_chainman.GetNotifications(), state, _("Failed to write to coin database."));
29 }
30 m_last_flush = nNow;