Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(hash): Use new hash map #66

Merged
merged 5 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uniswap-sdk-core"
version = "0.23.0"
version = "0.24.0"
edition = "2021"
authors = ["malik <[email protected]>", "Shuhui Luo <twitter.com/aureliano_law>"]
description = "The Uniswap SDK Core in Rust provides essential functionality for interacting with the Uniswap decentralized exchange"
Expand All @@ -15,6 +15,7 @@ num-bigint = "0.4.4"
num-integer = "0.1.45"
num-traits = "0.2.17"
regex = { version = "1.10", optional = true }
rustc-hash = "2.0.0"
thiserror = "1.0"

[features]
Expand Down
42 changes: 21 additions & 21 deletions src/addresses.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::prelude::*;

type AddressMap = HashMap<u64, Address>;
type ChainMap = HashMap<u64, ChainAddresses>;
type ChainAddress = HashMap<u64, Address>;
type AddressMap = FxHashMap<u64, Address>;
type ChainMap = FxHashMap<u64, ChainAddresses>;
type ChainAddress = FxHashMap<u64, Address>;

/// Represents the addresses of various core contracts of uniswap on a network.
#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -32,7 +32,7 @@ pub const DEFAULT_NETWORKS: [ChainId; 3] = [ChainId::MAINNET, ChainId::GOERLI, C
pub fn construct_same_address_map(address: Address, additional_networks: &[ChainId]) -> AddressMap {
let mut networks = DEFAULT_NETWORKS.to_vec();
networks.extend_from_slice(additional_networks);
let mut map = AddressMap::new();
let mut map = AddressMap::default();
for chain_id in networks {
map.insert(chain_id as u64, address);
}
Expand Down Expand Up @@ -63,8 +63,8 @@ pub const V2_FACTORY_ADDRESS: Address = address!("5C69bEe701ef814a2B6a3EDD4B1652

lazy_static! {
/// A map of Uniswap V2 Factory addresses for various networks.
pub static ref V2_FACTORY_ADDRESSES: HashMap<u64, Address> = {
let mut m = HashMap::new();
pub static ref V2_FACTORY_ADDRESSES: FxHashMap<u64, Address> = {
let mut m = FxHashMap::default();
m.insert(ChainId::MAINNET as u64, V2_FACTORY_ADDRESS);
m.insert(ChainId::GOERLI as u64, V2_FACTORY_ADDRESS);
m.insert(
Expand Down Expand Up @@ -116,8 +116,8 @@ lazy_static! {
/// This map is used to look up the address of the Uniswap V2 Router contract
/// for a given network. The keys in the map are the network IDs, and the values
/// are the corresponding contract addresses.
pub static ref V2_ROUTER_ADDRESSES: HashMap<u64, Address> = {
let mut m = HashMap::new();
pub static ref V2_ROUTER_ADDRESSES: FxHashMap<u64, Address> = {
let mut m = FxHashMap::default();
m.insert(ChainId::MAINNET as u64, V2_ROUTER_ADDRESS);
m.insert(ChainId::GOERLI as u64, V2_ROUTER_ADDRESS);
m.insert(
Expand Down Expand Up @@ -417,7 +417,7 @@ lazy_static! {
/// for a given network. The keys in the map are the network IDs, and the values
/// are the corresponding contract addresses.
pub static ref CHAIN_TO_ADDRESSES_MAP: ChainMap = {
let mut new_map = ChainMap::new();
let mut new_map = ChainMap::default();
new_map.insert(ChainId::BNB as u64, BNB_ADDRESSES);

new_map.insert(ChainId::AVALANCHE as u64, AVALANCHE_ADDRESSES);
Expand Down Expand Up @@ -448,7 +448,7 @@ lazy_static! {
lazy_static! {
/// V3 Contract Addresses
pub static ref V3_CORE_FACTORY_ADDRESSES: ChainAddress = {
let mut chain_add = ChainAddress::new();
let mut chain_add = ChainAddress::default();
for chain_id in SUPPORTED_CHAINS {
chain_add.insert(
chain_id as u64,
Expand All @@ -465,7 +465,7 @@ lazy_static! {
lazy_static! {
/// V3 Contract Addresses
pub static ref V3_MIGRATOR_ADDRESSES: ChainAddress = {
let mut chain_add = ChainAddress::new();
let mut chain_add = ChainAddress::default();
for chain_id in SUPPORTED_CHAINS {
chain_add.insert(
chain_id as u64,
Expand All @@ -483,7 +483,7 @@ lazy_static! {
lazy_static! {
/// V3 Contract Addresses
pub static ref MULTICALL_ADDRESSES: ChainAddress = {
let mut chain_add = ChainAddress::new();
let mut chain_add = ChainAddress::default();
for chain_id in SUPPORTED_CHAINS {
chain_add.insert(
chain_id as u64,
Expand All @@ -507,7 +507,7 @@ pub static ref GOVERNANCE_ALPHA_V0_ADDRESSES: AddressMap = {
lazy_static! {
/// The older V1 governance address
pub static ref GOVERNANCE_ALPHA_V1_ADDRESSES: AddressMap = {
let mut new_map = AddressMap::new();
let mut new_map = AddressMap::default();
new_map.insert(
ChainId::MAINNET as u64,
address!("C4e172459f1E7939D522503B81AFAaC1014CE6F6"),
Expand All @@ -519,7 +519,7 @@ pub static ref GOVERNANCE_ALPHA_V1_ADDRESSES: AddressMap = {
lazy_static! {
/// The latest governor bravo that is currently admin of timelock
pub static ref GOVERNANCE_BRAVO_ADDRESSES: AddressMap = {
let mut new_map = AddressMap::new();
let mut new_map = AddressMap::default();
new_map.insert(
ChainId::MAINNET as u64,
address!("408ED6354d4973f66138C91495F2f2FCbd8724C3"),
Expand All @@ -537,7 +537,7 @@ lazy_static! {
lazy_static! {
/// The `MERKLE_DISTRIBUTOR_ADDRESS` struct holds the merkle distributor contract address for the mainnet.
pub static ref MERKLE_DISTRIBUTOR_ADDRESS: AddressMap = {
let mut new_map = AddressMap::new();
let mut new_map = AddressMap::default();
new_map.insert(
ChainId::MAINNET as u64,
address!("090D4613473dEE047c3f2706764f49E0821D256e"),
Expand All @@ -549,7 +549,7 @@ lazy_static! {
lazy_static! {
/// The `ARGENT_WALLET_DETECTOR_ADDRESS` struct holds the Argent Wallet Detector contract address for the mainnet.
pub static ref ARGENT_WALLET_DETECTOR_ADDRESS: AddressMap = {
let mut new_map = AddressMap::new();
let mut new_map = AddressMap::default();
new_map.insert(
ChainId::MAINNET as u64,
address!("eca4B0bDBf7c55E9b7925919d03CbF8Dc82537E8"),
Expand All @@ -564,7 +564,7 @@ lazy_static! {
/// This includes the addresses for the quoter contract on different networks.
/// Each field in the struct corresponds to a specific contract and its address on the network
pub static ref QUOTER_ADDRESSES: ChainAddress = {
let mut chain_add = ChainAddress::new();
let mut chain_add = ChainAddress::default();
for chain_id in SUPPORTED_CHAINS {
chain_add.insert(
chain_id as u64,
Expand All @@ -584,7 +584,7 @@ lazy_static! {
/// This includes the addresses for the non-fungible position manager contract on different networks.
/// Each field in the struct corresponds to a specific contract and its address on the network.
pub static ref NONFUNGIBLE_POSITION_MANAGER_ADDRESSES: ChainAddress = {
let mut chain_add = ChainAddress::new();
let mut chain_add = ChainAddress::default();
for chain_id in SUPPORTED_CHAINS {
if CHAIN_TO_ADDRESSES_MAP
.get(&(chain_id as u64))
Expand Down Expand Up @@ -621,7 +621,7 @@ lazy_static! {
/// This includes the addresses for the SOCKS Controller contract on different networks.
/// Each field in the struct corresponds to a specific contract and its address on the network.
pub static ref SOCKS_CONTROLLER_ADDRESSES: AddressMap = {
let mut new_map = AddressMap::new();
let mut new_map = AddressMap::default();
new_map.insert(
ChainId::MAINNET as u64,
address!("65770b5283117639760beA3F867b69b3697a91dd"),
Expand All @@ -636,7 +636,7 @@ lazy_static! {
/// This includes the addresses for the tick lens contract on different networks.
/// Each field in the struct corresponds to a specific contract and its address on the network.
pub static ref TICK_LENS_ADDRESSES: ChainAddress = {
let mut chain_add = ChainAddress::new();
let mut chain_add = ChainAddress::default();
for chain_id in SUPPORTED_CHAINS {
if CHAIN_TO_ADDRESSES_MAP
.get(&(chain_id as u64))
Expand Down Expand Up @@ -664,7 +664,7 @@ lazy_static! {
/// This includes the addresses for the mixed route quoter contract on different networks.
/// Each field in the struct corresponds to a specific contract and its address on the network.
pub static ref MIXED_ROUTE_QUOTER_V1_ADDRESSES: ChainAddress = {
let mut chain_add = ChainAddress::new();
let mut chain_add = ChainAddress::default();
for chain_id in SUPPORTED_CHAINS {
if CHAIN_TO_ADDRESSES_MAP
.get(&(chain_id as u64))
Expand Down
2 changes: 1 addition & 1 deletion src/entities/ether.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::prelude::*;

// Lazy static cache for Ether instances
lazy_static! {
static ref ETHER_CACHE: Mutex<HashMap<u64, Ether>> = Mutex::new(HashMap::default());
static ref ETHER_CACHE: Mutex<FxHashMap<u64, Ether>> = Mutex::new(FxHashMap::default());
}

/// Ether is the main usage of a 'native' currency, i.e., for Ethereum mainnet and all testnets.
Expand Down
4 changes: 2 additions & 2 deletions src/entities/weth9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{prelude::*, token};
#[derive(Clone, PartialEq, Debug)]
pub struct WETH9 {
/// A mapping of chain IDs to corresponding WETH tokens.
tokens: HashMap<u64, Token>,
tokens: FxHashMap<u64, Token>,
}

/// Default implementation for [`WETH9`], creating an instance with predefined WETH tokens on
Expand All @@ -27,7 +27,7 @@ impl WETH9 {
///
/// A new `WETH9` instance with predefined WETH tokens.
pub fn new() -> Self {
let mut tokens = HashMap::new();
let mut tokens = FxHashMap::default();

// Insert predefined WETH tokens for different chains.
tokens.insert(
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#![warn(
missing_copy_implementations,
missing_debug_implementations,
missing_docs,
unreachable_pub,
clippy::missing_const_for_fn,
rustdoc::all
Expand Down
5 changes: 2 additions & 3 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub use lazy_static::lazy_static;
pub use num_bigint::{BigInt, BigUint, ToBigInt, ToBigUint};
pub use num_integer::Integer;
pub use num_traits::{Num, ToPrimitive, Zero};
pub use std::{
cmp::Ordering, collections::HashMap, num::NonZeroU64, ops::Div, str::FromStr, sync::Mutex,
};
pub use rustc_hash::FxHashMap;
pub use std::{cmp::Ordering, num::NonZeroU64, ops::Div, str::FromStr, sync::Mutex};
pub use thiserror::Error;
2 changes: 0 additions & 2 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

pub mod compute_price_impact;


pub mod sorted_insert;

pub mod sqrt;
Expand Down