Skip to content

perf: avoid per-header to_lowercase() allocation in proxy #68

Description

@ttatsato

Problem

src/routes/proxy.rs:73 allocates a String for every inbound header just to compare against a small blacklist:

for (k, v) in headers.iter() {
    let name = k.as_str().to_lowercase();
    if matches!(name.as_str(), "host" | "x-api-key" | "content-length") {
        continue;
    }
    req = req.header(k.as_str(), v);
}

HeaderName already normalizes to lowercase internally, and even if it did not, eq_ignore_ascii_case would do the comparison without allocation.

Proposed fix

for (k, v) in headers.iter() {
    let name = k.as_str();
    if name.eq_ignore_ascii_case("host")
        || name.eq_ignore_ascii_case("x-api-key")
        || name.eq_ignore_ascii_case("content-length")
    {
        continue;
    }
    req = req.header(name, v);
}

Or, since http::HeaderName constants exist:

use axum::http::header;
if k == header::HOST || k == header::CONTENT_LENGTH || k.as_str() == "x-api-key" { continue; }

Notes

  • Micro-optimization; impact is small but the change is trivial and the current code is misleading.
  • Good first issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions