From b8c4db39334c6612bd966043d73eb8c1fcacb5b8 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sat, 9 May 2026 23:34:57 +0100 Subject: Make genEventID 5x as fast showIntAtBase wasn't inlined, so not specialised to Word64, so all the arithmetic went via typeclasses (inluding a number of fromIntegral @Word64 @Int, which went via Integer). --- src/Index.hs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Index.hs b/src/Index.hs index 148f314..5ac59be 100644 --- a/src/Index.hs +++ b/src/Index.hs @@ -4,6 +4,7 @@ {-# LANGUAGE ViewPatterns #-} module Index ( Index, + EventID, initIndex, indexGetEventsLinear, findEventIDLinear, @@ -388,10 +389,19 @@ genEventID (YMDHMS (YMD y m d) (HMS hh mm ss)) off let cast :: Integral a => a -> Word64 ; cast = fromIntegral num = (((((cast off * 5000 + cast y) * 12 + cast (m - 1)) * 31 + cast (d - 1)) * 24 + cast hh) * 60 + cast mm) * 60 + cast ss - in T.pack ('a' : showIntAtBase 62 base62char num "") + in T.pack ('a' : showBase62 num "") where base62char = chr . fromIntegral . (base62alphabet `BS.index`) base62alphabet = BS.pack (map (fromIntegral . ord) (['0'..'9'] ++ ['A'..'Z'] ++ ['a'..'z'])) + showBase62 :: Word64 -> ShowS + showBase62 topnum + | topnum == 0 = (base62char 0 :) + | otherwise = go topnum + where + go :: Word64 -> ShowS + go 0 = id + go n = let (q, r) = n `quotRem` 62 + in go q . (base62char (fromIntegral @Word64 @Int r) :) -- >>> parseEventID "a5d9CtBVX" -- Just (YMDHMS (YMD 2026 4 7) (HMS 18 56 55),123) -- cgit v1.3.1