-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use functions from newer primitive and primitive-unlifted #63
Changes from 1 commit
8064146
ae9cc11
2569284
5ea2ed0
95e48d0
6c4e8a5
709735d
08eebb6
a8b1607
3e43cce
546847a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ module Data.Primitive.Contiguous.Class | |
, Always | ||
) where | ||
|
||
import Data.Primitive hiding (fromList, fromListN) | ||
import Data.Primitive | ||
import Data.Primitive.Contiguous.Shim | ||
import Data.Primitive.Unlifted.Array | ||
import Prelude hiding | ||
|
@@ -569,7 +569,12 @@ class (Contiguous arr) => ContiguousU arr where | |
-- | The unifted version of the mutable array type (i.e. eliminates an indirection through a thunk). | ||
type UnliftedMut arr = (r :: Type -> Type -> TYPE UnliftedRep) | r -> arr | ||
|
||
-- | Resize an array into one with the given size. | ||
-- | Resize an array into one with the given size. If the array is grown, | ||
-- then reading from any newly introduced element before writing to it is undefined behavior. | ||
-- The current behavior is that anything backed by @MutableByteArray#@ ends | ||
andrewthad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
-- uninitialized memory at these indices. But for @SmallMutableArray@, these | ||
andrewthad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
-- are set to an error thunk, so reading from them and forcing the result | ||
-- causes the program to crash. | ||
andrewthad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
resize :: | ||
andrewthad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(PrimMonad m, Element arr b) => | ||
Mutable arr (PrimState m) b -> | ||
|
@@ -807,7 +812,7 @@ instance Contiguous SmallArray where | |
{-# INLINE size #-} | ||
size = sizeofSmallArray | ||
{-# INLINE sizeMut #-} | ||
sizeMut = (\x -> pure $! sizeofSmallMutableArray x) | ||
sizeMut = getSizeofSmallMutableArray | ||
{-# INLINE thaw_ #-} | ||
thaw_ = thawSmallArray | ||
{-# INLINE equals #-} | ||
|
@@ -872,15 +877,19 @@ instance Contiguous SmallArray where | |
{-# INLINE copyMut_ #-} | ||
copyMut_ = copySmallMutableArray | ||
{-# INLINE replicateMut #-} | ||
replicateMut = replicateSmallMutableArray | ||
replicateMut = newSmallArray | ||
{-# INLINE run #-} | ||
run = runSmallArrayST | ||
{-# INLINE shrink #-} | ||
shrink !arr !n = do | ||
shrinkSmallMutableArray arr n | ||
pure arr | ||
Comment on lines
+865
to
+868
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent! I'd do these 4 lines for PrimArray also, using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. I've done this in 95e48d0. |
||
|
||
instance ContiguousU SmallArray where | ||
type Unlifted SmallArray = SmallArray# | ||
type UnliftedMut SmallArray = SmallMutableArray# | ||
{-# INLINE resize #-} | ||
resize = resizeSmallArray | ||
resize !arr !n = resizeSmallMutableArray arr n resizeSmallMutableArrayUninitializedElement | ||
{-# INLINE unlift #-} | ||
unlift (SmallArray x) = x | ||
{-# INLINE unliftMut #-} | ||
|
@@ -926,7 +935,7 @@ instance Contiguous PrimArray where | |
lengthMut <- sizeMut baseMut | ||
pure MutableSlice {offsetMut = 0, lengthMut, baseMut = unliftMut baseMut} | ||
{-# INLINE freeze_ #-} | ||
freeze_ = freezePrimArrayShim | ||
freeze_ = freezePrimArray | ||
{-# INLINE unsafeFreeze #-} | ||
unsafeFreeze = unsafeFreezePrimArray | ||
{-# INLINE thaw_ #-} | ||
|
@@ -936,9 +945,9 @@ instance Contiguous PrimArray where | |
{-# INLINE copyMut_ #-} | ||
copyMut_ = copyMutablePrimArray | ||
{-# INLINE clone_ #-} | ||
clone_ = clonePrimArrayShim | ||
clone_ = clonePrimArray | ||
{-# INLINE cloneMut_ #-} | ||
cloneMut_ = cloneMutablePrimArrayShim | ||
cloneMut_ = cloneMutablePrimArray | ||
{-# INLINE equals #-} | ||
equals = (==) | ||
{-# INLINE null #-} | ||
|
@@ -1250,6 +1259,20 @@ instance Contiguous (UnliftedArray_ unlifted_a) where | |
unsafeFreezeUnliftedArray m | ||
{-# INLINE run #-} | ||
run = runUnliftedArrayST | ||
{-# INLINE shrink #-} | ||
shrink !arr !n = do | ||
-- See Note [Shrinking Unlifted Arrays] | ||
cloneMutableUnliftedArray arr 0 n | ||
{-# INLINE unsafeShrinkAndFreeze #-} | ||
unsafeShrinkAndFreeze !arr !n = | ||
-- See Note [Shrinking Unlifted Arrays] | ||
freezeUnliftedArray arr 0 n | ||
andrewthad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
-- Note [Shrinking Unlifted Arrays] | ||
-- ================================ | ||
-- This implementation copies the array rather than freezing it in place. | ||
-- But at least it is able to avoid assigning all of the elements to | ||
-- a nonsense value before replacing them with memcpy. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, very clever! But I think you want to apply this to lifted arrays also, right? Including defining a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. Added this change in 95e48d0. |
||
|
||
newtype UnliftedArray## (u :: TYPE UnliftedRep) (a :: Type) | ||
= UnliftedArray## (Exts.Array# u) | ||
|
@@ -1269,3 +1292,7 @@ instance ContiguousU (UnliftedArray_ unlifted_a) where | |
lift (UnliftedArray## x) = UnliftedArray (UnliftedArray# x) | ||
{-# INLINE liftMut #-} | ||
liftMut (MutableUnliftedArray## x) = MutableUnliftedArray (MutableUnliftedArray# x) | ||
|
||
resizeSmallMutableArrayUninitializedElement :: a | ||
{-# noinline resizeSmallMutableArrayUninitializedElement #-} | ||
resizeSmallMutableArrayUninitializedElement = errorWithoutStackTrace "uninitialized element of resizeSmallMutableArray" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd change line 142 to:
-- | Resize a mutable array without growing it. It may be shrunk in place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd still like line 142 changed (to add " It may be shrunk in place."). :)