Skip to content

Commit 832b2fa

Browse files
committed
Introduce SeqConstraintUnit to reduce possible overhead
The definition of `Extendss` that involves pairs: type Extendss p xs where Extendss p '[] = () Extendss p (x : xs) = (Extends p x, Extendss p xs) Would produce a constraint that looks like this: `((), ((), ((), ...)))` That may result in a runtime overhead if GHC would be incapable to perform cross-module inlining here (it would be) The new `SeqConstraintUnit` type family ensures that `Extends p x` reduces to the constraint unit and drops it after that entirely. Hence, produced constraint would be just `()`. Much better!
1 parent 79bea09 commit 832b2fa

File tree

5 files changed

+58
-7
lines changed

5 files changed

+58
-7
lines changed

generate-new/src/Render/Spec/Extends.hs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,18 @@ classes Spec {..} = do
187187
class PeekChain (xs :: [Type])
188188
class PokeChain (xs :: [Type])
189189
type family Extends (p :: [Type] -> Type) (x :: Type) :: Constraint where ..
190+
191+
-- | We don't really need constraint units produced by `Extends`, so this type
192+
-- family will ensure that it would reduce and drop the result
193+
--
194+
-- That will result in less overhead because `Extendss` reduces into a single
195+
-- contraint unit `()` instead of cons-list `((), ((), ()))` produced by `(,)`
196+
type family SeqConstraintUnit (a :: Constraint) (b :: Constraint) :: Constraint where
197+
SeqConstraintUnit () b = b
198+
190199
type family Extendss (p :: [Type] -> Type) (xs :: [Type]) :: Constraint where
191200
Extendss p '[] = ()
192-
Extendss p (x : xs) = (Extends p x, Extendss p xs)
201+
Extendss p (x : xs) = Extends p x `SeqConstraintUnit` Extendss p xs
193202
type family Chain (xs :: [a]) = (r :: a) | r -> xs where
194203
Chain '[] = ()
195204
Chain (x:xs) = (x, Chain xs)
@@ -362,9 +371,17 @@ classes Spec {..} = do
362371
infixr 7 :&
363372
\{-# complete (:&) #-}
364373

374+
-- | We don't really need constraint units produced by `Extends`, so this type
375+
-- family will ensure that it would reduce and drop the result
376+
--
377+
-- That will result in less overhead because `Extendss` reduces into a single
378+
-- contraint unit `()` instead of cons-list `((), ((), ()))` produced by `(,)`
379+
type family SeqConstraintUnit (a :: Constraint) (b :: Constraint) :: Constraint where
380+
SeqConstraintUnit () b = b
381+
365382
type family Extendss (p :: [Type] -> Type) (xs :: [Type]) :: Constraint where
366383
Extendss p '[] = ()
367-
Extendss p (x : xs) = (Extends p x, Extendss p xs)
384+
Extendss p (x : xs) = Extends p x `SeqConstraintUnit` Extendss p xs
368385

369386
class PokeChain es where
370387
withChain :: Chain es -> (Ptr (Chain es) -> IO a) -> IO a

openxr/src/OpenXR/CStruct/Extends.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,17 @@ pattern e :& es = (e, es)
583583
infixr 7 :&
584584
{-# complete (:&) #-}
585585

586+
-- | We don't really need constraint units produced by `Extends`, so this type
587+
-- family will ensure that it would reduce and drop the result
588+
--
589+
-- That will result in less overhead because `Extendss` reduces into a single
590+
-- contraint unit `()` instead of cons-list `((), ((), ()))` produced by `(,)`
591+
type family SeqConstraintUnit (a :: Constraint) (b :: Constraint) :: Constraint where
592+
SeqConstraintUnit () b = b
593+
586594
type family Extendss (p :: [Type] -> Type) (xs :: [Type]) :: Constraint where
587595
Extendss p '[] = ()
588-
Extendss p (x : xs) = (Extends p x, Extendss p xs)
596+
Extendss p (x : xs) = Extends p x `SeqConstraintUnit` Extendss p xs
589597

590598
class PokeChain es where
591599
withChain :: Chain es -> (Ptr (Chain es) -> IO a) -> IO a

openxr/src/OpenXR/CStruct/Extends.hs-boot

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,19 @@ instance FromCStruct BaseOutStructure
3131

3232
class PeekChain (xs :: [Type])
3333
class PokeChain (xs :: [Type])
34-
type family Extends (p :: [Type] -> Type) (x :: Type) :: Constraint
34+
type family Extends (p :: [Type] -> Type) (x :: Type) :: Constraint where ..
35+
36+
-- | We don't really need constraint units produced by `Extends`, so this type
37+
-- family will ensure that it would reduce and drop the result
38+
--
39+
-- That will result in less overhead because `Extendss` reduces into a single
40+
-- contraint unit `()` instead of cons-list `((), ((), ()))` produced by `(,)`
41+
type family SeqConstraintUnit (a :: Constraint) (b :: Constraint) :: Constraint where
42+
SeqConstraintUnit () b = b
43+
3544
type family Extendss (p :: [Type] -> Type) (xs :: [Type]) :: Constraint where
3645
Extendss p '[] = ()
37-
Extendss p (x : xs) = (Extends p x, Extendss p xs)
46+
Extendss p (x : xs) = Extends p x `SeqConstraintUnit` Extendss p xs
3847
type family Chain (xs :: [a]) = (r :: a) | r -> xs where
3948
Chain '[] = ()
4049
Chain (x:xs) = (x, Chain xs)

src/Vulkan/CStruct/Extends.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2952,9 +2952,17 @@ pattern e :& es = (e, es)
29522952
infixr 7 :&
29532953
{-# complete (:&) #-}
29542954

2955+
-- | We don't really need constraint units produced by `Extends`, so this type
2956+
-- family will ensure that it would reduce and drop the result
2957+
--
2958+
-- That will result in less overhead because `Extendss` reduces into a single
2959+
-- contraint unit `()` instead of cons-list `((), ((), ()))` produced by `(,)`
2960+
type family SeqConstraintUnit (a :: Constraint) (b :: Constraint) :: Constraint where
2961+
SeqConstraintUnit () b = b
2962+
29552963
type family Extendss (p :: [Type] -> Type) (xs :: [Type]) :: Constraint where
29562964
Extendss p '[] = ()
2957-
Extendss p (x : xs) = (Extends p x, Extendss p xs)
2965+
Extendss p (x : xs) = Extends p x `SeqConstraintUnit` Extendss p xs
29582966

29592967
class PokeChain es where
29602968
withChain :: Chain es -> (Ptr (Chain es) -> IO a) -> IO a

src/Vulkan/CStruct/Extends.hs-boot

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,18 @@ instance FromCStruct BaseOutStructure
3232
class PeekChain (xs :: [Type])
3333
class PokeChain (xs :: [Type])
3434
type family Extends (p :: [Type] -> Type) (x :: Type) :: Constraint where ..
35+
36+
-- | We don't really need constraint units produced by `Extends`, so this type
37+
-- family will ensure that it would reduce and drop the result
38+
--
39+
-- That will result in less overhead because `Extendss` reduces into a single
40+
-- contraint unit `()` instead of cons-list `((), ((), ()))` produced by `(,)`
41+
type family SeqConstraintUnit (a :: Constraint) (b :: Constraint) :: Constraint where
42+
SeqConstraintUnit () b = b
43+
3544
type family Extendss (p :: [Type] -> Type) (xs :: [Type]) :: Constraint where
3645
Extendss p '[] = ()
37-
Extendss p (x : xs) = (Extends p x, Extendss p xs)
46+
Extendss p (x : xs) = Extends p x `SeqConstraintUnit` Extendss p xs
3847
type family Chain (xs :: [a]) = (r :: a) | r -> xs where
3948
Chain '[] = ()
4049
Chain (x:xs) = (x, Chain xs)

0 commit comments

Comments
 (0)