Solutions to performance issues caused by large variant size of enum

前端 未结 1 519
庸人自扰
庸人自扰 2020-12-21 23:30

I am trying to build a tree-like data structure using the Node representation:

use std::cmp::Ordering;
use std::fmt::Debug;
use std::sync::Arc;
         


        
相关标签:
1条回答
  • 2020-12-22 00:04

    You don't need trait objects here because you don't need the unbounded polymorphism they provide.

    Clippy tells you what to do:

    warning: large size difference between variants
      --> src/main.rs:13:5
       |
    13 | /     RelaxedBranch {
    14 | |         children: [Option<Arc<Node<T>>>; BRANCH_FACTOR],
    15 | |         sizes: [Option<usize>; BRANCH_FACTOR],
    16 | |         len: usize,
    17 | |     },
       | |_____^
       |
       = note: #[warn(large_enum_variant)] on by default
    help: consider boxing the large fields to reduce the total size of the enum
      --> src/main.rs:13:5
       |
    13 | /     RelaxedBranch {
    14 | |         children: [Option<Arc<Node<T>>>; BRANCH_FACTOR],
    15 | |         sizes: [Option<usize>; BRANCH_FACTOR],
    16 | |         len: usize,
    17 | |     },
       | |_____^
       = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.211/index.html#large_enum_variant
    

    Switching to

    RelaxedBranch {
        children: Box<[Option<Arc<Node<T>>>; BRANCH_FACTOR]>,
        sizes: Box<[Option<usize>; BRANCH_FACTOR]>,
        len: usize,
    },
    

    Reduces the size of Node<()> from 784 to 272. You could go further by combining all the fields into a new struct and boxing that.

    0 讨论(0)
提交回复
热议问题