Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 28ec30e

Browse files
committed
perf: single-pass getValByDates for sorted curves (#357 Tier 2)
Add a forward-cursor fast path for getValByDates when curve points and input dates are ascending, avoiding a reversed-list allocation and O(n) scan for every date. Descending dates fall back lazily for the remaining suffix; unsorted points and unsupported curve types use the legacy per-date lookup, so results are unchanged. Add legacy-oracle UTs covering Inc/Exc, duplicates, exact boundaries, before/after-range dates, descending and mixed date order, unsorted points, empty curves/dates, FactorCurveClosed, ThresholdCurve and PricingCurve. 609 tests pass.
1 parent fc4f26b commit 28ec30e

3 files changed

Lines changed: 181 additions & 2 deletions

File tree

‎src/Util.hs‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE OverloadedStrings #-}
22
{-# LANGUAGE ScopedTypeVariables #-}
3+
{-# LANGUAGE BangPatterns #-}
34

45
module Util
56
(mulBR,mulBIR,mulBI,mulBInt,mulBInteger,lastN
@@ -203,8 +204,70 @@ getIndexRateByDates :: RateAssumption -> [Date] -> [IRate]
203204
getIndexRateByDates (RateCurve idx rc) ds = fromRational <$> getValByDates rc Inc ds
204205
getIndexRateByDates (RateFlat idx r) ds = replicate (length ds) r
205206

207+
-- | Curve points are traversed with a single forward cursor when the input
208+
-- dates are ascending. This avoids reversing the curve for every date, which
209+
-- is what the per-date 'getValByDate' path does. Unsorted dates (or unsorted
210+
-- points) fall back to the legacy implementation, so results are unchanged.
206211
getValByDates :: Ts -> CutoffType -> [Date] -> [Rational]
207-
getValByDates rc ct = map (getValByDate rc ct)
212+
getValByDates _ _ [] = []
213+
getValByDates rc@(LeftBalanceCurve pts) ct ds
214+
| isAscendingPoints pts = mergeLastBy toRational (getValByDate rc ct) ct pts ds
215+
getValByDates rc@(BalanceCurve pts) ct ds
216+
| isAscendingPoints pts = mergeLastBy toRational (getValByDate rc ct) ct pts ds
217+
getValByDates rc@(FloatCurve pts) ct ds
218+
| isAscendingPoints pts = mergeLastBy id (getValByDate rc ct) ct pts ds
219+
getValByDates rc@(IRateCurve pts) ct ds
220+
| isAscendingPoints pts = mergeLastBy toRational (getValByDate rc ct) ct pts ds
221+
getValByDates rc@(RatioCurve pts) ct ds
222+
| isAscendingPoints pts = mergeLastBy id (getValByDate rc ct) ct pts ds
223+
getValByDates rc@(FactorCurveClosed pts ed) Exc ds
224+
| isAscendingPoints pts = mergeFactorClosed ed (getValByDate rc Exc) pts ds
225+
getValByDates rc ct ds = map (getValByDate rc ct) ds
226+
227+
isAscendingPoints :: [TsPoint a] -> Bool
228+
isAscendingPoints (TsPoint d1 _ : rest@(TsPoint d2 _ : _)) = d1 <= d2 && isAscendingPoints rest
229+
isAscendingPoints _ = True
230+
231+
mergeLastBy :: (a -> Rational) -> (Date -> Rational) -> CutoffType
232+
-> [TsPoint a] -> [Date] -> [Rational]
233+
mergeLastBy valOf fallback ct pts0 ds = go pts0 0 ds
234+
where
235+
go _ !prev [] = []
236+
go pts !prev (d:ds) =
237+
let (pts', prev') = advance pts prev d
238+
rest = case ds of
239+
(d2:_) | d2 < d -> map fallback ds
240+
_ -> go pts' prev' ds
241+
in prev' : rest
242+
243+
advance [] prev _ = ([], prev)
244+
advance allPts@(TsPoint pd pv : rest) prev d
245+
| within pd d = advance rest (valOf pv) d
246+
| otherwise = (allPts, prev)
247+
248+
within pd d = case ct of
249+
Inc -> pd <= d
250+
Exc -> pd < d
251+
252+
mergeFactorClosed :: Date -> (Date -> Rational) -> [TsPoint Rational]
253+
-> [Date] -> [Rational]
254+
mergeFactorClosed ed fallback pts0 ds = go pts0 Nothing ds
255+
where
256+
go _ _ [] = []
257+
go pts !prevM (d:ds) =
258+
let (pts', prevM') = advance pts prevM d
259+
value = case prevM' of
260+
Nothing -> 1.0
261+
Just v -> if d >= ed then 1.0 else v
262+
rest = case ds of
263+
(d2:_) | d2 < d -> map fallback ds
264+
_ -> go pts' prevM' ds
265+
in value : rest
266+
267+
advance [] prev _ = ([], prev)
268+
advance allPts@(TsPoint pd pv : rest) prev d
269+
| pd < d = advance rest (Just (toRational pv)) d
270+
| otherwise = (allPts, prev)
208271

209272
getTsVals :: Ts -> [Rational]
210273
getTsVals (FloatCurve ts) = [ v | (TsPoint d v) <- ts ]

‎test/MainTest.hs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ tests = testGroup "Tests" [AT.mortgageTests
100100
,UtilT.tsTest
101101
,UtilT.ts2Test
102102
,UtilT.ts3Test
103+
,UtilT.curveLookupLegacyTest
103104
,UtilT.dateVectorPatternTest
104105
,UtilT.paddingTest
105106
,UtilT.dateSliceTest

‎test/UT/UtilTest.hs‎

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{-# LANGUAGE ScopedTypeVariables #-}
22

33
module UT.UtilTest(daycountTests1,daycountTests2,daycountTests3,daycountTests4
4-
,tsTest,ts2Test,ts3Test,dateVectorPatternTest,paddingTest,dateSliceTest
4+
,tsTest,ts2Test,ts3Test,curveLookupLegacyTest,dateVectorPatternTest,paddingTest,dateSliceTest
55
,capTest,roundingTest,sliceTest,splitTsTest,tableTest,lastOftest,paySeqTest
66
,scaleListTest,mapUpdateTest,boundedParMapTest,resolveWorkerCapTest,forceMapValuesTest
77
,workerNumTest,serverConfigLimitTest,badRequestJSONTest
@@ -341,6 +341,121 @@ ts2Test =
341341
getValByDate testThresholdCurve Inc (toDate "20220310")
342342
]
343343

344+
-- | Legacy reference for the plural lookup. The fast path must match this
345+
-- exactly, including for descending/unsorted dates and unsorted curve points.
346+
legacyGetValByDates :: Ts -> CutoffType -> [Date] -> [Rational]
347+
legacyGetValByDates rc ct = map (getValByDate rc ct)
348+
349+
assertCurveLookup :: String -> Ts -> CutoffType -> [[Date]] -> IO ()
350+
assertCurveLookup label curve ct dss =
351+
mapM_ (\ds -> assertEqual (label ++ " / " ++ show ct ++ " / n=" ++ show (length ds))
352+
(legacyGetValByDates curve ct ds)
353+
(getValByDates curve ct ds)) dss
354+
355+
curveLookupLegacyTest :: TestTree
356+
curveLookupLegacyTest =
357+
let
358+
d0 = toDate "20220101"
359+
d1 = T.addDays 30 d0
360+
d2 = T.addDays 60 d0
361+
d3 = T.addDays 90 d0
362+
dBefore = T.addDays (-10) d0
363+
dAfter = T.addDays 200 d0
364+
365+
ratePts :: [TsPoint Rational]
366+
ratePts = [ TsPoint d0 (1 % 100)
367+
, TsPoint d1 (2 % 100)
368+
, TsPoint d1 (25 % 1000)
369+
, TsPoint d2 (3 % 100)
370+
, TsPoint d3 (4 % 100) ]
371+
372+
iratePts :: [TsPoint IRate]
373+
iratePts = [ TsPoint d0 0.01
374+
, TsPoint d1 0.02
375+
, TsPoint d1 0.025
376+
, TsPoint d2 0.03
377+
, TsPoint d3 0.04 ]
378+
379+
balPts :: [TsPoint Balance]
380+
balPts = [ TsPoint d0 100
381+
, TsPoint d1 200
382+
, TsPoint d2 300
383+
, TsPoint d3 400 ]
384+
385+
unsortedRatePts :: [TsPoint Rational]
386+
unsortedRatePts = [TsPoint d2 (3 % 100), TsPoint d0 (1 % 100), TsPoint d1 (2 % 100)]
387+
388+
unsortedIratePts :: [TsPoint IRate]
389+
unsortedIratePts = [TsPoint d2 0.03, TsPoint d0 0.01, TsPoint d1 0.02]
390+
391+
dateSets :: [[Date]]
392+
dateSets = [ []
393+
, [d0]
394+
, [dBefore]
395+
, [dAfter]
396+
, [d0, d1, d2, d3]
397+
, [d0, d0, d1, d1, d2, d3, d3]
398+
, [d1]
399+
, [d1, d2]
400+
, [d3, d2, d1, d0]
401+
, [d1, d0, d2, d3]
402+
, [dBefore, d0, d1, dAfter]
403+
, [T.addDays (toInteger i) d0 | i <- [0..999]]
404+
]
405+
406+
simpleCurves :: [(String, Ts)]
407+
simpleCurves =
408+
[ ("IRateCurve", IRateCurve iratePts)
409+
, ("FloatCurve", FloatCurve ratePts)
410+
, ("RatioCurve", RatioCurve ratePts)
411+
, ("BalanceCurve", BalanceCurve balPts)
412+
, ("LeftBalanceCurve", LeftBalanceCurve balPts)
413+
]
414+
415+
emptyCurves :: [Ts]
416+
emptyCurves =
417+
[ IRateCurve []
418+
, FloatCurve []
419+
, RatioCurve []
420+
, BalanceCurve []
421+
, LeftBalanceCurve []
422+
, ThresholdCurve []
423+
]
424+
in
425+
testGroup "getValByDates legacy compatibility"
426+
[ testCase "ascending, duplicate, descending and unsorted dates" $
427+
mapM_ (\(name, c) -> do
428+
assertCurveLookup name c Inc dateSets
429+
assertCurveLookup name c Exc dateSets) simpleCurves
430+
, testCase "unsorted curve points fall back exactly" $
431+
mapM_ (\(name, c) -> do
432+
assertCurveLookup name c Inc dateSets
433+
assertCurveLookup name c Exc dateSets)
434+
[ ("IRateCurve-unsorted", IRateCurve unsortedIratePts)
435+
, ("FloatCurve-unsorted", FloatCurve unsortedRatePts)
436+
]
437+
, testCase "FactorCurveClosed matches the legacy lookup" $ do
438+
assertCurveLookup "factor" (FactorCurveClosed ratePts d2) Exc dateSets
439+
assertCurveLookup "factor-unsorted" (FactorCurveClosed unsortedRatePts d2) Exc dateSets
440+
, testCase "ThresholdCurve falls back exactly" $ do
441+
assertCurveLookup "threshold" (ThresholdCurve ratePts) Inc dateSets
442+
assertCurveLookup "threshold" (ThresholdCurve ratePts) Exc dateSets
443+
, testCase "PricingCurve falls back exactly" $ do
444+
assertCurveLookup "pricing" (PricingCurve ratePts) Inc dateSets
445+
assertCurveLookup "pricing" (PricingCurve ratePts) Exc dateSets
446+
, testCase "empty curves" $ do
447+
mapM_ (\c -> do
448+
assertCurveLookup "empty" c Inc dateSets
449+
assertCurveLookup "empty" c Exc dateSets) emptyCurves
450+
assertCurveLookup "empty-factor" (FactorCurveClosed [] d2) Exc dateSets
451+
assertCurveLookup "empty-pricing" (PricingCurve []) Inc dateSets
452+
, testCase "empty dates always return []" $ do
453+
mapM_ (\(name, c) -> do
454+
assertEqual (name ++ " Inc") [] (getValByDates c Inc [])
455+
assertEqual (name ++ " Exc") [] (getValByDates c Exc [])) simpleCurves
456+
assertEqual "factor" [] (getValByDates (FactorCurveClosed ratePts d2) Exc [])
457+
]
458+
344459
-- ^ date pattern test
345460
dateVectorPatternTest =
346461
let

0 commit comments

Comments
 (0)