Skip to content
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

Asymptotics are linear in a few places where they should be logarithmic. #3

Open
HuwCampbell opened this issue Jan 30, 2024 · 2 comments

Comments

@HuwCampbell
Copy link

HuwCampbell commented Jan 30, 2024

I was just having a look and noticed this.

It appears that the asymptotics for a few operations are worse that they can and should be.

For example:

insert :: (Ord r, Ord c) => r -> c -> a -> Table r c a -> Table r c a
insert r c a (Table (rm, cm, _)) = fromMaps' r c rm' cm'
  where
    rm' = Map.alter f r rm
    cm' = Map.alter g c cm
    f = Just . maybe (Map.singleton c a) (Map.insert c a)
    g = Just . maybe (Map.singleton r a) (Map.insert r a)

Kind of looks fine, and should be O(ln(c) · ln(r)) or something, but, the size' function called in fromMaps' clearly looks at every key in one of the maps.

So it's actually more like O(r)

I think this could be pretty easily fixed by using insertLookup and alterF.

@HuwCampbell
Copy link
Author

Right. This usually just creates a thunk which is probably GC'd.

@zliu41 zliu41 reopened this Jan 30, 2024
@zliu41
Copy link
Owner

zliu41 commented Jan 30, 2024

@HuwCampbell Thanks for raising this. I think you replied to and closed the wrong issue.

I don't remember why I decided to re-calculate the size, but it seems indeed unnecessary. I'll take a closer look soon.

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

No branches or pull requests

2 participants