@@ -6,24 +6,24 @@ Rust’s greatest strengths: a lack of a substantial runtime.
66As organizations grow, they increasingly rely on a multitude of programming
77languages. Different programming languages have different strengths and
88weaknesses, and a polyglot stack lets you use a particular language where
9- its strengths make sense, and use a different language where it’s weak.
9+ its strengths make sense and a different one where it’s weak.
1010
1111A very common area where many programming languages are weak is in runtime
1212performance of programs. Often, using a language that is slower, but offers
13- greater programmer productivity is a worthwhile trade-off. To help mitigate
14- this, they provide a way to write some of your system in C, and then call
15- the C code as though it were written in the higher-level language. This is
13+ greater programmer productivity, is a worthwhile trade-off. To help mitigate
14+ this, they provide a way to write some of your system in C and then call
15+ that C code as though it were written in the higher-level language. This is
1616called a ‘foreign function interface’, often shortened to ‘FFI’.
1717
1818Rust has support for FFI in both directions: it can call into C code easily,
1919but crucially, it can also be called _ into_ as easily as C. Combined with
2020Rust’s lack of a garbage collector and low runtime requirements, this makes
2121Rust a great candidate to embed inside of other languages when you need
22- some extra oomph.
22+ that extra oomph.
2323
2424There is a whole [ chapter devoted to FFI] [ ffi ] and its specifics elsewhere in
2525the book, but in this chapter, we’ll examine this particular use-case of FFI,
26- with three examples, in Ruby, Python, and JavaScript.
26+ with examples in Ruby, Python, and JavaScript.
2727
2828[ ffi ] : ffi.html
2929
@@ -40,18 +40,18 @@ optimizations can stack allocate particular numbers, but rather than relying
4040on an optimizer to do its job, we may want to ensure that we’re always using
4141primitive number types rather than some sort of object type.
4242
43- Second, many languages have a ‘global interpreter lock’, which limits
43+ Second, many languages have a ‘global interpreter lock’ (GIL) , which limits
4444concurrency in many situations. This is done in the name of safety, which is
4545a positive effect, but it limits the amount of work that can be done at the
4646same time, which is a big negative.
4747
4848To emphasize these two aspects, we’re going to create a little project that
49- uses these two aspects heavily. Since the focus of the example is the embedding
50- of Rust into the languages, rather than the problem itself, we’ll just use a
49+ uses these two aspects heavily. Since the focus of the example is to embed
50+ Rust into other languages, rather than the problem itself, we’ll just use a
5151toy example:
5252
5353> Start ten threads. Inside each thread, count from one to five million. After
54- > All ten threads are finished, print out ‘done!’.
54+ > all ten threads are finished, print out ‘done!’.
5555
5656I chose five million based on my particular computer. Here’s an example of this
5757code in Ruby:
@@ -69,7 +69,7 @@ threads = []
6969 end
7070end
7171
72- threads.each {|t | t.join }
72+ threads.each { |t | t.join }
7373puts " done!"
7474```
7575
@@ -82,12 +82,12 @@ sort of process monitoring tool, like `top`, I can see that it only uses one
8282core on my machine. That’s the GIL kicking in.
8383
8484While it’s true that this is a synthetic program, one can imagine many problems
85- that are similar to this in the real world. For our purposes, spinning up some
85+ that are similar to this in the real world. For our purposes, spinning up a few
8686busy threads represents some sort of parallel, expensive computation.
8787
8888# A Rust library
8989
90- Let’s re-write this problem in Rust. First, let’s make a new project with
90+ Let’s rewrite this problem in Rust. First, let’s make a new project with
9191Cargo:
9292
9393``` bash
@@ -129,7 +129,7 @@ src/lib.rs:3 fn process() {
129129src/lib.rs:4 let handles: Vec< _> = (0..10).map(| _| {
130130src/lib.rs:5 thread::spawn(|| {
131131src/lib.rs:6 let mut x = 0;
132- src/lib.rs:7 for _ in (0..5_000_001 ) {
132+ src/lib.rs:7 for _ in (0..5_000_000 ) {
133133src/lib.rs:8 x += 1
134134 ...
135135src/lib.rs:6:17: 6:22 warning: variable ` x` is assigned to, but never used, # [warn(unused_variables)] on by default
@@ -151,7 +151,7 @@ Finally, we join on each thread.
151151Right now, however, this is a Rust library, and it doesn’t expose anything
152152that’s callable from C. If we tried to hook this up to another language right
153153now, it wouldn’t work. We only need to make two small changes to fix this,
154- though. The first is modify the beginning of our code:
154+ though. The first is to modify the beginning of our code:
155155
156156` ` ` rust,ignore
157157# [no_mangle]
@@ -161,7 +161,7 @@ pub extern fn process() {
161161We have to add a new attribute, ` no_mangle` . When you create a Rust library, it
162162changes the name of the function in the compiled output. The reasons for this
163163are outside the scope of this tutorial, but in order for other languages to
164- know how to call the function, we need to not do that. This attribute turns
164+ know how to call the function, we can’t do that. This attribute turns
165165that behavior off.
166166
167167The other change is the ` pub extern` . The ` pub` means that this function should
@@ -178,7 +178,7 @@ crate-type = ["dylib"]
178178` ` `
179179
180180This tells Rust that we want to compile our library into a standard dynamic
181- library. By default, Rust compiles into an ‘rlib’, a Rust-specific format.
181+ library. By default, Rust compiles an ‘rlib’, a Rust-specific format.
182182
183183Let’s build the project now:
184184
@@ -204,7 +204,7 @@ Now that we’ve got our Rust library built, let’s use it from our Ruby.
204204
205205# Ruby
206206
207- Open up a ` embed.rb` file inside of our project, and do this:
207+ Open up an ` embed.rb` file inside of our project, and do this:
208208
209209` ` ` ruby
210210require ' ffi'
217217
218218Hello.process
219219
220- puts " done!"
220+ puts ' done!'
221221` ` `
222222
223223Before we can run this, we need to install the ` ffi` gem:
@@ -241,7 +241,7 @@ done!
241241$
242242` ` `
243243
244- Whoah , that was fast! On my system, this took ` 0.086` seconds, rather than
244+ Whoa , that was fast! On my system, this took ` 0.086` seconds, rather than
245245the two seconds the pure Ruby version took. Let’s break down this Ruby
246246code:
247247
@@ -258,11 +258,11 @@ module Hello
258258 ffi_lib ' target/release/libembed.so'
259259` ` `
260260
261- The ` ffi ` gem’s authors recommend using a module to scope the functions
262- we’ll import from the shared library. Inside, we ` extend` the necessary
263- ` FFI::Library ` module, and then call ` ffi_lib` to load up our shared
264- object library. We just pass it the path that our library is stored,
265- which as we saw before, is ` target/release/libembed.so` .
261+ The ` Hello ` module is used to attach the native functions from the shared
262+ library. Inside, we ` extend` the necessary ` FFI::Library ` module and then call
263+ ` ffi_lib` to load up our shared object library. We just pass it the path that
264+ our library is stored, which, as we saw before, is
265+ ` target/release/libembed.so` .
266266
267267` ` ` ruby
268268attach_function :process, [], :void
@@ -280,10 +280,10 @@ Hello.process
280280
281281This is the actual call into Rust. The combination of our ` module`
282282and the call to ` attach_function` sets this all up. It looks like
283- a Ruby function, but is actually Rust!
283+ a Ruby function but is actually Rust!
284284
285285` ` ` ruby
286- puts " done!"
286+ puts ' done!'
287287` ` `
288288
289289Finally, as per our project’s requirements, we print out ` done! ` .
@@ -329,7 +329,7 @@ After that installs, we can use it:
329329var ffi = require(' ffi' );
330330
331331var lib = ffi.Library(' target/release/libembed' , {
332- ' process' : [ ' void' , [] ]
332+ ' process' : [' void' , []]
333333});
334334
335335lib.process ();
@@ -340,7 +340,7 @@ console.log("done!");
340340It looks more like the Ruby example than the Python example. We use
341341the ` ffi` module to get access to ` ffi.Library()` , which loads up
342342our shared object. We need to annotate the return type and argument
343- types of the function, which are ' void' for return, and an empty
343+ types of the function, which are ` void` for return and an empty
344344array to signify no arguments. From there, we just call it and
345345print the result.
346346
0 commit comments