1
1
mirror of https://github.com/juspay/jrec.git synced 2024-08-15 13:20:50 +03:00

Add unsafeGet function and re-implement toTuple with it

An attempt to improve JRec.Tuple compile time.
This commit is contained in:
kamoii 2020-10-28 20:11:31 +09:00
parent 41fb875256
commit 433713feec
3 changed files with 145 additions and 137 deletions

8
bin/genrecord Normal file → Executable file
View File

@ -42,12 +42,10 @@ genInstance i =
i
toTuple =
format
"toTuple r = let {} = R.getFields r in unsafeCoerce {}"
exprList
exprTuple
"toTuple r = let n = R.FldProxy :: R.FldProxy \"\" in unsafeCoerce {}"
exprGetTuple
constraints =
tupleF $
format "R.RecApply {} {} R.NoConstraint" typeList typeList :
[format "n{} ~ n{}'" j j :: Builder | j <- [1 .. i]]
++ [format "v{} ~ v{}'" j j :: Builder | j <- [1 .. i]]
in format
@ -71,3 +69,5 @@ genInstance i =
exprList = listF ["f" <> show j | j <- [1 .. i]]
-- R.unsafeRCons f1 $ R.unsafeRCons f2 $
consApps = mconcat [format "R.unsafeRCons f{} =<< " j :: Builder | j <- [1 .. i]]
-- (n R.:= R.unsafeGet 0 r, n R.:= R.unsafeGet 1 r)
exprGetTuple = tupleF [format "n R.:= R.unsafeGet {} r" j :: Builder | j <- [0 .. i-1]]

View File

@ -187,6 +187,20 @@ unsafeRCons (_ := val) (MkRec vec#) =
!(I# size#) = fromIntegral $ natVal' (proxy# :: Proxy# size)
{-# INLINE unsafeRCons #-}
-- | Get the i-th value as Any unsafely.
-- No boundary check. Also ther caller is responsible for coerceing to correct type.
-- Intented for internal use only.
unsafeGet ::
forall lts.
Int ->
Rec lts ->
Any
unsafeGet (I# index#) (MkRec vec#) =
let size# = sizeofSmallArray# vec#
in case indexSmallArray# vec# (size# -# index# -# 1#) of
(# a# #) -> a#
{-# INLINE unsafeGet #-}
-- Not in superrecord
recCopy :: forall lts rts. RecCopy lts lts rts => Rec lts -> Rec rts
recCopy r@(MkRec vec#) =
@ -298,15 +312,9 @@ get ::
FldProxy l ->
Rec lts ->
v
get _ (MkRec vec#) =
let !(I# index#) =
fromIntegral (natVal' (proxy# :: Proxy# (RecTyIdxH 0 l lts)))
size# = sizeofSmallArray# vec#
anyVal :: Any
anyVal =
case indexSmallArray# vec# (size# -# index# -# 1#) of
(# a# #) -> a#
in unsafeCoerce# anyVal
get _ r =
let !index = fromIntegral (natVal' (proxy# :: Proxy# (RecTyIdxH 0 l lts)))
in unsafeCoerce $ unsafeGet index r
{-# INLINE get #-}
-- | Alias for 'get'

File diff suppressed because one or more lines are too long