Repository: tikv/tikv
File: components/engine_rocks/src/misc.rs (Rust)

Code before:
                    sst_path,
                    ranges,
                    allow_write_during_ingestion,
                )?;
            }
        }
        Ok(written)
    }

    fn get_approximate_memtable_stats_cf(&self, cf: &str, range: &Range<'_>) -> Result<(u64, u64)> {
        let range = util::range_to_rocks_range(range);
        let handle = util::get_cf_handle(self.as_inner(), cf)?;
        Ok(self
            .as_inner()
            .get_approximate_memtable_stats_cf(handle, &range))
    }

    fn ingest_maybe_slowdown_writes(&self, cf: &str, sst_cnt: u64) -> Result<bool> {
        let handle = util::get_cf_handle(self.as_inner(), cf)?;
        if let Some(n) = util::get_cf_num_files_at_level(self.as_inner(), handle, 0) {
            let options = self.as_inner().get_options_cf(handle);
            let slowdown_trigger = options.get_level_zero_slowdown_writes_trigger();
            let compaction_trigger = options.get_level_zero_file_num_compaction_trigger() as u64;
            // Leave enough buffer to tolerate heavy write workload,
            // which may flush some memtables in a short time.
            if n + sst_cnt - 1 > u64::from(slowdown_trigger) / 2 && n >= compaction_trigger {
                return Ok(true);
            }
        }
        Ok(false)
    }

    fn get_sst_key_ranges(&self, cf: &str, level: usize) -> Result<Vec<(Vec<u8>, Vec<u8>)>> {
        let handle = util::get_cf_handle(self.as_inner(), cf)?;
        let ret = self
            .as_inner()
            .get_column_family_meta_data(handle)
            .get_level(level)
            .get_files()
            .iter()
            .map(|sst_meta| {
                (
                    sst_meta.get_smallestkey().to_vec(),
                    sst_meta.get_largestkey().to_vec(),
                )
            })
            .collect();
        Ok(ret)
    }

    fn get_engine_used_size(&self) -> Result<u64> {

Review comment (performance):
Can you explain why `sst_cnt` should be considered when comparing to `slowdown_trigger`, but not `compaction_trigger`?

Code after:
                allow_write_during_ingestion,
            } => {
                written |= self.delete_all_in_range_cf_by_ingest(
                    wopts,
                    cf,
                    sst_path,
                    ranges,
                    allow_write_during_ingestion,
                )?;
            }
        }
        Ok(written)
    }

    fn get_approximate_memtable_stats_cf(&self, cf: &str, range: &Range<'_>) -> Result<(u64, u64)> {
        let range = util::range_to_rocks_range(range);
        let handle = util::get_cf_handle(self.as_inner(), cf)?;
        Ok(self
            .as_inner()
            .get_approximate_memtable_stats_cf(handle, &range))
    }

    // Checks if ingesting additional SSTs might trigger write slowdown, based
    // on a conservative estimate of the L0 file count after ingesting the SSTs
    // that are already inflight for ingestion.
    fn ingest_maybe_slowdown_writes(&self, cf: &str, inflight_ingest_cnt: u64) -> Result<bool> {
        let handle = util::get_cf_handle(self.as_inner(), cf)?;
        if let Some(n) = util::get_cf_num_files_at_level(self.as_inner(), handle, 0) {
            let options = self.as_inner().get_options_cf(handle);
            let slowdown_trigger = options.get_level_zero_slowdown_writes_trigger();
            let compaction_trigger = options.get_level_zero_file_num_compaction_trigger() as u64;
            // Leave enough buffer to tolerate heavy write workload,
            // which may flush some memtables in a short time.
            let worse_case_l0_file_count = n + inflight_ingest_cnt;
            if worse_case_l0_file_count > u64::from(slowdown_trigger) / 2
                && worse_case_l0_file_count >= compaction_trigger
            {
                return Ok(true);
            }
        }
        Ok(false)
    }

    fn get_engine_used_size(&self) -> Result<u64> {
        let mut used_size: u64 = 0;
        for cf in self.cf_names() {
            let handle = util::get_cf_handle(self.as_inner(), cf)?;
            used_size += util::get_engine_cf_used_size(self.as_inner(), handle);
        }
        Ok(used_size)
    }