From 2dd325ca8a86a762ccc0619303d5af936d38ba5d Mon Sep 17 00:00:00 2001 From: ZhuchkaTriplesix Date: Sun, 18 Oct 2026 17:19:38 +0300 Subject: [PATCH] perf(response): replace protocol.getattr with call_method1 to avoid PyMethodObject allocations --- src/response.rs | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/response.rs b/src/response.rs index a834928..6e80da8 100644 --- a/src/response.rs +++ b/src/response.rs @@ -76,7 +76,7 @@ pub async fn send_with_headers( Python::with_gil(|py| { let p = protocol.bind(py); let h = build_header_list_from_pairs(py, &headers)?; - p.getattr("response_bytes")?.call1((status, h, body))?; + p.call_method1("response_bytes", (status, h, body))?; Ok(Py::from(pyo3::types::PyNone::get(py)).into_any()) }) } @@ -89,7 +89,7 @@ async fn send_empty_with_header_pairs( Python::with_gil(|py| { let p = protocol.bind(py); let h = build_header_list_from_pairs(py, &headers)?; - p.getattr("response_empty")?.call1((status, h))?; + p.call_method1("response_empty", (status, h))?; Ok(Py::from(pyo3::types::PyNone::get(py)).into_any()) }) } @@ -196,7 +196,7 @@ pub fn send_str_sync( ) -> PyResult<()> { let p = protocol.bind(py); let h = build_headers_ct(py, Some(content_type))?; - p.getattr("response_str")?.call1((status, h, text))?; + p.call_method1("response_str", (status, h, text))?; Ok(()) } @@ -227,7 +227,7 @@ pub fn send_pybytes_sync( } let p = protocol.bind(py); let h = build_headers_ct(py, Some(content_type))?; - p.getattr("response_bytes")?.call1((status, h, body))?; + p.call_method1("response_bytes", (status, h, body))?; Ok(()) } @@ -244,7 +244,7 @@ pub fn send_bytes_sync( } let p = protocol.bind(py); let h = build_headers_ct(py, Some(content_type))?; - p.getattr("response_bytes")?.call1((status, h, body))?; + p.call_method1("response_bytes", (status, h, body))?; Ok(()) } @@ -257,7 +257,7 @@ pub fn send_empty_sync( ) -> PyResult<()> { let p = protocol.bind(py); let h = build_headers_ct(py, content_type)?; - p.getattr("response_empty")?.call1((status, h))?; + p.call_method1("response_empty", (status, h))?; Ok(()) } @@ -272,9 +272,9 @@ pub fn send_with_headers_sync( let p = protocol.bind(py); let h = build_header_list_from_pairs(py, &headers)?; if body.is_empty() { - p.getattr("response_empty")?.call1((status, h))?; + p.call_method1("response_empty", (status, h))?; } else { - p.getattr("response_bytes")?.call1((status, h, body))?; + p.call_method1("response_bytes", (status, h, body))?; } Ok(()) } @@ -293,8 +293,10 @@ pub fn send_405_method_not_allowed_sync( let pair1 = PyTuple::new(py, [allow_key.as_any(), allow_val.as_any()])?; let pair2 = PyTuple::new(py, [ct_key.as_any(), ct_val.as_any()])?; let h = PyList::new(py, [pair1, pair2])?; - p.getattr("response_bytes")? - .call1((405u16, h, b"Method Not Allowed" as &[u8]))?; + p.call_method1( + "response_bytes", + (405u16, h, b"Method Not Allowed" as &[u8]), + )?; Ok(()) } @@ -324,11 +326,10 @@ pub fn send_429_rate_limited_sync( let pair5 = PyTuple::new(py, [retry_key.as_any(), retry_val.as_any()])?; let h = PyList::new(py, [pair1, pair2, pair3, pair4, pair5])?; - p.getattr("response_bytes")?.call1(( - 429u16, - h, - b"{\"detail\":\"Too Many Requests\"}" as &[u8], - ))?; + p.call_method1( + "response_bytes", + (429u16, h, b"{\"detail\":\"Too Many Requests\"}" as &[u8]), + )?; Ok(()) } @@ -360,7 +361,7 @@ pub fn send_head_simple_sync( let pair1 = PyTuple::new(py, [ct_key.as_any(), ct_val.as_any()])?; let pair2 = PyTuple::new(py, [cl_key.as_any(), cl_val.as_any()])?; let h = PyList::new(py, [pair1, pair2])?; - p.getattr("response_empty")?.call1((status, h))?; + p.call_method1("response_empty", (status, h))?; Ok(()) }