diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..fcc9543 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-04-19 - Optimization of cold start enrichment hot path +**Learning:** In high-frequency loops (like `updateTorque` running at 2000Hz), defining functions and tables locally causes excessive garbage collection pressure due to constant allocations. +**Action:** Move static data structures and helper functions to the module level to avoid re-allocation in the hot path. diff --git a/combustionEngine.lua b/combustionEngine.lua index 0aff3be..c597e9f 100644 --- a/combustionEngine.lua +++ b/combustionEngine.lua @@ -36,6 +36,49 @@ local torqueToPower = 0.0001404345295653085 local psToWatt = 735.499 local hydrolockThreshold = 1.9 +-- Cold start enrichment using temperature-based lookup table +local enrichmentMap = { + [-30] = 3.0, + [-20] = 2.6, + [-10] = 2.2, + [0] = 1.8, + [10] = 1.5, + [20] = 1.3, + [30] = 1.15, + [40] = 1.05, + [50] = 1.02, + [60] = 1.0, + [70] = 1.0 +} + +local function getColdEnrichment(tempC) + -- Find the two closest temperature points + local lowerTemp = -20 + local upperTemp = 80 + local lowerEnrich = 3.0 + local upperEnrich = 0.85 + + -- Find the two closest temperature points in the map + for temp, enrich in pairs(enrichmentMap) do + if temp <= tempC and temp > lowerTemp then + lowerTemp = temp + lowerEnrich = enrich + end + if temp >= tempC and temp < upperTemp then + upperTemp = temp + upperEnrich = enrich + end + end + + -- Linear interpolation between the two closest points + if lowerTemp == upperTemp then + return lowerEnrich + end + + local t = (tempC - lowerTemp) / (upperTemp - lowerTemp) + return lowerEnrich + (upperEnrich - lowerEnrich) * t +end + local function getTorqueData(device) local curves = {} local curveCounter = 1 @@ -1241,52 +1284,7 @@ local function updateTorque(device, dt) -- Temperature effect on starter torque (reduces torque in cold conditions) local tempEffectOnStarter = 1.0 - math.max(0, math.min(0.7, (0 - engineTempC) / 30)) - - -- Cold start enrichment using temperature-based lookup table (reduced values) - local function getColdEnrichment(tempC) - -- Temperature in Celsius to enrichment factor mapping - -- [tempC] = enrichmentMultiplier - local enrichmentMap = { - [-30] = 3.0, -- Reduced from 4.0 - [-20] = 2.6, -- Reduced from 3.5 - [-10] = 2.2, -- Reduced from 3.0 - [0] = 1.8, -- Reduced from 2.5 - [10] = 1.5, -- Reduced from 2.0 - [20] = 1.3, -- Reduced from 1.5 - [30] = 1.15, -- Reduced from 1.25 - [40] = 1.05, -- Reduced from 1.1 - [50] = 1.02, -- Reduced from 1.05 - [60] = 1.0, - [70] = 1.0 - } - - -- Find the two closest temperature points - local lowerTemp = -20 - local upperTemp = 80 - local lowerEnrich = 3.0 - local upperEnrich = 0.85 - - -- Find the two closest temperature points in the map - for temp, _ in pairs(enrichmentMap) do - if temp <= tempC and temp > lowerTemp then - lowerTemp = temp - lowerEnrich = enrichmentMap[temp] - end - if temp >= tempC and temp < upperTemp then - upperTemp = temp - upperEnrich = enrichmentMap[temp] - end - end - - -- Linear interpolation between the two closest points - if lowerTemp == upperTemp then - return lowerEnrich - end - - local t = (tempC - lowerTemp) / (upperTemp - lowerTemp) - return lowerEnrich + (upperEnrich - lowerEnrich) * t - end - + -- Calculate cold start enrichment based on engine temperature local coldStartEnrichment = getColdEnrichment(engineTempC)