Skip to content

Improve instance Ixed (HashMap k a) in lens #541

@sjakobi

Description

@sjakobi

Can we expose the tooling around LookupRes from proper public modules, so we can improve the instance like this?

 type instance IxValue (HashMap k a) = a
 instance (Eq k, Hashable k) => Ixed (HashMap k a) where
-  ix k f m = case HashMap.lookup k m of
-     Just v  -> f v <&> \v' -> HashMap.insert k v' m
-     Nothing -> pure m
+  ix k f m = case HashMap.Internal.lookupRecordCollision h k m of
+     HashMap.Internal.Present v i ->
+       f v <&> \v' -> HashMap.Internal.insertKeyExists i h k v' m
+     HashMap.Internal.Absent -> pure m
+    where h = HashMap.Internal.hash k
   {-# INLINE ix #-}

For now, I suspect that even an implementation via ixAt (i.e. alterF) might be more efficient than the current one.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions