Skip to content

Unify CRC Implementation - Replace with crc-fast #872

@houseme

Description

@houseme

Unify CRC Implementation - Replace with crc-fast

Problem Description

The project currently uses multiple different CRC calculation libraries:

  • crc-fast = "1.6.0"
  • crc32c = "0.6.8"
  • crc32fast = "1.5.0"
  • crc64fast-nvme = "1.2.1"

This multi-library approach causes:

  1. Code redundancy - Multiple implementations of the same functionality
  2. Maintenance complexity - Need to familiarize with multiple library APIs
  3. Performance inconsistency - Different libraries may have performance variations
  4. Dependency bloat - Increases final binary size

Goal

Migrate all CRC calculations to use the crc-fast library exclusively and remove dependencies on other CRC libraries.

Migration Requirements

1. Dependency Management

# Remove
crc32c = "0.6.8"
crc32fast = "1.5.0"
crc64fast-nvme = "1.2.1"

# Keep and potentially upgrade
crc-fast = "1.6.0"  # or consider upgrading to latest version

2. API Migration Guide

From crc32c

// Original code
use crc32c::crc32c;
let checksum = crc32c(&data);

// New code
use crc_fast::Crc;
let crc = Crc::<u32>::new(&crc_fast::CRC_32_ISCSI); // CRC-32C (ISCSI)
let checksum = crc.calculate(&data);

From crc32fast

// Original code
use crc32fast::Hasher;
let mut hasher = Hasher::new();
hasher.update(&data);
let checksum = hasher.finalize();

// New code
use crc_fast::Crc;
let crc = Crc::<u32>::new(&crc_fast::CRC_32_ISO_HDLC);
let checksum = crc.calculate(&data);

From crc64fast-nvme

// Original code
use crc64fast_nvme::Crc64;
let checksum = Crc64::new().checksum(&data);

// New code
use crc_fast::Crc;
let crc = Crc::<u64>::new(&crc_fast::CRC_64_ECMA_182);
let checksum = crc.calculate(&data);

Implementation Steps

Step 1: Analyze Existing Code Usage

  • Use grep to analyze all CRC library usage locations
  • Document CRC algorithm types for each usage scenario
  • Confirm migration feasibility

Step 2: Create Unified CRC Adapter Layer

  • Create unified interface in src/crc/mod.rs
  • Implement all required CRC algorithm variants
  • Maintain compatibility with original APIs

Step 3: Gradually Replace Calling Code

  • First replace crc32c usage
  • Then replace crc32fast usage
  • Finally replace crc64fast-nvme usage
  • Run tests to verify after each replacement

Step 4: Clean Up Dependencies and Code

  • Remove unnecessary dependencies from Cargo.toml
  • Remove adapter layer (if no longer needed)
  • Run full test suite

Step 5: Performance Verification and Optimization

  • Compare performance before and after migration
  • Enable crc-fast SIMD features
  • Conduct stress testing

Acceptance Criteria

  • All CRC calculation functions work correctly
  • Performance is not worse than original implementation
  • All test cases pass
  • Code review approved
  • Dependency cleanup completed

Risk Control

  1. Rollback plan: Keep git history of original code for easy rollback if needed
  2. Phased implementation: Replace gradually to avoid large-scale changes at once
  3. Performance testing: Compare performance before and after migration
  4. Compatibility verification: Ensure CRC results match in all usage scenarios

Metadata

Metadata

Labels

dependenciesPull requests that update a dependency fileenhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions