1.0.0 ~ 1.2.x2.0.0 ~ 2.2.x本文聚焦使用层面差异与迁移建议,不展开内部重构细节。
2.0.0 的分片安全构造策略升级,2.1+ 新增可选重平衡 API,2.2+ 新增可选快照模式。| 维度 | 1.x | 2.x |
|---|---|---|
| 分片数量安全 | 超大 shard_count 可能带来内存风险 |
默认 MAX_SHARDS 保护,并提供严格构造器 |
| 构造器策略 | 以直接构造为主 | 兼容构造 + capped 构造 + strict 构造 |
| 动态重平衡 | 不支持 | rebalance_to、start_rebalance_online、advance_rebalance |
| 快照策略 | 传统路径 | SnapshotMode::{Clone, Cached, Cow} |
| 升级改造量 | - | 默认使用场景改造量低,可渐进启用新能力 |
2.0.0)2.x 的实质变化:
with_shards_and_hasher_capped(...)ShardCountError):
try_with_shards_and_hasher(...)try_with_shards_and_hasher_capped(...)建议在“外部输入/用户配置”场景使用 strict:
use starshard::ShardedHashMap;
use rustc_hash::FxBuildHasher;
let map = ShardedHashMap::<String, i32>::try_with_shards_and_hasher_capped(
4096,
FxBuildHasher,
8192,
)?;
迁移提示:
2.1+)1.x 不支持动态重平衡。
2.1+ 新增:
rebalance_to(new_shard_count, options)start_rebalance_online(new_shard_count)advance_rebalance(batch_shards)rebalance_status()use starshard::{RebalanceOptions, ShardedHashMap};
let m: ShardedHashMap<String, i32> = ShardedHashMap::new(8);
m.rebalance_to(32, RebalanceOptions::default())?;
2.2+)2.2+ 将快照策略显式化:
SnapshotMode::Clone(默认)SnapshotMode::CachedSnapshotMode::Cow并新增模式构造器:
with_snapshot_mode(...)with_shards_and_hasher_and_snapshot_mode(...)with_shards_and_hasher_capped_and_snapshot_mode(...)use starshard::{ShardedHashMap, SnapshotMode};
let map: ShardedHashMap<String, i32> =
ShardedHashMap::with_snapshot_mode(64, SnapshotMode::Cached);
batch_insert / batch_remove / batch_get 在 2.0 做了分组路径优化。compute_if_present 路径在 2.0 做了行为对齐,减少不必要 remove+reinsert。这些变化通常对调用方透明。
从早期 1.x 升级时,建议重新确认 Cargo.toml 的 feature 组合:
[dependencies]
starshard = { version = "2.2.0", features = ["async", "rayon", "serde", "lifecycle", "advanced"] }
2.2.x)。try_with_*SnapshotMode::Cached / Cow。cargo test --all-features