Skip to content

Commit

Permalink
flt2vhs: Experiment with rustc_hash::FxHasher
Browse files Browse the repository at this point in the history
It claims to be consistently faster than FNV:
https://docs.rs/rustc-hash/1.1.0/rustc_hash/struct.FxHasher.html

Benchmark it against a larger FLT
  • Loading branch information
mrkline committed May 29, 2021
1 parent 662e78e commit c864771
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flt2vhs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ edition = "2018"

[dependencies]
anyhow = "1.0"
fnv = "1.0"
humansize = "1.0"
log = "0.4"
logsetup = { path = "../logsetup" }
memmap = "0.7"
rayon = "1.4"
rustc-hash = "1.1"
structopt = "0.3.8"
20 changes: 10 additions & 10 deletions flt2vhs/src/flt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use std::{io, io::prelude::*, path::Path, time::Instant};

use anyhow::*;
use fnv::{FnvHashMap, FnvHashSet};
use log::*;
use rustc_hash::{FxHashMap, FxHashSet};

use crate::primitives::*;

Expand All @@ -30,15 +30,15 @@ pub struct Flight {
/// Map entity & feature UIDs to callsigns (16 byte blocks for strings) and faction colors.
///
/// Use an ordered map to quickly inflate it back to an array on VHS write.
pub callsigns: FnvHashMap<i32, CallsignRecord>,
pub callsigns: FxHashMap<i32, CallsignRecord>,

/// A map of unique IDs for entities (all moving objects in game)
/// to their position updates and events.
pub entities: FnvHashMap<i32, EntityData>,
pub entities: FxHashMap<i32, EntityData>,

/// A map of unique IDs for all features (static objects)
/// and their positions.
pub features: FnvHashMap<i32, FeatureData>,
pub features: FxHashMap<i32, FeatureData>,

/// "General" events not associated with any particular entities
pub general_events: Vec<GeneralEvent>,
Expand Down Expand Up @@ -164,10 +164,10 @@ impl Flight {
fn merge_entities(self: &mut Flight, next_flight: &Flight, unique_id: &mut i32) {
let starting_uid = *unique_id;

let mut used_previous_entities: FnvHashSet<i32> =
FnvHashSet::with_capacity_and_hasher(self.entities.len(), Default::default());
let mut next_to_previous_ids: FnvHashMap<i32, i32> =
FnvHashMap::with_capacity_and_hasher(next_flight.entities.len(), Default::default());
let mut used_previous_entities: FxHashSet<i32> =
FxHashSet::with_capacity_and_hasher(self.entities.len(), Default::default());
let mut next_to_previous_ids: FxHashMap<i32, i32> =
FxHashMap::with_capacity_and_hasher(next_flight.entities.len(), Default::default());

for (next_id, next_entity) in &next_flight.entities {
let mut closest_entity: Option<i32> = None;
Expand Down Expand Up @@ -280,8 +280,8 @@ impl Flight {
fn merge_features(self: &mut Flight, next_flight: &Flight, unique_id: &mut i32) {
let starting_uid = *unique_id;

let mut next_to_previous_ids: FnvHashMap<i32, i32> =
FnvHashMap::with_capacity_and_hasher(next_flight.features.len(), Default::default());
let mut next_to_previous_ids: FxHashMap<i32, i32> =
FxHashMap::with_capacity_and_hasher(next_flight.features.len(), Default::default());

for (next_id, next_feature) in &next_flight.features {
let mut matching_previous = None;
Expand Down
16 changes: 8 additions & 8 deletions flt2vhs/src/vhs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use std::io;
use std::io::prelude::*;

use anyhow::*;
use fnv::FnvHashMap;
use log::*;
use rayon::prelude::*;
use rustc_hash::FxHashMap;

use crate::flt::{self, Flight};
use crate::primitives::*;
Expand Down Expand Up @@ -180,8 +180,8 @@ pub fn write<W: Write>(flight: &Flight, w: W) -> Result<u32> {

// Some of the feature fields refer to the index of other features.
// Let's put those in hash map so we can get constant time lookup.
let mut feature_indexes: FnvHashMap<i32, i32> =
FnvHashMap::with_capacity_and_hasher(flight.features.len(), Default::default());
let mut feature_indexes: FxHashMap<i32, i32> =
FxHashMap::with_capacity_and_hasher(flight.features.len(), Default::default());
for (i, id) in mapping.features.iter().map(|m| m.original).enumerate() {
feature_indexes.insert(id, i as i32);
}
Expand Down Expand Up @@ -406,7 +406,7 @@ fn write_entities<W: Write>(
header: &Header,
w: &mut W,
) -> Result<u32> {
let mut kind_indexes = FnvHashMap::default();
let mut kind_indexes = FxHashMap::default();
let mut position_index = 0;
let mut event_index = 0;

Expand Down Expand Up @@ -459,7 +459,7 @@ fn write_entities<W: Write>(
fn write_features<W: Write>(
flight: &Flight,
feature_mapping: &[IdRemap],
feature_indexes: &FnvHashMap<i32, i32>,
feature_indexes: &FxHashMap<i32, i32>,
feature_position_offset: u32,
header: &Header,
w: &mut W,
Expand Down Expand Up @@ -503,8 +503,8 @@ fn write_entity_positions<W: Write>(
w: &mut CountedWrite<W>,
) -> Result<()> {
// Radar targets need to be converted from UIDs to entity indexes.
let mut entity_indexes: FnvHashMap<i32, i32> =
FnvHashMap::with_capacity_and_hasher(flight.entities.len(), Default::default());
let mut entity_indexes: FxHashMap<i32, i32> =
FxHashMap::with_capacity_and_hasher(flight.entities.len(), Default::default());
for (i, id) in entity_mapping.iter().map(|m| m.original).enumerate() {
entity_indexes.insert(id, i as i32);
}
Expand Down Expand Up @@ -688,7 +688,7 @@ fn write_general_events<W: Write>(

fn write_feature_events<W: Write>(
flight: &Flight,
feature_indexes: &FnvHashMap<i32, i32>,
feature_indexes: &FxHashMap<i32, i32>,
w: &mut CountedWrite<W>,
) -> Result<()> {
for event in &flight.feature_events {
Expand Down

0 comments on commit c864771

Please sign in to comment.