Skip to content

[#30] Rewrite fromSortedList to use arrays #51

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

Merged
merged 3 commits into from
Aug 20, 2018

Conversation

vrom911
Copy link
Member

@vrom911 vrom911 commented Aug 20, 2018

No description provided.

Copy link
Contributor

@chshersh chshersh left a comment

Choose a reason for hiding this comment

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

This is a great change!

let arrOrigin = fromList l
arrResult <- thawArray arrOrigin 0 n
_ <- go n 0 0 arrResult arrOrigin
toList <$> freezeArray arrResult 0 n
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like you can use unsafeFreezeArray here:

Both thawArray and freezeArray functions create a copy of an array by cloning, as I can see from the documentation. So in most cases one of the two (freeze or thaw) can be unsafe to make implementation even more efficient. In our case we can't use unsafeThaw because we actually use arrOrigin after thaw.

Copy link
Collaborator

Choose a reason for hiding this comment

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

yes, same goes for the adjust patch (I wanted to comment but it was merged already)

let n = length l
let arrOrigin = fromList l
arrResult <- thawArray arrOrigin 0 n
_ <- go n 0 0 arrResult arrOrigin
Copy link
Contributor

Choose a reason for hiding this comment

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

() <$ 😏

Copy link
Collaborator

Choose a reason for hiding this comment

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

it's called void (from Control.Monad)

fromSortedList l = IM.elems $ fst $ go 0 0 mempty (IM.fromList $ zip [0..] l)
fromSortedList l = runST $ do
let n = length l
let arrOrigin = fromList l
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you've precalculated length of the array, you can use fromListN function to create arrOrigin. This will be more efficient:

if i >= IM.size vector
then (result, first)
go :: Int -> Int -> Int -> MutableArray s a -> Array a -> ST s Int
go n i first result vector =
Copy link
Contributor

Choose a reason for hiding this comment

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

I see that arguments n, result and vector are always passed to go function without changes. So it makes sense to me to rearrange and rename argument and introduce one more where function to make implementation more clean and robust and efficient:

go len result origin = loop  -- or destination/source (dst/src) instead of `result/vector`
  where
    loop = ...

@chshersh chshersh merged commit 32ebb33 into master Aug 20, 2018
@chshersh chshersh deleted the vrom911/30-remove-containers branch August 20, 2018 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants