Repository: tikv/tikv
File: components/raftstore/src/store/peer.rs (Rust)

Code before:
            unpersisted_readies: VecDeque::default(),
            unpersisted_message_count: 0,
            unpersisted_ready: None,
            persisted_number: 0,
            apply_snap_ctx: None,
            region_buckets_info: BucketStatsInfo::default(),
            transfer_leader_state: TransferLeaderState::default(),
            unsafe_recovery_state: None,
            snapshot_recovery_state: None,
            busy_on_apply: Some(false),
            last_leader_committed_idx: None,
            uncampaigned_new_regions: None,
        };

        // If this region has only one peer and I am the one, campaign directly.
        if region.get_peers().len() == 1 && region.get_peers()[0].get_store_id() == store_id {
            peer.raft_group.campaign()?;
        }

        let (persisted_index, term) = (
            peer.raft_group.raft.raft_log.persisted,
            peer.raft_group.raft.term,
        );
        peer.mut_store().update_cache_persisted(persisted_index);
        // For initilization, uses the persisted raft log to initialize the term cache.
        peer.mut_store().update_term_cache(persisted_index, term);
        Ok(peer)
    }

    /// Sets commit group to the peer.
    pub fn init_replication_mode(&mut self, state: &mut GlobalReplicationState) {
        debug!("init commit group"; "state" => ?state, "region_id" => self.region_id, "peer_id" => self.peer.id);
        if self.is_initialized() {
            let version = state.status().get_dr_auto_sync().state_id;
            let gb = state.calculate_commit_group(version, self.get_store().region().get_peers());
            self.raft_group.raft.assign_commit_groups(gb);
        }
        self.replication_sync = false;
        if state.status().get_mode() == ReplicationMode::Majority {
            self.raft_group.raft.enable_group_commit(false);
            self.replication_mode_version = 0;
            self.dr_auto_sync_state = DrAutoSyncState::Async;
            return;
        }
        self.replication_mode_version = state.status().get_dr_auto_sync().state_id;
        let enable = !matches!(
            state.status().get_dr_auto_sync().get_state(),
            DrAutoSyncState::Async | DrAutoSyncState::SyncRecover
        );
        self.raft_group.raft.enable_group_commit(enable);
        self.dr_auto_sync_state = state.status().get_dr_auto_sync().get_state();

Review comment (performance):
How about iterate raft logs to initialize term cache? It should be reasonably fast, because for split regions and replicated regions,  there is no raft logs.

Code after:
            unpersisted_readies: VecDeque::default(),
            unpersisted_message_count: 0,
            unpersisted_ready: None,
            persisted_number: 0,
            apply_snap_ctx: None,
            region_buckets_info: BucketStatsInfo::default(),
            transfer_leader_state: TransferLeaderState::default(),
            unsafe_recovery_state: None,
            snapshot_recovery_state: None,
            busy_on_apply: Some(false),
            last_leader_committed_idx: None,
            uncampaigned_new_regions: None,
        };

        // If this region has only one peer and I am the one, campaign directly.
        if region.get_peers().len() == 1 && region.get_peers()[0].get_store_id() == store_id {
            peer.raft_group.campaign()?;
        }

        let persisted_index = peer.raft_group.raft.raft_log.persisted;
        peer.mut_store().update_cache_persisted(persisted_index);
        Ok(peer)
    }

    /// Sets commit group to the peer.
    pub fn init_replication_mode(&mut self, state: &mut GlobalReplicationState) {
        debug!("init commit group"; "state" => ?state, "region_id" => self.region_id, "peer_id" => self.peer.id);
        if self.is_initialized() {
            let version = state.status().get_dr_auto_sync().state_id;
            let gb = state.calculate_commit_group(version, self.get_store().region().get_peers());
            self.raft_group.raft.assign_commit_groups(gb);
        }
        self.replication_sync = false;
        if state.status().get_mode() == ReplicationMode::Majority {
            self.raft_group.raft.enable_group_commit(false);
            self.replication_mode_version = 0;
            self.dr_auto_sync_state = DrAutoSyncState::Async;
            return;
        }
        self.replication_mode_version = state.status().get_dr_auto_sync().state_id;
        let enable = !matches!(
            state.status().get_dr_auto_sync().get_state(),
            DrAutoSyncState::Async | DrAutoSyncState::SyncRecover
        );
        self.raft_group.raft.enable_group_commit(enable);
        self.dr_auto_sync_state = state.status().get_dr_auto_sync().get_state();
    }

    /// Updates replication mode.
    pub fn switch_replication_mode(&mut self, state: &Mutex<GlobalReplicationState>) {
        self.replication_sync = false;