rust-lang / rust-lang/rust-clippy

Boolean lists can be optimized as bitset/bitvecs

Open
#8,893 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Reading the README of the bitvec crate, it says

To get started, you can perform basic text replacement on your project. Translate any existing types as follows:
[bool; N] becomes BitArray
[bool] becomes BitSlice
Vec becomes BitVec
Box<[bool]> becomes BitBox

I was wondering how easy it would be to write a performance lint for this since I know at least in C, each bool is a usize long. I am not sure if the Rust compiler optimizes this code to make each bool a bit and packs them together. There are many bitvec-like crates that people could use.

Lint Name

bool_collection

Category

perf

Advantage

Bits take less space than bools, and are probably faster to work with.

Drawbacks

I am sure there are tons of false positives where it is useful to keep an array of booleans. Also, bitvec is a crate, so another dependency will need to be added to the crate/binary. Also bitvec is not the newest but not the matureist either, and I think this is the most mature bit manipulation crate.

Example

Using the bit_array crate

fn main() {
  let mut bool_list = [false; 4];
  bool_list[1] = true;
  
  let sum = bool_list.iter().filter(|x| **x).count();
}

Could be written as:

extern crate typenum;
use bit_array::BitArray;
use typenum::{Unsigned, U4};

fn main() {
  let mut bitarr = BitArray::<u32, U4>::from_elem(false);
  bitarr.set(1, true);
  
  let sum = bitarr.iter().filter(|x| *x).count();
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Begin with the proposed lint name and the bitvec README mappings cited in the issue. No source file, test, or entry point is identified, so first establish which Rust boolean collections and replacement crate are in scope. Done requires defined lint behavior and coverage for intended cases, but the issue does not specify those cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools, performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.