Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release the GIL in run_block_generator() and run_block_generator2() #712

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 48 additions & 38 deletions wheel/src/run_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn py_to_slice<'a>(buf: PyBuffer<u8>) -> &'a [u8] {

#[pyfunction]
pub fn run_block_generator<'a>(
_py: Python<'a>,
py: Python<'a>,
program: PyBuffer<u8>,
block_refs: &Bound<'_, PyList>,
max_cost: Cost,
Expand All @@ -29,37 +29,42 @@ pub fn run_block_generator<'a>(
) -> (Option<u32>, Option<OwnedSpendBundleConditions>) {
let mut allocator = make_allocator(flags);

let refs = block_refs.into_iter().map(|b| {
let buf = b
.extract::<PyBuffer<u8>>()
.expect("block_refs should be a list of buffers");
py_to_slice::<'a>(buf)
});
let refs = block_refs
.into_iter()
.map(|b| {
let buf = b
.extract::<PyBuffer<u8>>()
.expect("block_refs should be a list of buffers");
py_to_slice::<'a>(buf)
})
.collect::<Vec<&'a [u8]>>();
let program = py_to_slice::<'a>(program);
let run_block = if (flags & ANALYZE_SPENDS) == 0 {
native_run_block_generator::<_, EmptyVisitor, _>
} else {
native_run_block_generator::<_, MempoolVisitor, _>
};

match run_block(&mut allocator, program, refs, max_cost, flags, constants) {
Ok(spend_bundle_conds) => (
None,
Some(OwnedSpendBundleConditions::from(
&allocator,
spend_bundle_conds,
)),
),
Err(ValidationErr(_, error_code)) => {
// a validation error occurred
(Some(error_code.into()), None)
py.allow_threads(|| {
match run_block(&mut allocator, program, refs, max_cost, flags, constants) {
Ok(spend_bundle_conds) => (
None,
Some(OwnedSpendBundleConditions::from(
&allocator,
spend_bundle_conds,
)),
),
Err(ValidationErr(_, error_code)) => {
// a validation error occurred
(Some(error_code.into()), None)
}
}
}
})
}

#[pyfunction]
pub fn run_block_generator2<'a>(
_py: Python<'a>,
py: Python<'a>,
program: PyBuffer<u8>,
block_refs: &Bound<'_, PyList>,
max_cost: Cost,
Expand All @@ -68,12 +73,15 @@ pub fn run_block_generator2<'a>(
) -> (Option<u32>, Option<OwnedSpendBundleConditions>) {
let mut allocator = make_allocator(flags);

let refs = block_refs.into_iter().map(|b| {
let buf = b
.extract::<PyBuffer<u8>>()
.expect("block_refs must be list of buffers");
py_to_slice::<'a>(buf)
});
let refs = block_refs
.into_iter()
.map(|b| {
let buf = b
.extract::<PyBuffer<u8>>()
.expect("block_refs must be list of buffers");
py_to_slice::<'a>(buf)
})
.collect::<Vec<&'a [u8]>>();

let program = py_to_slice::<'a>(program);
let run_block = if (flags & ANALYZE_SPENDS) == 0 {
Expand All @@ -82,17 +90,19 @@ pub fn run_block_generator2<'a>(
native_run_block_generator2::<_, MempoolVisitor, _>
};

match run_block(&mut allocator, program, refs, max_cost, flags, constants) {
Ok(spend_bundle_conds) => (
None,
Some(OwnedSpendBundleConditions::from(
&allocator,
spend_bundle_conds,
)),
),
Err(ValidationErr(_, error_code)) => {
// a validation error occurred
(Some(error_code.into()), None)
py.allow_threads(|| {
match run_block(&mut allocator, program, refs, max_cost, flags, constants) {
Ok(spend_bundle_conds) => (
None,
Some(OwnedSpendBundleConditions::from(
&allocator,
spend_bundle_conds,
)),
),
Err(ValidationErr(_, error_code)) => {
// a validation error occurred
(Some(error_code.into()), None)
}
}
}
})
}
Loading