Mercurial > public > mercurial-scm > hg-stable
annotate rust/hg-cpython/src/copy_tracing.rs @ 46627:47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
? that runs in parallel with the parent thread fetching data.
This can be disabled through a new config. CLI example:
hg --config=devel.copy-tracing.multi-thread=no
For now both threads use the GIL, later commits will reduce this.
Differential Revision: https://phab.mercurial-scm.org/D9684
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Wed, 06 Jan 2021 14:09:01 +0100 |
parents | cb4b0b0c6de4 |
children | 620c88fb42a2 |
rev | line source |
---|---|
45960
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
1 use cpython::ObjectProtocol; |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
2 use cpython::PyBytes; |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
3 use cpython::PyDict; |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
4 use cpython::PyList; |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
5 use cpython::PyModule; |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
6 use cpython::PyObject; |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
7 use cpython::PyResult; |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
8 use cpython::PyTuple; |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
9 use cpython::Python; |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
10 |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
11 use hg::copy_tracing::ChangedFiles; |
46626
cb4b0b0c6de4
copies-rust: split up combine_changeset_copies function into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46613
diff
changeset
|
12 use hg::copy_tracing::CombineChangesetCopies; |
45960
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
13 use hg::Revision; |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
14 |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
15 /// Combines copies information contained into revision `revs` to build a copy |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
16 /// map. |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
17 /// |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
18 /// See mercurial/copies.py for details |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
19 pub fn combine_changeset_copies_wrapper( |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
20 py: Python, |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
21 revs: PyList, |
46149
294d5aca4ff5
copies: iterate over children directly (instead of parents)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46061
diff
changeset
|
22 children_count: PyDict, |
45960
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
23 target_rev: Revision, |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
24 rev_info: PyObject, |
46627
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
25 multi_thread: bool, |
45960
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
26 ) -> PyResult<PyDict> { |
46626
cb4b0b0c6de4
copies-rust: split up combine_changeset_copies function into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46613
diff
changeset
|
27 let children_count = children_count |
45960
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
28 .items(py) |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
29 .iter() |
46149
294d5aca4ff5
copies: iterate over children directly (instead of parents)
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
46061
diff
changeset
|
30 .map(|(k, v)| Ok((k.extract(py)?, v.extract(py)?))) |
46626
cb4b0b0c6de4
copies-rust: split up combine_changeset_copies function into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46613
diff
changeset
|
31 .collect::<PyResult<_>>()?; |
cb4b0b0c6de4
copies-rust: split up combine_changeset_copies function into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46613
diff
changeset
|
32 |
cb4b0b0c6de4
copies-rust: split up combine_changeset_copies function into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46613
diff
changeset
|
33 /// (Revision number, parent 1, parent 2, copy data for this revision) |
cb4b0b0c6de4
copies-rust: split up combine_changeset_copies function into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46613
diff
changeset
|
34 type RevInfo = (Revision, Revision, Revision, Option<PyBytes>); |
cb4b0b0c6de4
copies-rust: split up combine_changeset_copies function into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46613
diff
changeset
|
35 |
cb4b0b0c6de4
copies-rust: split up combine_changeset_copies function into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46613
diff
changeset
|
36 let revs_info = revs.iter(py).map(|rev_py| -> PyResult<RevInfo> { |
cb4b0b0c6de4
copies-rust: split up combine_changeset_copies function into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46613
diff
changeset
|
37 let rev = rev_py.extract(py)?; |
cb4b0b0c6de4
copies-rust: split up combine_changeset_copies function into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46613
diff
changeset
|
38 let tuple: PyTuple = |
cb4b0b0c6de4
copies-rust: split up combine_changeset_copies function into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46613
diff
changeset
|
39 rev_info.call(py, (rev_py,), None)?.cast_into(py)?; |
cb4b0b0c6de4
copies-rust: split up combine_changeset_copies function into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46613
diff
changeset
|
40 let p1 = tuple.get_item(py, 0).extract(py)?; |
cb4b0b0c6de4
copies-rust: split up combine_changeset_copies function into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46613
diff
changeset
|
41 let p2 = tuple.get_item(py, 1).extract(py)?; |
cb4b0b0c6de4
copies-rust: split up combine_changeset_copies function into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46613
diff
changeset
|
42 let opt_bytes = tuple.get_item(py, 2).extract(py)?; |
cb4b0b0c6de4
copies-rust: split up combine_changeset_copies function into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46613
diff
changeset
|
43 Ok((rev, p1, p2, opt_bytes)) |
cb4b0b0c6de4
copies-rust: split up combine_changeset_copies function into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46613
diff
changeset
|
44 }); |
45960
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
45 |
46627
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
46 let path_copies = if !multi_thread { |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
47 let mut combine_changeset_copies = |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
48 CombineChangesetCopies::new(children_count); |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
49 |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
50 for rev_info in revs_info { |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
51 let (rev, p1, p2, opt_bytes) = rev_info?; |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
52 let files = match &opt_bytes { |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
53 Some(bytes) => ChangedFiles::new(bytes.data(py)), |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
54 // Python None was extracted to Option::None, |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
55 // meaning there was no copy data. |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
56 None => ChangedFiles::new_empty(), |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
57 }; |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
58 |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
59 combine_changeset_copies.add_revision(rev, p1, p2, files) |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
60 } |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
61 combine_changeset_copies.finish(target_rev) |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
62 } else { |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
63 // Use a bounded channel to provide back-pressure: |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
64 // if the child thread is slower to process revisions than this thread |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
65 // is to gather data for them, an unbounded channel would keep |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
66 // growing and eat memory. |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
67 // |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
68 // TODO: tweak the bound? |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
69 let (rev_info_sender, rev_info_receiver) = |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
70 crossbeam_channel::bounded::<RevInfo>(1000); |
46626
cb4b0b0c6de4
copies-rust: split up combine_changeset_copies function into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46613
diff
changeset
|
71 |
46627
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
72 // Start a thread that does CPU-heavy processing in parallel with the |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
73 // loop below. |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
74 // |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
75 // If the parent thread panics, `rev_info_sender` will be dropped and |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
76 // “disconnected”. `rev_info_receiver` will be notified of this and |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
77 // exit its own loop. |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
78 let thread = std::thread::spawn(move || { |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
79 let mut combine_changeset_copies = |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
80 CombineChangesetCopies::new(children_count); |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
81 for (rev, p1, p2, opt_bytes) in rev_info_receiver { |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
82 let gil = Python::acquire_gil(); |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
83 let py = gil.python(); |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
84 let files = match &opt_bytes { |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
85 Some(raw) => ChangedFiles::new(raw.data(py)), |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
86 // Python None was extracted to Option::None, |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
87 // meaning there was no copy data. |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
88 None => ChangedFiles::new_empty(), |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
89 }; |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
90 combine_changeset_copies.add_revision(rev, p1, p2, files) |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
91 } |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
92 |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
93 combine_changeset_copies.finish(target_rev) |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
94 }); |
46626
cb4b0b0c6de4
copies-rust: split up combine_changeset_copies function into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46613
diff
changeset
|
95 |
46627
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
96 for rev_info in revs_info { |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
97 let (rev, p1, p2, opt_bytes) = rev_info?; |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
98 |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
99 // We’d prefer to avoid the child thread calling into Python code, |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
100 // but this avoids a potential deadlock on the GIL if it does: |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
101 py.allow_threads(|| { |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
102 rev_info_sender.send((rev, p1, p2, opt_bytes)).expect( |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
103 "combine_changeset_copies: channel is disconnected", |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
104 ); |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
105 }); |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
106 } |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
107 // We’d prefer to avoid the child thread calling into Python code, |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
108 // but this avoids a potential deadlock on the GIL if it does: |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
109 py.allow_threads(|| { |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
110 // Disconnect the channel to signal the child thread to stop: |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
111 // the `for … in rev_info_receiver` loop will end. |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
112 drop(rev_info_sender); |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
113 |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
114 // Wait for the child thread to stop, and propagate any panic. |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
115 thread.join().unwrap_or_else(|panic_payload| { |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
116 std::panic::resume_unwind(panic_payload) |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
117 }) |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
118 }) |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
119 }; |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
120 |
45960
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
121 let out = PyDict::new(py); |
46626
cb4b0b0c6de4
copies-rust: split up combine_changeset_copies function into a struct
Simon Sapin <simon.sapin@octobus.net>
parents:
46613
diff
changeset
|
122 for (dest, source) in path_copies.into_iter() { |
45960
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
123 out.set_item( |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
124 py, |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
125 PyBytes::new(py, &dest.into_vec()), |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
126 PyBytes::new(py, &source.into_vec()), |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
127 )?; |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
128 } |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
129 Ok(out) |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
130 } |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
131 |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
132 /// Create the module, with `__package__` given from parent |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
133 pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> { |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
134 let dotted_name = &format!("{}.copy_tracing", package); |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
135 let m = PyModule::new(py, dotted_name)?; |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
136 |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
137 m.add(py, "__package__", package)?; |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
138 m.add(py, "__doc__", "Copy tracing - Rust implementation")?; |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
139 |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
140 m.add( |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
141 py, |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
142 "combine_changeset_copies", |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
143 py_fn!( |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
144 py, |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
145 combine_changeset_copies_wrapper( |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
146 revs: PyList, |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
147 children: PyDict, |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
148 target_rev: Revision, |
46627
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
149 rev_info: PyObject, |
47557ea79fc7
copies-rust: move CPU-heavy Rust processing into a child thread
Simon Sapin <simon.sapin@octobus.net>
parents:
46626
diff
changeset
|
150 multi_thread: bool |
45960
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
151 ) |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
152 ), |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
153 )?; |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
154 |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
155 let sys = PyModule::import(py, "sys")?; |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
156 let sys_modules: PyDict = sys.get(py, "modules")?.extract(py)?; |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
157 sys_modules.set_item(py, dotted_name, &m)?; |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
158 |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
159 Ok(m) |
50c5ee3bdf9a
copies: introduce the hg-cpython wrapper for `combine_changeset_copies`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff
changeset
|
160 } |