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

Benchmark Dictionary implementation in Swarm #2191

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open

Conversation

xsebek
Copy link
Member

@xsebek xsebek commented Oct 20, 2024

  • add dictionary benchmarks in Swarm language
  • add more tests
  • fix discovered bugs

You can test it with:

run "example/recursive-containers.sw";
benchmark_tree
Screenshot
Screenshot 2024-10-20 at 5 10 06 PM

@xsebek xsebek requested review from kostmo and byorgey October 20, 2024 15:11
@xsebek
Copy link
Member Author

xsebek commented Oct 20, 2024

@byorgey without import you can copy the relevant part of the header and use it like this:

tydef Maybe a = Unit + a end

tydef IDict d k v =
  [ empty: d
  , insert: k -> v -> d -> d
  , delete: k -> d -> d
  , get: k -> d -> Maybe v
  , contains: k -> d -> Bool
  , pretty: d -> Text
  ]
end
def undefined_dict : any -> IDict any k v =\empty.
    [empty=empty, insert=\x.undefined, delete=\x.undefined, get=\x.undefined, contains=\x.undefined, pretty=\x.undefined]
end

tydef RBTree k = rec b. Maybe [red: Bool, k: k, l: b, r: b] end
tydef Dict k v = RBTree (k * v) end
def tree_dict : IDict (Dict k v) k v = undefined_dict (inl ()) end

run "recursive-containers.sw";
log $ tree_dict.pretty $ tree_dict.insert 1 "b" $ tree_dict.insert 0 "a" $ tree_dict.insert 4 "e" tree_dict.empty

I hoped the records would make this shorter, but they are values, so I can not just declare them undefined.

EDIT: I wonder if IDict (Dict k v) k v is not too obscure - non Java/C# people might not understand the I means interface convention 🤔

end

tydef FlatSet a = List a end
def flat_set : ISet (FlatSet a) a = undefined_set (inl ()) end
Copy link
Member

Choose a reason for hiding this comment

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

What's the purpose of defining flat_set et al as records with lots of undefined at first, and then later redefining them?

Copy link
Member Author

@xsebek xsebek Oct 20, 2024

Choose a reason for hiding this comment

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

It's an overview, as it can be hard to spot between the Red-Black Tree implementation and tests.

It also works as a "header" of sorts, if you wanted to copy paste part of it in a scenario definition/other Swarm script and then run this file.

def benchmark: Int -> s -> (s -> s) -> Cmd (Int * (Int * Int)) = \times.\s.\act.
let min = \x.\y. if (x > y) {y} {x} in
let max = \x.\y. if (x > y) {x} {y} in
let runM: (Int * Maybe (Int * Int)) -> s -> Int -> Cmd (Int * Maybe (Int * Int)) = \acc.\s.\n.
Copy link
Member

Choose a reason for hiding this comment

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

Scoped type variables! 😍

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.

Dictionaries
2 participants