changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > org > notes / 20231105.org

changeset 0: 87b04952fb18
child: 04e86b94ef1a
author: Richard Westhaver <ellis@rwest.io>
date: Sun, 28 Apr 2024 19:51:45 -0400
permissions: -rw-r--r--
description: init
1 * DRAFT dylib-skel-1
2 - State "DRAFT" from [2023-11-05 Sun 22:23]
3 ** Overview
4 Our core languages are [[https://www.rust-lang.org/][Rust]] and [[https://lisp-lang.org/][Lisp]] - this is the killer combo which will allow NAS-T
5 to rapidly develop high-quality software. As such, it's crucial that these two very
6 different languages (i.e. compilers) are able to interoperate seamlessly.
7 
8 Some interop methods are easy to accomodate via the OS - such as IPC or data sharing,
9 but others are a bit more difficult.
10 
11 In this 2-part series we'll build a FFI bridge between Rust and Lisp, which is something
12 that /can/ be difficult, due to some complications with Rust and because this is not the
13 most popular software stack (yet ;). This is an experiment and may not make it to our
14 code-base, but it's definitely something worth adding to the toolbox in case we need it.
15 
16 ** FFI
17 The level of interop we're after in this case is [[https://en.wikipedia.org/wiki/Foreign_function_interface][FFI]].
18 
19 Basically, calling Rust code from Lisp and vice-versa. There's an article about calling
20 Rust from Common Lisp [[https://dev.to/veer66/calling-rust-from-common-lisp-45c5][here]] which shows the basics and serves as a great starting point
21 for those interested.
22 *** Rust != C
23 The complication(s) with Rust I mentioned early is really just that /it is not C/. =C=
24 is old, i.e. well-supported with a stable ABI, making the process of creating bindings
25 for a C library a breeze in many languages.
26 
27 For a Rust library we need to first appease the compiler, as explained in [[https://doc.rust-lang.org/nomicon/ffi.html#calling-rust-code-from-c][this section]]
28 of the Rustonomicon. Among other things it involves changing the calling-convention of
29 functions with a type signature and editing the Cargo.toml file to produce a
30 C-compatible ABI binary. The Rust default ABI is unstable and can't reliably be used
31 like the C ABI can.
32 
33 *** Overhead
34 Using FFI involves some overhead. Check [[https://github.com/dyu/ffi-overhead][here]] for an example benchmark across a few
35 languages. While building the NAS-T core, I'm very much aware of this, and will need a
36 few sanity benchmarks to make sure the cost doesn't outweigh the benefit. In particular,
37 I'm concerned about crossing multiple language barriers (Rust<->C<->Lisp).
38 
39 ** Rust -> C -> Lisp
40 *** Setup
41 For starters, I'm going to assume we all have Rust (via =rustup=) and Lisp (=sbcl= only)
42 installed on our GNU/Linux system (some tweaks needed for Darwin/Windows, not covered in
43 this post).
44 **** Cargo
45 Create a new library crate. For this example we're focusing on a 'skeleton' for
46 /dynamic/ libraries only, so our experiment will be called =dylib-skel= or *dysk* for
47 short.
48 src_sh[:exports code]{cargo init dysk --lib && cd dysk}
49 
50 A =src/lib.rs= will be generated for you. Go ahead and delete that. We're going to be
51 making our own =lib.rs= file directly in the root directory (just to be cool).
52 
53 The next step is to edit your =Cargo.toml= file. Add these lines after the =[package]=
54 section and before =[dependencies]=:
55 #+begin_src conf-toml
56 [lib]
57 crate-type = ["cdylib","rlib"]
58 path = "lib.rs"
59 [[bin]]
60 name="dysk-test"
61 path="test.rs"
62 #+end_src
63 
64 This tells Rust to generate a shared C-compatible object with a =.so= extension which we
65 can open using [[https://man.archlinux.org/man/dlopen.3.en][dlopen]].
66 **** cbindgen
67 ***** install
68 Next, we want the =cbindgen= program which we'll use to generate header files for
69 C/C++. This step isn't necessary at all, we just want it for further experimentation.
70 
71 src_sh[:exports code]{cargo install --force cbindgen}
72 
73 We append the =cbindgen= crate as a /build dependency/ to our =Cargo.toml= like so:
74 #+begin_src conf-toml
75 [build-dependencies]
76 cbindgen = "0.24"
77 #+end_src
78 ***** cbindgen.toml
79 #+begin_src conf-toml :tangle cbindgen.toml
80 language = "C"
81 autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */"
82 include_version = true
83 namespace = "dysk"
84 cpp_compat = true
85 after_includes = "#define DYSK_VERSION \"0.1.0\""
86 line_length = 88
87 tab_width = 2
88 documentation = true
89 documentation_style = "c99"
90 usize_is_size_t = true
91 [cython]
92 header = '"dysk.h"'
93 #+end_src
94 ***** build.rs
95 #+begin_src rust :tangle build.rs
96 fn main() -> Result<(), cbindgen::Error> {
97  if let Ok(b) = cbindgen::generate(std::env::var("CARGO_MANIFEST_DIR").unwrap()) {
98  b.write_to_file("dysk.h"); Ok(())}
99  else { panic!("failed to generate dysk.h from cbindgen.toml") } }
100 #+end_src
101 *** lib.rs
102 #+begin_src rust :tangle lib.rs
103 //! lib.rs --- dysk library
104 use std::ffi::{c_char, c_int, CString};
105 #[no_mangle]
106 pub extern "C" fn dysk_hello() -> *const c_char {
107  CString::new("hello from rust").unwrap().into_raw()}
108 #[no_mangle]
109 pub extern "C" fn dysk_plus(a:c_int,b:c_int) -> c_int {a+b}
110 #[no_mangle]
111 pub extern "C" fn dysk_plus1(n:c_int) -> c_int {n+1}
112 #+end_src
113 *** test.rs
114 #+begin_src rust :tangle test.rs
115 //! test.rs --- dysk test
116 fn main() { let mut i = 0u32; while i < 500000000 {i+=1; dysk::dysk_plus1(2 as core::ffi::c_int);}}
117 #+end_src
118 *** compile
119 #+begin_src sh
120 cargo build --release
121 #+end_src
122 *** load from SBCL
123 #+begin_src lisp :tangle dysk.lisp
124 (load-shared-object #P"target/release/libdysk.so")
125 (define-alien-routine dysk-hello c-string)
126 (define-alien-routine dysk-plus int (a int) (b int))
127 (define-alien-routine dysk-plus1 int (n int))
128 (dysk-hello) ;; => "hello from rust"
129 #+end_src
130 *** benchmark
131 #+begin_src shell
132 time target/release/dysk-test
133 #+end_src
134 #+begin_src lisp :tangle test.lisp
135 (time (dotimes (_ 500000000) (dysk-plus1 2)))
136 #+end_src