unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* Minimal example of extending Guile with Rust?
@ 2024-01-29 15:04 Dr. Arne Babenhauserheide
  2024-01-29 15:33 ` Olivier Dion
  2024-01-29 15:49 ` M
  0 siblings, 2 replies; 4+ messages in thread
From: Dr. Arne Babenhauserheide @ 2024-01-29 15:04 UTC (permalink / raw)
  To: guile-devel

[-- Attachment #1: Type: text/plain, Size: 604 bytes --]

Hi,

do you know a nice minimal example that shows how to extend Guile with
Rust to get maximum performance for some tasks?

I’m not looking for hints how to speed up Scheme code, or how to do
less. Instead I want to learn how to do those cases where I actually
need some specific algorithm at maximum speed — and want to implement it
in Rust.

Ideally with a fast Scheme-to-Rust-to-Scheme interface (similar to the
native C arrays numpy exposes to C that allow getting the speed of C).

Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein,
ohne es zu merken.
draketo.de

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 1125 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Minimal example of extending Guile with Rust?
@ 2024-01-29 15:31 Jean Abou Samra
  0 siblings, 0 replies; 4+ messages in thread
From: Jean Abou Samra @ 2024-01-29 15:31 UTC (permalink / raw)
  To: Dr. Arne Babenhauserheide; +Cc: guile-devel

[-- Attachment #1: Type: text/plain, Size: 1335 bytes --]


There are a number of crates with Rust bindings to the Guile C API: https://lib.rs/search?q=guile

Most of them are just the result of running the bindgen tool, which autogenerates Rust bindings from C header files. A few of them have slightly more convenient wrapper but none is really well-developed AFAICS.

Honestly, I think the easiest at this time is to do the interfacing with Guile from C. Create a Rust module which exports a function through C FFI, and convert between C and Guile types in C. Alternatively, use the Guile FFI facilities for the conversion. It is probably simpler than doing these conversions in Rust. It is, in any case, safer, because I'm quite sure that the Rust compiler will be happy to do optimizations that defeat conservative GC, by hiding pointers in local variables (which Rust can more easily do, thanks to its aliasing guarantees).

Be careful with calling Guile code from Rust code. This is fraught, because Guile code can raise exceptions, which are basically propagated through longjmp, and it is UB in Rust to unwind through Rust code with longjmp. Basically, if you call Guile from Rust then it must be wrapped in exception handling. An exception must not propagate through Rust stack frames. call/cc would be a problem too, so you likely want to use with-continuation-barrier.


[-- Attachment #2: Type: text/html, Size: 1838 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Minimal example of extending Guile with Rust?
  2024-01-29 15:04 Minimal example of extending Guile with Rust? Dr. Arne Babenhauserheide
@ 2024-01-29 15:33 ` Olivier Dion
  2024-01-29 15:49 ` M
  1 sibling, 0 replies; 4+ messages in thread
From: Olivier Dion @ 2024-01-29 15:33 UTC (permalink / raw)
  To: Dr. Arne Babenhauserheide, guile-devel

On Mon, 29 Jan 2024, "Dr. Arne Babenhauserheide" <arne_bab@web.de> wrote:

[...]

> Ideally with a fast Scheme-to-Rust-to-Scheme interface (similar to the
> native C arrays numpy exposes to C that allow getting the speed of C).

See <https://doc.rust-lang.org/nomicon/ffi.html>.  There is many path to
do this.  The main issue is the calling convention.

Rust functions that you want binding for must follow the C calling
convention by using `extern "C"'.  Otherwise, the calling convention of
the compiler is used (AFAIK, Rust does not define a calling convention).
You can then simply use the system foreign interface as usual as if the
Rust functions were C functions.

There are others thing to consider.  Structures in Rust must have the
attribute `#[repr(C)]' to follow the C ABI for structure members layout.
Otherwise, the compiler is free to re-order members.

Then there is callbacks .. and other things ..

The above link gives comments on all of this but not much details.

Hope that helps.

-- 
Olivier Dion
oldiob.dev



^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: Minimal example of extending Guile with Rust?
  2024-01-29 15:04 Minimal example of extending Guile with Rust? Dr. Arne Babenhauserheide
  2024-01-29 15:33 ` Olivier Dion
@ 2024-01-29 15:49 ` M
  1 sibling, 0 replies; 4+ messages in thread
From: M @ 2024-01-29 15:49 UTC (permalink / raw)
  To: Dr. Arne Babenhauserheide, guile-devel@gnu.org

[-- Attachment #1: Type: text/plain, Size: 662 bytes --]

> (similar to the
>native C arrays numpy exposes to C that allow getting the >speed of C).

For this in particular, there is pointer->bytevector, bytevector->pointer and bytevector-TYPE-ref, bytevector-TYPE-set! (it’s not limited to only bytes, also floats are available IIRC).

Don’t do bytevector->pointer + pointer->bytevector + … in long sequences though, or else this loop will use memory proportional to the length to this sequence. (This is intentionally undocumented (*).)

Best regards,
Maxime Devos

(*) For its application bytevector-slice, see https://lists.gnu.org/archive/html/guile-devel/2023-01/msg00042.html, which was ignored.

[-- Attachment #2: Type: text/html, Size: 2136 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-01-29 15:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-29 15:04 Minimal example of extending Guile with Rust? Dr. Arne Babenhauserheide
2024-01-29 15:33 ` Olivier Dion
2024-01-29 15:49 ` M
  -- strict thread matches above, loose matches on Subject: below --
2024-01-29 15:31 Jean Abou Samra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).