Skip to content

Commit

Permalink
IndexMap: use ahash hasher
Browse files Browse the repository at this point in the history
  • Loading branch information
deedy5 committed Jul 20, 2024
1 parent 47f50db commit 46af214
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 61 deletions.
53 changes: 49 additions & 4 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
111 changes: 56 additions & 55 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -36,8 +37,8 @@ pub struct Client {
client: Arc<reqwest_impersonate::Client>,
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
params: Option<IndexMap<String, String>>,
cookies: Option<IndexMap<String, String>>,
params: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
}

#[pymethods]
Expand Down Expand Up @@ -95,9 +96,9 @@ impl Client {
fn new(
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
params: Option<IndexMap<String, String>>,
headers: Option<IndexMap<String, String>>,
cookies: Option<IndexMap<String, String>>,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
cookie_store: Option<bool>,
referer: Option<bool>,
proxy: Option<&str>,
Expand Down Expand Up @@ -238,9 +239,9 @@ impl Client {
py: Python,
method: &str,
url: &str,
params: Option<IndexMap<String, String>>,
headers: Option<IndexMap<String, String>>,
cookies: Option<IndexMap<String, String>>,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
content: Option<Vec<u8>>,
data: Option<&Bound<'_, PyDict>>,
json: Option<&Bound<'_, PyDict>>,
Expand Down Expand Up @@ -373,11 +374,11 @@ impl Client {
})?;

// Response items
let cookies: IndexMap<String, String> = resp
let cookies: IndexMap<String, String, RandomState> = resp
.cookies()
.map(|cookie| (cookie.name().to_string(), cookie.value().to_string()))
.collect();
let headers: IndexMap<String, String> = resp
let headers: IndexMap<String, String, RandomState> = resp
.headers()
.iter()
.map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or("").to_string()))
Expand Down Expand Up @@ -435,9 +436,9 @@ impl Client {
&self,
py: Python,
url: &str,
params: Option<IndexMap<String, String>>,
headers: Option<IndexMap<String, String>>,
cookies: Option<IndexMap<String, String>>,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
timeout: Option<f64>,
Expand All @@ -464,9 +465,9 @@ impl Client {
&self,
py: Python,
url: &str,
params: Option<IndexMap<String, String>>,
headers: Option<IndexMap<String, String>>,
cookies: Option<IndexMap<String, String>>,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
timeout: Option<f64>,
Expand All @@ -493,9 +494,9 @@ impl Client {
&self,
py: Python,
url: &str,
params: Option<IndexMap<String, String>>,
headers: Option<IndexMap<String, String>>,
cookies: Option<IndexMap<String, String>>,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
timeout: Option<f64>,
Expand All @@ -522,9 +523,9 @@ impl Client {
&self,
py: Python,
url: &str,
params: Option<IndexMap<String, String>>,
headers: Option<IndexMap<String, String>>,
cookies: Option<IndexMap<String, String>>,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
timeout: Option<f64>,
Expand Down Expand Up @@ -552,9 +553,9 @@ impl Client {
&self,
py: Python,
url: &str,
params: Option<IndexMap<String, String>>,
headers: Option<IndexMap<String, String>>,
cookies: Option<IndexMap<String, String>>,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
content: Option<Vec<u8>>,
data: Option<&Bound<'_, PyDict>>,
json: Option<&Bound<'_, PyDict>>,
Expand Down Expand Up @@ -586,9 +587,9 @@ impl Client {
&self,
py: Python,
url: &str,
params: Option<IndexMap<String, String>>,
headers: Option<IndexMap<String, String>>,
cookies: Option<IndexMap<String, String>>,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
content: Option<Vec<u8>>,
data: Option<&Bound<'_, PyDict>>,
json: Option<&Bound<'_, PyDict>>,
Expand Down Expand Up @@ -620,9 +621,9 @@ impl Client {
&self,
py: Python,
url: &str,
params: Option<IndexMap<String, String>>,
headers: Option<IndexMap<String, String>>,
cookies: Option<IndexMap<String, String>>,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
content: Option<Vec<u8>>,
data: Option<&Bound<'_, PyDict>>,
json: Option<&Bound<'_, PyDict>>,
Expand Down Expand Up @@ -657,9 +658,9 @@ fn request(
py: Python,
method: &str,
url: &str,
params: Option<IndexMap<String, String>>,
headers: Option<IndexMap<String, String>>,
cookies: Option<IndexMap<String, String>>,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
content: Option<Vec<u8>>,
data: Option<&Bound<'_, PyDict>>,
json: Option<&Bound<'_, PyDict>>,
Expand Down Expand Up @@ -710,9 +711,9 @@ fn request(
fn get(
py: Python,
url: &str,
params: Option<IndexMap<String, String>>,
headers: Option<IndexMap<String, String>>,
cookies: Option<IndexMap<String, String>>,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
timeout: Option<f64>,
Expand Down Expand Up @@ -754,9 +755,9 @@ fn get(
fn head(
py: Python,
url: &str,
params: Option<IndexMap<String, String>>,
headers: Option<IndexMap<String, String>>,
cookies: Option<IndexMap<String, String>>,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
timeout: Option<f64>,
Expand Down Expand Up @@ -798,9 +799,9 @@ fn head(
fn options(
py: Python,
url: &str,
params: Option<IndexMap<String, String>>,
headers: Option<IndexMap<String, String>>,
cookies: Option<IndexMap<String, String>>,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
timeout: Option<f64>,
Expand Down Expand Up @@ -842,9 +843,9 @@ fn options(
fn delete(
py: Python,
url: &str,
params: Option<IndexMap<String, String>>,
headers: Option<IndexMap<String, String>>,
cookies: Option<IndexMap<String, String>>,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
timeout: Option<f64>,
Expand Down Expand Up @@ -886,9 +887,9 @@ fn delete(
fn post(
py: Python,
url: &str,
params: Option<IndexMap<String, String>>,
headers: Option<IndexMap<String, String>>,
cookies: Option<IndexMap<String, String>>,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
content: Option<Vec<u8>>,
data: Option<&Bound<'_, PyDict>>,
json: Option<&Bound<'_, PyDict>>,
Expand Down Expand Up @@ -938,9 +939,9 @@ fn post(
fn put(
py: Python,
url: &str,
params: Option<IndexMap<String, String>>,
headers: Option<IndexMap<String, String>>,
cookies: Option<IndexMap<String, String>>,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
content: Option<Vec<u8>>,
data: Option<&Bound<'_, PyDict>>,
json: Option<&Bound<'_, PyDict>>,
Expand Down Expand Up @@ -990,9 +991,9 @@ fn put(
fn patch(
py: Python,
url: &str,
params: Option<IndexMap<String, String>>,
headers: Option<IndexMap<String, String>>,
cookies: Option<IndexMap<String, String>>,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
content: Option<Vec<u8>>,
data: Option<&Bound<'_, PyDict>>,
json: Option<&Bound<'_, PyDict>>,
Expand Down
7 changes: 5 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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<String, String>) -> Option<String> {
pub fn get_encoding_from_headers(
headers: &IndexMap<String, String, RandomState>,
) -> Option<String> {
// Extract and decode the Content-Type header
let content_type = headers
.iter()
Expand Down Expand Up @@ -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"),
Expand Down

0 comments on commit 46af214

Please sign in to comment.