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

Simplify interfacing with Allegro foreign structs #21

Open
resttime opened this issue May 22, 2020 · 7 comments
Open

Simplify interfacing with Allegro foreign structs #21

resttime opened this issue May 22, 2020 · 7 comments

Comments

@resttime
Copy link
Owner

resttime commented May 22, 2020

This could make accessing slots of foreign structures much easier:
https://common-lisp.net/project/cffi/manual/html_node/Foreign-Structure-Types.html#Foreign-Structure-Types

@resttime
Copy link
Owner Author

After trying to implement this, it might be a little heavy handed. Most of the Allegro5 structures are opaque so translation isn't needed for most structs. CFFI also already defaults to exploding structs to a plist with MEM-REF in which members are easily accessible with GETF. CFFI also has macros for accessing the foreign slots of the structures. I don't think there's any performance improvement to be found using structs over plists when the number elements aren't expected to be large.

It'd probably be better to add helper functions/macros that aren't intrusive and documentation on common patterns of interacting with CFFI.

@resttime
Copy link
Owner Author

I'll keep thinking for a bit if there is a use case. Maybe in the future when library can be improved in some way that this will come about again.

@lockie
Copy link
Contributor

lockie commented May 28, 2020

As a heavy user of cl-liballegro I might add that there's also the issue with convenience. Surely you can use CFFI utilities to access struct fields, but in that case e.g. even simplest accessing of al_event type to dispatch on it looks like

(cffi:foreign-slot-value event '(:union al:event) 'al::type)

which is like super-verbose compared to some theoretical helper like (al:event-type event).
(Although my knowledge of CFFI is inferior compared to yours, maybe I'm missing something and you can suggest something here.)

So anyway the idea to use lisp defstruct's seems like a nice one to me.

@resttime
Copy link
Owner Author

My thoughts is the same on creating helper functions, just how to implement would differ so I'm interested in your experience as a user. Of the situations and resulting changes below, I was thinking of 1) but if you've come across 2) or 3) I'm down to implement whichever one.

For non-opaque structures you've come across do you...

1.) Simply read values?
New functions added
(al:event-type event) is (cffi:foreign-slot-value event '(:union al:event) 'al::type)

2.) Find yourself wanting a DEFSTRUCT?
New simple data structure added with basic accessors and setters
Memory translation

3.) Find yourself wanting a DEFCLASS?
New data structure added with accessors and setters
Memory translation

@resttime resttime reopened this May 28, 2020
@lockie
Copy link
Contributor

lockie commented May 28, 2020

I think as long as we leave performance questions out of consideration, the 1) approach would do just fine - if you're fine with manually writing bunch of accessors for every structure field, of course. DEFSTRUCT does that for you, but it'll also require writing custom foreign translation methods (which are, by the looks of it, are not that hard to implement, and have the nice property of improving performance which you've mentioned in #20). Anyways, if I'd be you, I think I'd go with 2) approach, just because I hate a lot of manual work 😅

DEFCLASS, on the other hand, seems to be overkill because, again, performance reasons: DEFSTRUCT's field access compiles to one indirect MOV instruction, while DEFCLASS's field access compiles to the entire function call (SLOT-VALUE or similar).

@resttime
Copy link
Owner Author

resttime commented May 28, 2020

#20 is actually in reference to this issue to optimize the overhead created by the memory translation from a foreign struct to a lisp struct rather than overall improvement in performance.

I hate a lot of manual work

Me too, this is where macros and/or a good text editor can dissolve a lot of it.

One of my worries was possible feature creep and imposing the overheard of managing a new structure, but getting an ok from a user opens up more possibilities to making higher level decisions in that case. I'll keep thinking of the options.

As a user there's going to be a simpler interface regardless of implementation, so no worries. Just keep making cool stuff! 😄

@resttime resttime changed the title Translate Foreign Structs to Lisp Structs Simplify interfacing with Allegro foreign structs May 28, 2020
@lockie
Copy link
Contributor

lockie commented May 29, 2020

As a user there's going to be a simpler interface regardless of implementation, so no worries. Just keep making cool stuff!

Thanks man, you too 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants