Module thread_local

Module thread_local 

Source
Expand description

Optimized thread-local parser infrastructure

This module provides an optimized thread-local parser pool that eliminates duplication and reduces initialization overhead across the codebase.

§Performance Optimizations

  1. OnceLock initialization - Parser created once per thread, not on every call
  2. Direct language detection - Uses Language::from_extension directly
  3. Reduced RefCell overhead - Single borrow per parse operation
  4. Centralized API - Eliminates code duplication across CLI commands

§Usage

use infiniloom_engine::parser::parse_file_symbols;
use std::path::Path;

let content = "fn main() {}";
let path = Path::new("src/main.rs");
let symbols = parse_file_symbols(content, path);

§Migration from Old Pattern

Before (duplicated in 3 places):

use std::cell::RefCell;
use crate::parser::{Parser, Language};

thread_local! {
    static THREAD_PARSER: RefCell<Parser> = RefCell::new(Parser::new());
}

let symbols = THREAD_PARSER.with(|parser| {
    let mut parser = parser.borrow_mut();
    if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
        if let Some(lang) = Language::from_extension(ext) {
            parser.parse(content, lang).unwrap_or_default()
        } else {
            Vec::new()
        }
    } else {
        Vec::new()
    }
});

After (centralized):

use infiniloom_engine::parser::parse_file_symbols;

let symbols = parse_file_symbols(content, path);

Functions§

parse_file_symbols
Parse file content using optimized thread-local parser
parse_with_language
Parse content with explicit language (bypasses extension detection)