diff --git a/packages/squiggle-lang/src/rescript/Utility/E/E_A.res b/packages/squiggle-lang/src/rescript/Utility/E/E_A.res index 46ada15f..5bef1d32 100644 --- a/packages/squiggle-lang/src/rescript/Utility/E/E_A.res +++ b/packages/squiggle-lang/src/rescript/Utility/E/E_A.res @@ -307,9 +307,12 @@ module Floats = { continuous samples and discrete samples. The discrete samples are stored in a mutable map. Samples are thought to be discrete if they have at least `minDiscreteWight` duplicates. - If the min discreet weight is 4, that would mean that at least four elements needed from a specific + If the min discrete weight is 4, that would mean that at least four elements needed from a specific value for that to be kept as discrete. This is important because in some cases, we can expect that - some common elements will be generated by regular operations. The final continous array will be sorted. + some common elements will be generated by regular operations. The final continuous array will be sorted. + + This function is performance-critical, don't change it significantly without benchmarking + SampleSet->PointSet conversion performance. */ let splitContinuousAndDiscreteForMinWeight = ( sortedArray: array, @@ -318,34 +321,32 @@ module Floats = { let continuous: array = [] let discrete = FloatFloatMap.empty() - let flush = (cnt: int, value: float): unit => { - if cnt >= minDiscreteWeight { - FloatFloatMap.add(value, cnt->Belt.Int.toFloat, discrete) + let addData = (count: int, value: float): unit => { + if count >= minDiscreteWeight { + FloatFloatMap.add(value, count->Belt.Int.toFloat, discrete) } else { - for _ in 1 to cnt { - let _ = continuous->Js.Array2.push(value) + for _ in 1 to count { + continuous->Js.Array2.push(value)->ignore } } } - if sortedArray->Js.Array2.length != 0 { - let (finalCnt, finalValue) = sortedArray->Belt.Array.reduce( - // initial prev value doesn't matter; if it collides with the first element of the array, flush won't do anything - (0, 0.), - ((cnt, prev), element) => { - if element == prev { - (cnt + 1, prev) - } else { - // new value, process previous ones - flush(cnt, prev) - (1, element) - } - }, - ) + let (finalCount, finalValue) = sortedArray->Belt.Array.reduce( + // initial prev value doesn't matter; if it collides with the first element of the array, flush won't do anything + (0, 0.), + ((count, prev), element) => { + if element == prev { + (count + 1, prev) + } else { + // new value, process previous ones + addData(count, prev) + (1, element) + } + }, + ) - // flush final values - flush(finalCnt, finalValue) - } + // flush final values + addData(finalCount, finalValue) (continuous, discrete) }