Skip to content

Commit

Permalink
Use swap to save on memory allocs
Browse files Browse the repository at this point in the history
  • Loading branch information
InnovativeInventor committed May 17, 2022
1 parent cd7cecc commit 9a821f6
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
use std::mem;

use std::cmp::Ordering;
use std::collections::VecDeque;
Expand Down Expand Up @@ -209,7 +211,8 @@ pub fn balanced_cut_edge(
// this will be false if root
let neighbor = unseen_neighbors[0];
pops[neighbor.index()] += pop.clone();
let mut current_partition_tracker = same_partition_tracker[node.index()].clone();
let mut current_partition_tracker = Vec::<usize>::new();
mem::swap(&mut current_partition_tracker, &mut same_partition_tracker[node.index()]);
same_partition_tracker[neighbor.index()].append(&mut current_partition_tracker);
// eprintln!("node pushed to queue (pop = {}, target = {}): {}", pops[neighbor.index()], pop_target, neighbor.index());

Expand Down Expand Up @@ -270,7 +273,7 @@ pub fn bipartition_tree(
let mut balanced_nodes: Vec<(usize, Vec<usize>)> = vec![];

while balanced_nodes.len() == 0 {
// Wee: https://pyo3.rs/v0.15.1/memory.html#gil-bound-memory
// See: https://pyo3.rs/v0.15.1/memory.html#gil-bound-memory
// (workaround to force objects to be gc'ed on each loop)
let pool = unsafe { py.new_pool() };
let py = pool.python();
Expand Down

0 comments on commit 9a821f6

Please sign in to comment.