| 1 |
// copy_tracing.rs |
| 2 |
// |
| 3 |
// Copyright 2025 Mercurial developers |
| 4 |
// |
| 5 |
// This software may be used and distributed according to the terms of the |
| 6 |
// GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
//! Bindings for the `hg::copy_tracing` module provided by the |
| 9 |
//! `hg-core` package. |
| 10 |
//! |
| 11 |
//! From Python, this will be seen as `mercurial.pyo3_rustext.copy_tracing` |
| 12 |
|
| 13 |
use hg::copy_tracing::ChangedFiles; |
| 14 |
use hg::copy_tracing::CombineChangesetCopies; |
| 15 |
use hg::Revision; |
| 16 |
use pyo3::prelude::*; |
| 17 |
use pyo3::types::PyBytes; |
| 18 |
use pyo3::types::PyDict; |
| 19 |
use pyo3::types::PyList; |
| 20 |
use pyo3::types::PyTuple; |
| 21 |
|
| 22 |
use crate::revision::PyRevision; |
| 23 |
use crate::utils::new_submodule; |
| 24 |
use crate::utils::PyBytesDeref; |
| 25 |
|
| 26 |
/// Combines copies information contained into revision `revs` to build a copy |
| 27 |
/// map. |
| 28 |
/// |
| 29 |
/// See mercurial/copies.py for details |
| 30 |
#[pyfunction] |
| 31 |
#[pyo3(name = "combine_changeset_copies")] |
| 32 |
pub fn combine_changeset_copies_wrapper( |
| 33 |
revs: Bound<'_, PyList>, |
| 34 |
children_count: Bound<'_, PyDict>, |
| 35 |
target_rev: PyRevision, |
| 36 |
rev_info: Bound<'_, PyAny>, |
| 37 |
multi_thread: bool, |
| 38 |
) -> PyResult<Py<PyAny>> { |
| 39 |
let py = revs.py(); |
| 40 |
let target_rev = Revision(target_rev.0); |
| 41 |
let children_count = children_count |
| 42 |
.iter() |
| 43 |
.map(|(k, v)| { |
| 44 |
Ok((Revision(k.extract::<PyRevision>()?.0), v.extract()?)) |
| 45 |
}) |
| 46 |
.collect::<PyResult<_>>()?; |
| 47 |
|
| 48 |
/// (Revision number, parent 1, parent 2, copy data for this revision) |
| 49 |
type RevInfo<Bytes> = (Revision, Revision, Revision, Option<Bytes>); |
| 50 |
|
| 51 |
let revs_info = |
| 52 |
revs.iter().map(|rev_py| -> PyResult<RevInfo<Py<PyBytes>>> { |
| 53 |
let rev = Revision(rev_py.extract::<PyRevision>()?.0); |
| 54 |
let ret = rev_info.call1((rev_py,))?; |
| 55 |
let tuple: &Bound<'_, PyTuple> = ret.cast()?; |
| 56 |
let p1 = Revision(tuple.get_item(0)?.extract::<PyRevision>()?.0); |
| 57 |
let p2 = Revision(tuple.get_item(1)?.extract::<PyRevision>()?.0); |
| 58 |
let opt_bytes = tuple.get_item(2)?.extract()?; |
| 59 |
Ok((rev, p1, p2, opt_bytes)) |
| 60 |
}); |
| 61 |
|
| 62 |
let path_copies; |
| 63 |
if !multi_thread { |
| 64 |
let mut combine_changeset_copies = |
| 65 |
CombineChangesetCopies::new(children_count); |
| 66 |
|
| 67 |
for rev_info in revs_info { |
| 68 |
let (rev, p1, p2, opt_bytes) = rev_info?; |
| 69 |
let files = match &opt_bytes { |
| 70 |
Some(bytes) => ChangedFiles::new(bytes.as_bytes(py)), |
| 71 |
// Python None was extracted to Option::None, |
| 72 |
// meaning there was no copy data. |
| 73 |
None => ChangedFiles::new_empty(), |
| 74 |
}; |
| 75 |
|
| 76 |
combine_changeset_copies.add_revision(rev, p1, p2, files) |
| 77 |
} |
| 78 |
path_copies = combine_changeset_copies.finish(target_rev) |
| 79 |
} else { |
| 80 |
// Use a bounded channel to provide back-pressure: |
| 81 |
// if the child thread is slower to process revisions than this thread |
| 82 |
// is to gather data for them, an unbounded channel would keep |
| 83 |
// growing and eat memory. |
| 84 |
// |
| 85 |
// TODO: tweak the bound? |
| 86 |
let (rev_info_sender, rev_info_receiver) = |
| 87 |
crossbeam_channel::bounded::<RevInfo<PyBytesDeref>>(1000); |
| 88 |
|
| 89 |
// This channel (going the other way around) however is unbounded. |
| 90 |
// If they were both bounded, there might potentially be deadlocks |
| 91 |
// where both channels are full and both threads are waiting on each |
| 92 |
// other. |
| 93 |
let (pybytes_sender, pybytes_receiver) = crossbeam_channel::unbounded(); |
| 94 |
|
| 95 |
// Start a thread that does CPU-heavy processing in parallel with the |
| 96 |
// loop below. |
| 97 |
// |
| 98 |
// If the parent thread panics, `rev_info_sender` will be dropped and |
| 99 |
// “disconnected”. `rev_info_receiver` will be notified of this and |
| 100 |
// exit its own loop. |
| 101 |
let thread = std::thread::spawn(move || { |
| 102 |
let mut combine_changeset_copies = |
| 103 |
CombineChangesetCopies::new(children_count); |
| 104 |
for (rev, p1, p2, opt_bytes) in rev_info_receiver { |
| 105 |
let files = match &opt_bytes { |
| 106 |
Some(raw) => ChangedFiles::new(raw.as_ref()), |
| 107 |
// Python None was extracted to Option::None, |
| 108 |
// meaning there was no copy data. |
| 109 |
None => ChangedFiles::new_empty(), |
| 110 |
}; |
| 111 |
combine_changeset_copies.add_revision(rev, p1, p2, files); |
| 112 |
|
| 113 |
// Send `PyBytes` back to the parent thread so the parent |
| 114 |
// thread can drop it. Otherwise the GIL would be implicitly |
| 115 |
// acquired here through `impl Drop for PyBytes`. |
| 116 |
if let Some(bytes) = opt_bytes { |
| 117 |
if pybytes_sender.send(bytes.unwrap()).is_err() { |
| 118 |
// The channel is disconnected, meaning the parent |
| 119 |
// thread panicked or returned |
| 120 |
// early through |
| 121 |
// `?` to propagate a Python exception. |
| 122 |
break; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
combine_changeset_copies.finish(target_rev) |
| 128 |
}); |
| 129 |
|
| 130 |
for rev_info in revs_info { |
| 131 |
let (rev, p1, p2, opt_bytes) = rev_info?; |
| 132 |
let opt_bytes = opt_bytes.map(|b| PyBytesDeref::new(py, b)); |
| 133 |
|
| 134 |
// We’d prefer to avoid the child thread calling into Python code, |
| 135 |
// but this avoids a potential deadlock on the GIL if it does: |
| 136 |
Python::detach(py, || { |
| 137 |
rev_info_sender.send((rev, p1, p2, opt_bytes)).expect( |
| 138 |
"combine_changeset_copies: channel is disconnected", |
| 139 |
); |
| 140 |
}); |
| 141 |
|
| 142 |
// Drop anything in the channel, without blocking |
| 143 |
pybytes_receiver.try_iter().for_each(drop); |
| 144 |
} |
| 145 |
// We’d prefer to avoid the child thread calling into Python code, |
| 146 |
// but this avoids a potential deadlock on the GIL if it does: |
| 147 |
path_copies = Python::detach(py, || { |
| 148 |
// Disconnect the channel to signal the child thread to stop: |
| 149 |
// the `for … in rev_info_receiver` loop will end. |
| 150 |
drop(rev_info_sender); |
| 151 |
|
| 152 |
// Wait for the child thread to stop, and propagate any panic. |
| 153 |
thread.join().unwrap_or_else(|panic_payload| { |
| 154 |
std::panic::resume_unwind(panic_payload) |
| 155 |
}) |
| 156 |
}); |
| 157 |
|
| 158 |
// Drop anything left in the channel |
| 159 |
drop(pybytes_receiver) |
| 160 |
}; |
| 161 |
|
| 162 |
let out = PyDict::new(py); |
| 163 |
for (dest, source) in path_copies.into_iter() { |
| 164 |
out.set_item( |
| 165 |
PyBytes::new(py, &dest.into_vec()), |
| 166 |
PyBytes::new(py, &source.into_vec()), |
| 167 |
)?; |
| 168 |
} |
| 169 |
Ok(out.into_any().unbind()) |
| 170 |
} |
| 171 |
|
| 172 |
pub fn init_module<'py>( |
| 173 |
py: Python<'py>, |
| 174 |
package: &str, |
| 175 |
) -> PyResult<Bound<'py, PyModule>> { |
| 176 |
let m = new_submodule(py, package, "copy_tracing")?; |
| 177 |
m.add_function(wrap_pyfunction!(combine_changeset_copies_wrapper, &m)?)?; |
| 178 |
Ok(m) |
| 179 |
} |