diff options
| author | Tom Smeding <tom@tomsmeding.com> | 2026-05-10 00:34:57 +0200 |
|---|---|---|
| committer | Tom Smeding <tom@tomsmeding.com> | 2026-05-10 00:34:57 +0200 |
| commit | b8c4db39334c6612bd966043d73eb8c1fcacb5b8 (patch) | |
| tree | 616f7ecb69d9c435e9810ec72d6146af9451be06 | |
| parent | a63a096d075797f12ed614d5029e16f55a8bc769 (diff) | |
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).
| -rw-r--r-- | src/Index.hs | 12 |
1 files changed, 11 insertions, 1 deletions
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) |
