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

Code before:
        }
    }

    pub fn get_gen_snap_task(&self) -> Ref<'_, Option<GenSnapTask>> {
        self.gen_snap_task.borrow()
    }

    pub fn has_gen_snap_task(&self) -> bool {
        self.gen_snap_task.borrow().is_some()
    }

    pub fn take_gen_snap_task(&self) -> Option<GenSnapTask> {
        let mut task = self.gen_snap_task.borrow_mut();
        (*task).take()
    }

    pub fn set_gen_snap_task(&self, task: GenSnapTask) {
        let mut gen_snap_task = self.gen_snap_task.borrow_mut();
        assert!(gen_snap_task.is_none(), "{:?}", gen_snap_task);
        *gen_snap_task = Some(task);
    }

    pub fn on_compact_raftlog(&mut self, idx: u64, state: Option<&mut CacheWarmupState>) {
        // Before compacting stale terms in advance to reduce the extra I/O costs on
        // fetching terms.
        self.entry_storage.compact_term_cache(idx);
        self.entry_storage.compact_entry_cache(idx, state);
        self.cancel_generating_snap(Some(idx));
    }

    // Apply the peer with given snapshot.
    pub fn apply_snapshot(
        &mut self,
        snap: &Snapshot,
        task: &mut WriteTask<EK, ER>,
        destroy_regions: &[metapb::Region],
    ) -> Result<(metapb::Region, bool)> {
        info!(
            "begin to apply snapshot";
            "region_id" => self.region.get_id(),
            "peer_id" => self.peer_id,
        );

        let mut snap_data = RaftSnapshotData::default();
        snap_data.merge_from_bytes(snap.get_data())?;

        let for_witness = snap_data.get_meta().get_for_witness();

        let region_id = self.get_region_id();
        let region = snap_data.take_region();
        if region.get_id() != region_id {

Review comment (performance):
Looks like `on_compact_raftlog` is called by `on_raft_gc_log_tick` to free unnecessary entry cache. In this case, `compact_term_cache` should not be compacted, because its memory usage is negligible. 

Code after:
        }
    }

    pub fn get_gen_snap_task(&self) -> Ref<'_, Option<GenSnapTask>> {
        self.gen_snap_task.borrow()
    }

    pub fn has_gen_snap_task(&self) -> bool {
        self.gen_snap_task.borrow().is_some()
    }

    pub fn take_gen_snap_task(&self) -> Option<GenSnapTask> {
        let mut task = self.gen_snap_task.borrow_mut();
        (*task).take()
    }

    pub fn set_gen_snap_task(&self, task: GenSnapTask) {
        let mut gen_snap_task = self.gen_snap_task.borrow_mut();
        assert!(gen_snap_task.is_none(), "{:?}", gen_snap_task);
        *gen_snap_task = Some(task);
    }

    pub fn on_compact_raftlog(&mut self, idx: u64, state: Option<&mut CacheWarmupState>) {
        self.entry_storage.compact_entry_cache(idx, state);
        self.entry_storage.compact_term_cache(idx);
        self.cancel_generating_snap(Some(idx));
    }

    // Apply the peer with given snapshot.
    pub fn apply_snapshot(
        &mut self,
        snap: &Snapshot,
        task: &mut WriteTask<EK, ER>,
        destroy_regions: &[metapb::Region],
    ) -> Result<(metapb::Region, bool)> {
        info!(
            "begin to apply snapshot";
            "region_id" => self.region.get_id(),
            "peer_id" => self.peer_id,
        );

        let mut snap_data = RaftSnapshotData::default();
        snap_data.merge_from_bytes(snap.get_data())?;

        let for_witness = snap_data.get_meta().get_for_witness();

        let region_id = self.get_region_id();
        let region = snap_data.take_region();
        if region.get_id() != region_id {
            return Err(box_err!(
                "mismatch region id {} != {}",