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

Interface Specializers #1127

Merged
merged 4 commits into from
Feb 20, 2024
Merged

Interface Specializers #1127

merged 4 commits into from
Feb 20, 2024

Conversation

vyzo
Copy link
Collaborator

@vyzo vyzo commented Feb 20, 2024

On top of ##1126

And so it begins... the compiler generates specializers for all bound methods that could benefit from it, and interface prototype creation plugs to it, with wondrous performance results for certain programs.

Here is an example:

(defclass A (x y))
(defclass (B A) (z))

(defmethod {linear A}
  (lambda (self w)
    (fx+ (fx* (A-x self) w) (A-y self))))

(defmethod {bilinear-combination B}
  (lambda (self w z)
    {self.bilinear {self.linear w} z}))

(defmethod {bilinear B}
  (lambda (self lc z)
    (fx+ lc (fx* (B-z self) z))))

(interface Combinator
  (bilinear-combination w z))

(def (run iters)
  (let (instance (Combinator (B x: 1 y: 2 z: 3)))
    (for (i (in-range iters))
      (let (result (&Combinator-bilinear-combination instance 4 5))
        (unless (= result 21)
          (error "bad result" result: result expected: 21))))))

(def (main iters)
  (let (iters (string->number iters))
    (time (run iters))))

With gxc master:

$ gxc -exe -o /tmp/ispec-bench -O src/gerbil/test/interface-specialization-bench.ss
/tmp/gxc.1708454081.817515/test__interface-specialization-bench.scm:
/tmp/ispec-bench__exe.scm:
/tmp/gxc.1708454081.817515/test__interface-specialization-bench.c:
/tmp/ispec-bench__exe.c:
/tmp/ispec-bench__exe_.c:
$ /tmp/ispec-bench 1000000
(time (let () (declare (not safe)) (test/interface-specialization-bench#run _iters79_)))
    0.215345 secs real time
    0.215332 secs cpu time (0.211338 user, 0.003994 system)
    20 collections accounting for 0.016191 secs real time (0.015828 user, 0.000356 system)
    159892208 bytes allocated
    671 minor faults
    no major faults
    562301090 cpu cycles

With the specializers:

$ ./build.sh env gxc -exe -o /tmp/ispec-bench -O gerbil/test/interface-specialization-bench.ss
/tmp/gxc.1708454105.0922964/test__interface-specialization-bench.scm:
/tmp/ispec-bench__exe.scm:
/tmp/gxc.1708454105.0922964/test__interface-specialization-bench.c:
/tmp/ispec-bench__exe.c:
/tmp/ispec-bench__exe_.c:
[*] Done
$ /tmp/ispec-bench 1000000
(time (let () (declare (not safe)) (test/interface-specialization-bench#run _iters79_)))
    0.010587 secs real time
    0.010587 secs cpu time (0.010586 user, 0.000001 system)
    no collections
    1408 bytes allocated
    no minor faults
    no major faults
    27638154 cpu cycles

20x, not bad huh?

Basically all the dynamic dispatch call cost of the MOP for self references (slots or methods) has disappeared.

@vyzo vyzo requested review from fare, drewc and a team February 20, 2024 18:40
Copy link
Collaborator

@fare fare left a comment

Choose a reason for hiding this comment

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

LGTM

@fare
Copy link
Collaborator

fare commented Feb 20, 2024

I'd like to see more documentation comments and type signature comments above all functions everywhere.

Base automatically changed from interface-spin-lock to master February 20, 2024 20:24
@vyzo
Copy link
Collaborator Author

vyzo commented Feb 20, 2024

rebased on master.

Copy link

netlify bot commented Feb 20, 2024

Deploy Preview for elastic-ritchie-8f47f9 ready!

Name Link
🔨 Latest commit 8a7f9bc
🔍 Latest deploy log https://app.netlify.com/sites/elastic-ritchie-8f47f9/deploys/65d50dbe842c170008ae8de9
😎 Deploy Preview https://deploy-preview-1127--elastic-ritchie-8f47f9.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@vyzo
Copy link
Collaborator Author

vyzo commented Feb 20, 2024

Added some more comments; we need to write docs, the code is mostly self explanatory with a good mop page in the hyperspec.

@vyzo vyzo enabled auto-merge (squash) February 20, 2024 20:40
@vyzo vyzo merged commit ab6a75f into master Feb 20, 2024
12 checks passed
@vyzo vyzo deleted the interface-specializers branch February 20, 2024 21:16
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.

None yet

2 participants