cache-lens 0.1.0

A static analyzer that detects cache-unfriendly Array-of-Structures (AoS) usage in Rust and suggests Structure-of-Arrays (SoA) refactors.
use std::collections::HashMap;
use syn::visit::{self, Visit};
use syn::{Fields, ItemStruct};

use crate::model::{FieldInfo, StructInfo};

#[derive(Default)]
pub struct StructCollector {
    pub structs: HashMap<String, StructInfo>,
}

impl<'ast> Visit<'ast> for StructCollector {
    fn visit_item_struct(&mut self, node: &'ast ItemStruct) {
        if let Fields::Named(named) = &node.fields {
            let fields = named
                .named
                .iter()
                .filter_map(|f| {
                    let name = f.ident.as_ref()?.to_string();
                    Some(FieldInfo {
                        name,
                        ty: f.ty.clone(),
                    })
                })
                .collect::<Vec<_>>();
            let def_line = node.ident.span().start().line;

            self.structs
                .entry(node.ident.to_string())
                .or_insert(StructInfo {
                    name: node.ident.to_string(),
                    fields,
                    def_line,
                });
        }
        visit::visit_item_struct(self, node);
    }
}