36d6645e7f
El rectificador deja la aproximación y pasa a la trigonometría exacta, con precisión de segundo — el "microajuste argentino". LA MATEMÁTICA. El rectificador ya NO usa el modelo simplificado (directed_longitude, rotación uniforme de RA + convergencia GR). Ahora usa `eternal_astrology::primary_direction::all_directions` — el método Placidus-mundano: semi-arcos diurnos/nocturnos bajo el polo de cada cuerpo, la trigonometría esférica de la escuela ascensional. No se reimplementó nada: la matemática, ya probada, vive en eternal; el engine sólo aporta la capa de optimización. - error_de_carta: por cada evento, la distancia en años a la dirección primaria que perfecciona más cerca; el error total es la suma. Es la función de coste del microajuste — el valle es la hora real. PRECISIÓN DE SEGUNDO. compute_natal_chart / build_eternal_inputs / natal_cache pasan a trabajar en SEGUNDOS (compose convierte ×60). El rectificador barre en dos pasadas: gruesa minuto a minuto sobre la ventana (el perfil que dibuja la curva), fina segundo a segundo en ±60 s alrededor del mejor minuto. - Rectificacion: mejor_offset_segundos; el perfil va en segundos. - UI: panel y curva muestran «±Xm Ys · error N.NNa». Las barras siguen siendo clicables (scrub a esa hora candidata). Tests verdes (engine 12, render 28). Limitación conocida: all_directions es sólo directo — converso necesita crecer en eternal (upstream). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
117 lines
3.9 KiB
Rust
117 lines
3.9 KiB
Rust
//! LRU cache para `NatalChart` por contenido.
|
|
//!
|
|
//! `NatalChart::compute` cuesta varios ms (VSOP2013 + casas + aspectos
|
|
//! base). En el shell, mover el slider de orbe o tocar un toggle
|
|
//! dispara un `compose()` completo donde la **misma** carta natal del
|
|
//! sujeto principal se recomputa idéntica. Lo mismo pasa con el partner
|
|
//! de Synastry / Composite — cada drag de slider rearma `partner_natal`.
|
|
//!
|
|
//! Este cache de 8 entradas es suficiente: el usuario rara vez tiene
|
|
//! más de 2 cartas activas a la vez (natal + partner) y el LRU bota la
|
|
//! más vieja cuando se llena. La clave es el **contenido** de
|
|
//! `StoredBirthData + StoredChartConfig + offset_seconds`, así que
|
|
//! editar una carta invalida automáticamente su entrada.
|
|
|
|
use std::collections::hash_map::DefaultHasher;
|
|
use std::hash::{Hash, Hasher};
|
|
use std::sync::{Arc, Mutex, OnceLock};
|
|
|
|
use eternal_astrology::NatalChart;
|
|
use cosmobiologia_model::{StoredBirthData, StoredChartConfig};
|
|
|
|
const CAPACITY: usize = 8;
|
|
|
|
type Key = u64;
|
|
|
|
struct Cache {
|
|
/// Front = más reciente, back = más viejo. `VecDeque` simple — con
|
|
/// cap 8 el search lineal cuesta menos que un HashMap.
|
|
entries: Vec<(Key, Arc<NatalChart>)>,
|
|
}
|
|
|
|
impl Cache {
|
|
fn new() -> Self {
|
|
Self {
|
|
entries: Vec::with_capacity(CAPACITY),
|
|
}
|
|
}
|
|
|
|
fn get(&mut self, k: Key) -> Option<Arc<NatalChart>> {
|
|
let idx = self.entries.iter().position(|(kk, _)| *kk == k)?;
|
|
// Move-to-front para mantener LRU.
|
|
let hit = self.entries.remove(idx);
|
|
let chart = hit.1.clone();
|
|
self.entries.insert(0, hit);
|
|
Some(chart)
|
|
}
|
|
|
|
fn put(&mut self, k: Key, v: Arc<NatalChart>) {
|
|
// Si ya existe la entrada (race: dos threads computaron lo mismo
|
|
// antes de poblar), reemplaza in-place.
|
|
if let Some(idx) = self.entries.iter().position(|(kk, _)| *kk == k) {
|
|
self.entries.remove(idx);
|
|
}
|
|
self.entries.insert(0, (k, v));
|
|
if self.entries.len() > CAPACITY {
|
|
self.entries.pop();
|
|
}
|
|
}
|
|
}
|
|
|
|
static CACHE: OnceLock<Mutex<Cache>> = OnceLock::new();
|
|
|
|
fn cache() -> &'static Mutex<Cache> {
|
|
CACHE.get_or_init(|| Mutex::new(Cache::new()))
|
|
}
|
|
|
|
/// Hash de contenido: incluye todos los campos relevantes para el
|
|
/// cómputo de la carta natal. `f64` se hashea via `to_bits` para evitar
|
|
/// el `Hash` ausente de los flotantes.
|
|
pub fn key_for(
|
|
birth: &StoredBirthData,
|
|
config: &StoredChartConfig,
|
|
offset_seconds: i64,
|
|
) -> u64 {
|
|
let mut h = DefaultHasher::new();
|
|
// Birth data — fecha/hora/lugar.
|
|
birth.year.hash(&mut h);
|
|
birth.month.hash(&mut h);
|
|
birth.day.hash(&mut h);
|
|
birth.hour.hash(&mut h);
|
|
birth.minute.hash(&mut h);
|
|
birth.second.to_bits().hash(&mut h);
|
|
birth.tz_offset_minutes.hash(&mut h);
|
|
birth.latitude_deg.to_bits().hash(&mut h);
|
|
birth.longitude_deg.to_bits().hash(&mut h);
|
|
birth.altitude_m.to_bits().hash(&mut h);
|
|
// Config — todos los toggles que afectan el cómputo de placements y
|
|
// casas. Los enums derivan Debug; reusamos eso para hashear sin
|
|
// forzarles `Hash` manualmente.
|
|
format!("{:?}", config.house_system).hash(&mut h);
|
|
format!("{:?}", config.zodiac).hash(&mut h);
|
|
config.ayanamsha.hash(&mut h);
|
|
config.bodies.hash(&mut h);
|
|
config.include_south_node.hash(&mut h);
|
|
config.include_lilith.hash(&mut h);
|
|
config.include_main_belt_asteroids.hash(&mut h);
|
|
config.include_fixed_stars.hash(&mut h);
|
|
// Offset temporal en segundos (microajuste de rectificación).
|
|
offset_seconds.hash(&mut h);
|
|
h.finish()
|
|
}
|
|
|
|
/// Consulta. Devuelve `None` en miss; el caller debe computar y llamar
|
|
/// a `insert`.
|
|
pub fn get(k: Key) -> Option<Arc<NatalChart>> {
|
|
cache().lock().ok()?.get(k)
|
|
}
|
|
|
|
/// Inserta una entrada. Idempotente: re-insertar la misma key la mueve
|
|
/// al frente.
|
|
pub fn insert(k: Key, v: Arc<NatalChart>) {
|
|
if let Ok(mut guard) = cache().lock() {
|
|
guard.put(k, v);
|
|
}
|
|
}
|
|
|