Repository: tikv/tikv
File: components/raftstore-v2/src/operation/command/admin/compact_log.rs (Rust)

Code before:
            debug!(
                self.logger,
                "compact index <= first index, no need to compact";
                "compact_index" => res.compact_index,
                "first_index" => first_index,
            );
            return;
        }
        assert!(
            res.compact_index < self.compact_log_context().last_applying_index,
            "{}: {}, {}",
            SlogFormat(&self.logger),
            res.compact_index,
            self.compact_log_context().last_applying_index
        );

        // Since this peer may be warming up the entry cache, log compaction should be
        // temporarily skipped. Otherwise, the warmup task may fail.
        if let Some(state) = &mut self.transfer_leader_state_mut().cache_warmup_state {
            if !state.check_stale() {
                return;
            }
        }

        // No need to compact term cache since each cached term is consistent with
        // persisted terms.
        let mut cache_warmup_state = self.transfer_leader_state_mut().cache_warmup_state.take();
        self.entry_storage_mut()
            .compact_entry_cache(res.compact_index, cache_warmup_state.as_mut());
        self.transfer_leader_state_mut().cache_warmup_state = cache_warmup_state;

        self.storage_mut()
            .cancel_generating_snap_due_to_compacted(res.compact_index);

        let truncated_state = self
            .entry_storage_mut()
            .apply_state_mut()
            .mut_truncated_state();
        truncated_state.set_index(res.compact_index);
        truncated_state.set_term(res.compact_term);

        let region_id = self.region_id();
        // TODO: get around this clone.
        let apply_state = self.entry_storage().apply_state().clone();
        self.state_changes_mut()
            .put_apply_state(region_id, res.index, &apply_state)
            .unwrap();
        self.set_has_extra_write();

        // All logs < persisted_apply will be deleted.
        let prev_first_index = first_index;

Review comment (performance):
We actually need to compact the term cache because truncated index is updated and raft logs in raft_engine is removed here.

Code after:
                self.logger,
                "compact index <= first index, no need to compact";
                "compact_index" => res.compact_index,
                "first_index" => first_index,
            );
            return;
        }
        assert!(
            res.compact_index < self.compact_log_context().last_applying_index,
            "{}: {}, {}",
            SlogFormat(&self.logger),
            res.compact_index,
            self.compact_log_context().last_applying_index
        );

        // Since this peer may be warming up the entry cache, log compaction should be
        // temporarily skipped. Otherwise, the warmup task may fail.
        if let Some(state) = &mut self.transfer_leader_state_mut().cache_warmup_state {
            if !state.check_stale() {
                return;
            }
        }

        let mut cache_warmup_state = self.transfer_leader_state_mut().cache_warmup_state.take();
        self.entry_storage_mut()
            .compact_entry_cache(res.compact_index, cache_warmup_state.as_mut());
        self.entry_storage_mut()
            .compact_term_cache(res.compact_index);
        self.transfer_leader_state_mut().cache_warmup_state = cache_warmup_state;

        self.storage_mut()
            .cancel_generating_snap_due_to_compacted(res.compact_index);

        let truncated_state = self
            .entry_storage_mut()
            .apply_state_mut()
            .mut_truncated_state();
        truncated_state.set_index(res.compact_index);
        truncated_state.set_term(res.compact_term);

        let region_id = self.region_id();
        // TODO: get around this clone.
        let apply_state = self.entry_storage().apply_state().clone();
        self.state_changes_mut()
            .put_apply_state(region_id, res.index, &apply_state)
            .unwrap();
        self.set_has_extra_write();

        // All logs < persisted_apply will be deleted.
        let prev_first_index = first_index;
        if prev_first_index < self.storage().apply_trace().persisted_apply_index()