Implemented Phase 1 optimizations focusing on vector and HashMap pre-allocation to reduce reallocation overhead during serialization and deserialization.
extract_terms() FunctionLocation: src/serialization/mod.rs:140
Changes:
terms vector with dictionary size estimatecurrent_term buffer with 32-byte capacityCode:
let est_size = dict.len().unwrap_or(100);
let mut terms = Vec::with_capacity(est_size);
let mut current_term = Vec::with_capacity(32); // Most words < 32 bytes
Expected Impact: Reduce allocation overhead by 40-50% for term extraction.
extract_graph)Location: src/serialization/mod.rs:258
Changes:
node_ids vector (2x dictionary size estimate)final_node_ids vector (dictionary size estimate)edges vector (3x dictionary size estimate)Code:
let est_size = dict.len().unwrap_or(100);
let mut node_ids = Vec::with_capacity(est_size * 2);
let mut final_node_ids = Vec::with_capacity(est_size);
let mut edges = Vec::with_capacity(est_size * 3);
Expected Impact: Eliminate 60-80% of vector reallocations during graph extraction.
Location: src/serialization/mod.rs:351
Changes:
Code:
let est_nodes = proto_dict.node_id.len();
let mut adjacency: HashMap<u64, Vec<(u8, u64)>> = HashMap::with_capacity(est_nodes);
let mut final_set: std::collections::HashSet<u64> =
std::collections::HashSet::with_capacity(proto_dict.final_node_id.len());
final_set.extend(proto_dict.final_node_id.iter().copied());
let est_terms = proto_dict.final_node_id.len();
let mut terms = Vec::with_capacity(est_terms);
let mut current_term = Vec::with_capacity(32);
Expected Impact: Reduce hash map rehashing overhead by 70-80%, reduce allocation overhead by 40%.
extract_graph_v2)Location: src/serialization/mod.rs:428
Changes:
final_node_ids vectoredge_data vector (9x dictionary size estimate for packed format)Code:
let est_size = dict.len().unwrap_or(100);
let mut final_node_ids = Vec::with_capacity(est_size);
let mut edge_data = Vec::with_capacity(est_size * 9); // 3 values/edge, ~3 edges/term
Expected Impact: Eliminate 60-80% of vector reallocations.
Location: src/serialization/mod.rs:537
Changes:
final_node_ids with delta countCode:
let mut final_node_ids = Vec::with_capacity(proto_dict.final_node_delta.len());
let num_edges = proto_dict.edge_data.len() / 3;
let est_nodes = (num_edges as f64 * 0.6) as usize; // Estimate from edges
let mut adjacency: HashMap<u64, Vec<(u8, u64)>> = HashMap::with_capacity(est_nodes);
let mut final_set: std::collections::HashSet<u64> =
std::collections::HashSet::with_capacity(final_node_ids.len());
final_set.extend(final_node_ids.iter().copied());
let est_terms = final_node_ids.len();
let mut terms = Vec::with_capacity(est_terms);
let mut current_term = Vec::with_capacity(32);
Expected Impact: Reduce allocation and rehashing overhead by 40-60%.
Based on the optimizations:
| Operation | Expected Improvement |
|---|---|
| Bincode Serialize | 15-25% faster |
| Bincode Deserialize | 20-30% faster |
| JSON Serialize | 15-25% faster |
| JSON Deserialize | 20-30% faster |
| Protobuf V1 Serialize | 25-35% faster |
| Protobuf V1 Deserialize | 30-40% faster |
| Protobuf V2 Serialize | 25-35% faster |
| Protobuf V2 Deserialize | 30-40% faster |
All tests pass with optimizations:
cargo test --features protobuf serialization
# Result: 11 serialization tests passing
src/serialization/mod.rs - All optimization changesbenches/serialization_benchmarks.rs - New benchmark suite (created)Cargo.toml - Added serialization_benchmarks bench targetdocs/SERIALIZATION_OPTIMIZATION_PLAN.md - Optimization plan (created)Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |