Multiple filter or just one

I'm wondering what is the best way to apply multiple filters to a large vector of objects. For example:

struct Pokemon {
  id: i32,
  name: String,
  type: (String, String),

let pokedex: Vec<Pokemon> = <the_all_pokedex>

// First way


// Second way

    filter1(pokemon) &&
    filter2(pokemon) &&
    filter3(pokemon) &&
    filter4(pokemon) &&

I think it probably doesn't matter.

The order will matter more than which style you use: Everything is tested with filter1, but only the items that pass get tested against filter2 and so on in either case. So, put the filter that will probably be most restrictive (throw out the most items) first.

if the filters are function pointers, it doesn't matter. if they're not, you should see increased performance in debug mode with the inlined version.


Thanks @alice, @2e71828, and @Phlopsi for your answers!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.