Repository: vercel/next.js
File: turbopack/crates/turbo-tasks-backend/src/backend/mod.rs (Rust)

Code before:
                    encode_task_data_into(task_id, &d, SpecificTaskDataCategory::Data, buffer)
                }),
            )
        };
        let process_snapshot = |task_id: TaskId,
                                inner: Box<TaskStorage>,
                                buffer: &mut TurboBincodeBuffer| {
            if task_id.is_transient() {
                return (task_id, None, None);
            }

            // Encode meta/data directly from TaskStorage snapshot
            (
                task_id,
                inner.flags.meta_modified().then(|| {
                    encode_task_data_into(task_id, &inner, SpecificTaskDataCategory::Meta, buffer)
                }),
                inner.flags.data_modified().then(|| {
                    encode_task_data_into(task_id, &inner, SpecificTaskDataCategory::Data, buffer)
                }),
            )
        };

        /// How big of a buffer to allocate initially.  Based on metrics from a large application
        /// this should cover about 98% of values with no resizes
        const SCRATCH_BUFFER_SIZE: usize = 4096;

        let snapshot = self.storage.take_snapshot(
            &preprocess,
            &process,
            &process_snapshot,
            SCRATCH_BUFFER_SIZE,
        );

        #[cfg(feature = "print_cache_item_size")]
        #[derive(Default)]
        struct TaskCacheStats {
            data: usize,
            data_compressed: usize,
            data_count: usize,
            meta: usize,
            meta_compressed: usize,
            meta_count: usize,
        }
        #[cfg(feature = "print_cache_item_size")]
        impl TaskCacheStats {
            fn compressed_size(data: &[u8]) -> Result<usize> {
                Ok(lzzzz::lz4::Compressor::new()?.next_to_vec(
                    data,
                    &mut Vec::new(),
                    lzzzz::lz4::ACC_LEVEL_DEFAULT,

Review comment (suggestion):
Should this constant live inside of `take_snapshot`? I don't know why anybody would want to call `take_snapshot` with a custom value.

Code after:
        let process_snapshot =
            |task_id: TaskId, inner: Box<TaskStorage>, buffer: &mut TurboBincodeBuffer| {
                if task_id.is_transient() {
                    return (task_id, None, None);
                }

                // Encode meta/data directly from TaskStorage snapshot
                (
                    task_id,
                    inner.flags.meta_modified().then(|| {
                        encode_task_data(task_id, &inner, SpecificTaskDataCategory::Meta, buffer)
                    }),
                    inner.flags.data_modified().then(|| {
                        encode_task_data(task_id, &inner, SpecificTaskDataCategory::Data, buffer)
                    }),
                )
            };

        let snapshot = self
            .storage
            .take_snapshot(&preprocess, &process, &process_snapshot);

        #[cfg(feature = "print_cache_item_size")]
        #[derive(Default)]
        struct TaskCacheStats {
            data: usize,
            data_compressed: usize,
            data_count: usize,
            meta: usize,
            meta_compressed: usize,
            meta_count: usize,
        }
        #[cfg(feature = "print_cache_item_size")]
        impl TaskCacheStats {
            fn compressed_size(data: &[u8]) -> Result<usize> {
                Ok(lzzzz::lz4::Compressor::new()?.next_to_vec(
                    data,
                    &mut Vec::new(),
                    lzzzz::lz4::ACC_LEVEL_DEFAULT,
                )?)
            }

            fn add_data(&mut self, data: &[u8]) {
                self.data += data.len();
                self.data_compressed += Self::compressed_size(data).unwrap_or(0);
                self.data_count += 1;
            }

            fn add_meta(&mut self, data: &[u8]) {
                self.meta += data.len();
                self.meta_compressed += Self::compressed_size(data).unwrap_or(0);