Skip to content

Commit 14f6d4f

Browse files
committed
Use buffered writer for multipart temp files
1 parent e09f4f1 commit 14f6d4f

4 files changed

Lines changed: 10 additions & 6 deletions

File tree

.github/workflows/build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ jobs:
118118
shell: bash
119119
- name: Generate PGO data
120120
shell: bash
121+
env:
122+
PGO_RUN: y
121123
run: |
122124
uv python install ${{ env.UV_PYTHON }}
123125
uv venv .venv

src/multipart/parse.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,9 @@ impl MultiPartParser {
272272

273273
let state = mem::take(&mut self.state);
274274
match state {
275-
MultiPartParserState::File(part) => {
275+
MultiPartParserState::File(mut part) => {
276276
// potentially allow py threads?
277-
part.file.as_ref().unwrap().sync_data()?;
277+
part.file.as_mut().unwrap().flush()?;
278278
self.stack.push_back(Node::File(part));
279279
}
280280
_ => unreachable!(),

src/multipart/parts.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use http::header::{self, HeaderMap};
33
use pyo3::{exceptions::PyStopIteration, prelude::*, types::PyBytes};
44
use std::{
55
fs::File,
6-
io::{BufReader, Read},
6+
io::{BufReader, BufWriter, Read},
77
path::PathBuf,
88
sync::Mutex,
99
};
@@ -44,7 +44,7 @@ pub(super) struct FilePart {
4444
pub name: String,
4545
filename: Option<String>,
4646
path: PathBuf,
47-
pub file: Option<File>,
47+
pub file: Option<BufWriter<File>>,
4848
pub size: Option<usize>,
4949
tempdir: Option<PathBuf>,
5050
}
@@ -61,8 +61,7 @@ impl FilePart {
6161
let tempdir = Some(path.clone());
6262
path.push(TextNonce::sized_urlsafe(32).unwrap().into_string());
6363

64-
let file = File::create(path.clone())?;
65-
64+
let file = BufWriter::with_capacity(131_072, File::create(path.clone())?);
6665
Ok(FilePart {
6766
headers,
6867
name,

tests/multipart/test_multipart.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import os
34
from pathlib import Path
45

56
import pytest
@@ -383,6 +384,7 @@ def test_multipart_request_file_save(tmpdir: Path, multipart_save_client):
383384
assert f1.read() == f2.read()
384385

385386

387+
@pytest.mark.skipif(bool(os.getenv("PGO_RUN")), reason="PGO build")
386388
def test_multipart_request_file_stream(tmpdir: Path, multipart_stream_client):
387389
path = tmpdir / "test.txt"
388390
target = tmpdir / "save.txt"
@@ -400,6 +402,7 @@ def test_multipart_request_file_stream(tmpdir: Path, multipart_stream_client):
400402
assert f1.read() == f2.read()
401403

402404

405+
@pytest.mark.skipif(bool(os.getenv("PGO_RUN")), reason="PGO build")
403406
def test_multipart_request_file_copy(tmpdir: Path, multipart_copy_client):
404407
path = tmpdir / "test.txt"
405408
target = tmpdir / "save.txt"

0 commit comments

Comments
 (0)