Skip to content

Fix a few bugs in arrays - #1259

Merged
harendra-kumar merged 8 commits into
masterfrom
writeN-test
Oct 9, 2021
Merged

harendra-kumar merged 8 commits into
masterfrom
writeN-test

Conversation

@harendra-kumar

Copy link
Copy Markdown
Member

No description provided.

@harendra-kumar harendra-kumar changed the title Add test for writeN fold applied multiple times Fix a few bugs in arrays Oct 6, 2021
return $ Array
{ aStart = newPtr
, aEnd = pNew `plusPtr` size
, aEnd = pNew `plusPtr` (size - (size `mod` elemSize))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since assert (size mod elemSize == 0) (return ()) is before so (size - (size mod elemSize)) must be == size.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

asserts are only for debug. In case this does not hold true we would still be fine in production.

writeN :: MonadIO m => Int -> Fold m a (Array a)
writeN limit = Fold step initial extract
where
writeN len = Fold step initial extract

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous implementation would not work for negative length. Anything else I'm missing?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    | i == limit = fmap FL.Done $ extract st

Discards the input x.

Comment on lines +88 to +90
if len > i1
then return $ FL.Partial st
else fmap FL.Done $ extract st

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better to check if the length is negative in initial once instead of checking this on every step?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean by "length is negative". We are checking "len > i1" here which means we have to take more elements.

-- XXX note that shirkToFit does not maintain alignment, in case we are
-- using aligned allocation.
extract = return -- liftIO . shrinkToFit
writeNAllocWith alloc n = FL.take n (writeNUnsafeWith alloc n)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check perf diff?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, there is no diff.

Comment on lines +171 to +176
next marr i = do
let i1 = i + 1
st = Tuple' marr i1
if len > i1
then return $ FL.Partial st
else fmap FL.Done $ extract st

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments as above

Comment on lines +113 to +118
next marr i = do
let i1 = i + 1
st = Tuple' marr i1
if len > i1
then return $ FL.Partial st
else fmap FL.Done $ extract st

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments as above

@harendra-kumar

Copy link
Copy Markdown
Member Author

Benchmarks master vs PR:

Data.Array.Foreign(cpuTime)
Benchmark                                                                default(0)(μs) default(1) - default(0)(%)
------------------------------------------------------------------------ -------------- --------------------------
All.Data.Array.Foreign/o-1-space.generation.fromList . intFromTo                1574.28                     +20.78
All.Data.Array.Foreign/o-1-space.generation.write . intFromTo                    217.91                      +7.49
All.Data.Array.Foreign/o-1-space.generation.writeN . unfoldr                     155.09                      +5.03
All.Data.Array.Foreign/o-n-space.elimination.writeN                              169.65                      +4.58
All.Data.Array.Foreign/o-1-space.generation.writeN . fromList                   1656.12                      +3.32
All.Data.Array.Foreign/o-1-space.generation.read                              214401.00                      +2.17
All.Data.Array.Foreign/o-1-space.generation.show                               38515.30                      +1.30
All.Data.Array.Foreign/o-1-space.generation.writeN . IsString.fromString        1245.05                      +0.77
All.Data.Array.Foreign/o-1-space.elimination.length . IsList.toList              189.97                      +0.36
All.Data.Array.Foreign/o-1-space.generation.writeN . intFromTo                   142.61                      -0.26
All.Data.Array.Foreign/o-1-space.elimination.writeLastN.1                        113.09                      -0.95
All.Data.Array.Foreign/o-1-space.generation.writeN . IsList.fromList            1603.76                      -2.57
All.Data.Array.Foreign/o-n-space.elimination.writeLastN.Max                      260.55                      -2.82
All.Data.Array.Foreign/o-1-space.elimination.id                                  146.57                      -2.96
All.Data.Array.Foreign/o-1-space.transformationX4.map                            624.46                     -10.08
All.Data.Array.Foreign/o-1-space.transformationX4.scanl'                         642.61                     -12.19
All.Data.Array.Foreign/o-1-space.transformation.scanl1'                          265.63                     -13.02
All.Data.Array.Foreign/o-1-space.transformation.scanl'                           270.54                     -16.45
All.Data.Array.Foreign/o-1-space.transformation.map                              263.87                     -16.64
All.Data.Array.Foreign/o-1-space.elimination.min                                 245.01                     -18.03
All.Data.Array.Foreign/o-1-space.elimination.writeLastN.10                       144.81                     -20.83
All.Data.Array.Foreign/o-1-space.elimination.<                                   207.64                     -21.08
All.Data.Array.Foreign/o-1-space.elimination./=                                  180.20                     -21.42
All.Data.Array.Foreign/o-1-space.elimination.==                                  180.20                     -23.14
All.Data.Array.Foreign/o-1-space.transformationX4.scanl1'                        638.43                     -24.72
All.Data.Array.Foreign/o-1-space.elimination.foldl'                              156.63                     -29.22
All.Data.Array.Foreign/o-1-space.elimination.read                                143.82                     -32.84
All.Data.Array.Foreign/o-1-space.elimination.toStreamRev                         143.96                     -33.16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants