From 46af214e9105850aecf509651db2fbc100d5eef3 Mon Sep 17 00:00:00 2001 From: deedy5 <65482418+deedy5@users.noreply.github.com> Date: Sat, 20 Jul 2024 18:36:49 +0300 Subject: [PATCH] IndexMap: use `ahash` hasher --- Cargo.lock | 53 ++++++++++++++++++++++-- Cargo.toml | 1 + src/lib.rs | 111 ++++++++++++++++++++++++++------------------------- src/utils.rs | 7 +++- 4 files changed, 111 insertions(+), 61 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 59d1c91..987cdee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,19 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "ahash" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" +dependencies = [ + "cfg-if", + "getrandom", + "once_cell", + "version_check", + "zerocopy", +] + [[package]] name = "aho-corasick" version = "1.1.3" @@ -426,6 +439,17 @@ dependencies = [ "pin-utils", ] +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "gimli" version = "0.29.0" @@ -640,9 +664,9 @@ checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" [[package]] name = "libloading" -version = "0.8.4" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e310b3a6b5907f99202fcdb4960ff45b93735d7c7d96b760fcff8db2dc0e103d" +checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4" dependencies = [ "cfg-if", "windows-targets 0.52.6", @@ -783,9 +807,9 @@ checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" [[package]] name = "portable-atomic" -version = "1.6.0" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" +checksum = "da544ee218f0d287a911e9c99a39a8c9bc8fcad3cb8db5959940044ecfc67265" [[package]] name = "powerfmt" @@ -886,6 +910,7 @@ dependencies = [ name = "pyreqwest_impersonate" version = "0.5.0" dependencies = [ + "ahash", "encoding_rs", "indexmap", "pyo3", @@ -1548,6 +1573,26 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "zstd" version = "0.13.2" diff --git a/Cargo.toml b/Cargo.toml index ceba63a..ea5b09a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ reqwest-impersonate = { version = "0.11", default-features = false, features = [ "zstd", ] } encoding_rs = { version = "0.8" } +ahash = "0.8" indexmap = { version = "2", features = ["serde"] } tokio = { version = "1", features = ["rt"] } diff --git a/src/lib.rs b/src/lib.rs index 150eefe..934b8dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ use std::str::FromStr; use std::sync::{Arc, OnceLock}; use std::time::Duration; +use ahash::RandomState; use indexmap::IndexMap; use pyo3::exceptions; use pyo3::prelude::*; @@ -36,8 +37,8 @@ pub struct Client { client: Arc, auth: Option<(String, Option)>, auth_bearer: Option, - params: Option>, - cookies: Option>, + params: Option>, + cookies: Option>, } #[pymethods] @@ -95,9 +96,9 @@ impl Client { fn new( auth: Option<(String, Option)>, auth_bearer: Option, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option>, + headers: Option>, + cookies: Option>, cookie_store: Option, referer: Option, proxy: Option<&str>, @@ -238,9 +239,9 @@ impl Client { py: Python, method: &str, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option>, + headers: Option>, + cookies: Option>, content: Option>, data: Option<&Bound<'_, PyDict>>, json: Option<&Bound<'_, PyDict>>, @@ -373,11 +374,11 @@ impl Client { })?; // Response items - let cookies: IndexMap = resp + let cookies: IndexMap = resp .cookies() .map(|cookie| (cookie.name().to_string(), cookie.value().to_string())) .collect(); - let headers: IndexMap = resp + let headers: IndexMap = resp .headers() .iter() .map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or("").to_string())) @@ -435,9 +436,9 @@ impl Client { &self, py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option>, + headers: Option>, + cookies: Option>, auth: Option<(String, Option)>, auth_bearer: Option, timeout: Option, @@ -464,9 +465,9 @@ impl Client { &self, py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option>, + headers: Option>, + cookies: Option>, auth: Option<(String, Option)>, auth_bearer: Option, timeout: Option, @@ -493,9 +494,9 @@ impl Client { &self, py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option>, + headers: Option>, + cookies: Option>, auth: Option<(String, Option)>, auth_bearer: Option, timeout: Option, @@ -522,9 +523,9 @@ impl Client { &self, py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option>, + headers: Option>, + cookies: Option>, auth: Option<(String, Option)>, auth_bearer: Option, timeout: Option, @@ -552,9 +553,9 @@ impl Client { &self, py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option>, + headers: Option>, + cookies: Option>, content: Option>, data: Option<&Bound<'_, PyDict>>, json: Option<&Bound<'_, PyDict>>, @@ -586,9 +587,9 @@ impl Client { &self, py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option>, + headers: Option>, + cookies: Option>, content: Option>, data: Option<&Bound<'_, PyDict>>, json: Option<&Bound<'_, PyDict>>, @@ -620,9 +621,9 @@ impl Client { &self, py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option>, + headers: Option>, + cookies: Option>, content: Option>, data: Option<&Bound<'_, PyDict>>, json: Option<&Bound<'_, PyDict>>, @@ -657,9 +658,9 @@ fn request( py: Python, method: &str, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option>, + headers: Option>, + cookies: Option>, content: Option>, data: Option<&Bound<'_, PyDict>>, json: Option<&Bound<'_, PyDict>>, @@ -710,9 +711,9 @@ fn request( fn get( py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option>, + headers: Option>, + cookies: Option>, auth: Option<(String, Option)>, auth_bearer: Option, timeout: Option, @@ -754,9 +755,9 @@ fn get( fn head( py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option>, + headers: Option>, + cookies: Option>, auth: Option<(String, Option)>, auth_bearer: Option, timeout: Option, @@ -798,9 +799,9 @@ fn head( fn options( py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option>, + headers: Option>, + cookies: Option>, auth: Option<(String, Option)>, auth_bearer: Option, timeout: Option, @@ -842,9 +843,9 @@ fn options( fn delete( py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option>, + headers: Option>, + cookies: Option>, auth: Option<(String, Option)>, auth_bearer: Option, timeout: Option, @@ -886,9 +887,9 @@ fn delete( fn post( py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option>, + headers: Option>, + cookies: Option>, content: Option>, data: Option<&Bound<'_, PyDict>>, json: Option<&Bound<'_, PyDict>>, @@ -938,9 +939,9 @@ fn post( fn put( py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option>, + headers: Option>, + cookies: Option>, content: Option>, data: Option<&Bound<'_, PyDict>>, json: Option<&Bound<'_, PyDict>>, @@ -990,9 +991,9 @@ fn put( fn patch( py: Python, url: &str, - params: Option>, - headers: Option>, - cookies: Option>, + params: Option>, + headers: Option>, + cookies: Option>, content: Option>, data: Option<&Bound<'_, PyDict>>, json: Option<&Bound<'_, PyDict>>, diff --git a/src/utils.rs b/src/utils.rs index db9c3c5..01178e8 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,11 +1,14 @@ use std::cmp::min; +use ahash::RandomState; use indexmap::IndexMap; use pyo3::prelude::*; use pyo3::types::{PyBool, PyDict}; // Get encoding from the "Content-Type" header -pub fn get_encoding_from_headers(headers: &IndexMap) -> Option { +pub fn get_encoding_from_headers( + headers: &IndexMap, +) -> Option { // Extract and decode the Content-Type header let content_type = headers .iter() @@ -85,7 +88,7 @@ mod utils_tests { #[test] fn test_get_encoding_from_headers() { // Test case: Content-Type header with charset specified - let mut headers = IndexMap::new(); + let mut headers = IndexMap::default(); headers.insert( String::from("Content-Type"), String::from("text/html;charset=UTF-8"),