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

Go-style interfaces for Gerbil #723

Merged
merged 6 commits into from
Jul 15, 2023
Merged

Go-style interfaces for Gerbil #723

merged 6 commits into from
Jul 15, 2023

Conversation

vyzo
Copy link
Collaborator

@vyzo vyzo commented Jul 14, 2023

This adds an :std/interface library module that implements Go-style interfaces.
Interface instances package an object with its method implementations flat, so that we can dispatch without lookup.

TBD:

  • unit test
  • docs

Here is an example:

(interface Operation
  (start!)
  (apply x)
  (finish!))

(defstruct LinearAccumulator (state a b))

(defmethod {apply LinearAccumulator}
  (lambda (self x)
    (with ((LinearAccumulator state a b) self)
      (set! (LinearAccumulator-state self)
         (+ state (* a x) b)))))

(defmethod {start! LinearAccumulator}
  (lambda (self)
    (set! (LinearAccumulator-state self) 0)))

(defmethod {finish! LinearAccumulator}
  LinearAccumulator-state)


;; implementation using vanilla method dispatch
(def (fold-accumulator/vanilla acc iters)
  {start! acc}
  (for (x (in-range iters))
    {apply acc x})
  {finish! acc})

;; implementation using the Operation interface
(def (fold-accumulator/interface acc iters)
  (let (op (Operation acc))
    (Operation-start! op)
    (for (x (in-range iters))
      (Operation-apply op x))
    (Operation-finish! op)))

And here are some timings for 1M iterations, after compiling the above code:

> (import :tmp/interface-example)
> (def acc (LinearAccumulator #f 2 3))
> (time (fold-accumulator/vanilla acc 1000000))
(time (tmp/interface-example#fold-accumulator/vanilla acc (##quote 1000000)))
    0.046875 secs real time
    0.045979 secs cpu time (0.045979 user, 0.000000 system)
    no collections
    no bytes allocated
    no minor faults
    no major faults
1000002000000
> (time (fold-accumulator/interface acc 1000000))
(time (tmp/interface-example#fold-accumulator/interface acc (##quote 1000000)))
    0.019163 secs real time
    0.018647 secs cpu time (0.018647 user, 0.000000 system)
    no collections
    1952 bytes allocated
    no minor faults
    no major faults
1000002000000
> (time (fold-accumulator/interface acc 1000000))
(time (tmp/interface-example#fold-accumulator/interface acc (##quote 1000000)))
    0.017777 secs real time
    0.017372 secs cpu time (0.017372 user, 0.000000 system)
    no collections
    1072 bytes allocated
    no minor faults
    no major faults
1000002000000

@vyzo vyzo requested a review from fare July 14, 2023 19:58
@vyzo vyzo requested review from ober and drewc July 15, 2023 09:56
Copy link
Collaborator

@ober ober left a comment

Choose a reason for hiding this comment

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

lgtm

@vyzo
Copy link
Collaborator Author

vyzo commented Jul 15, 2023

Fixed some edge cases with default arguments from mixins, will merge once it's green.

@vyzo vyzo merged commit 204ea3e into master Jul 15, 2023
@vyzo vyzo deleted the interfaces branch July 15, 2023 14:30
@vyzo vyzo added this to the Gerbil18 milestone Jul 16, 2023
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.

2 participants