-
-
Notifications
You must be signed in to change notification settings - Fork 18
[#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
Conversation
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.
This is a great change!
src/Data/TypeRepMap/Internal.hs
Outdated
let arrOrigin = fromList l | ||
arrResult <- thawArray arrOrigin 0 n | ||
_ <- go n 0 0 arrResult arrOrigin | ||
toList <$> freezeArray arrResult 0 n |
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.
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
.
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.
yes, same goes for the adjust
patch (I wanted to comment but it was merged already)
src/Data/TypeRepMap/Internal.hs
Outdated
let n = length l | ||
let arrOrigin = fromList l | ||
arrResult <- thawArray arrOrigin 0 n | ||
_ <- go n 0 0 arrResult arrOrigin |
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.
() <$
😏
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.
it's called void
(from Control.Monad
)
src/Data/TypeRepMap/Internal.hs
Outdated
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 |
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.
Since you've precalculated length of the array, you can use fromListN
function to create arrOrigin
. This will be more efficient:
src/Data/TypeRepMap/Internal.hs
Outdated
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 = |
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 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 = ...
No description provided.