Skip to content

Commit 6790e70

Browse files
Use bytes crate instead of Vec<u8> for binary data
To avoid pack/unpack overhead, use magnus's TryConvert trait implementation for bytes::Bytes.
1 parent c57e885 commit 6790e70

File tree

7 files changed

+53
-42
lines changed

7 files changed

+53
-42
lines changed

Cargo.lock

Lines changed: 23 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/ruby_wasm/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ publish = false
1010
crate-type = ["cdylib"]
1111

1212
[dependencies]
13-
magnus = "0.6.2"
13+
magnus = { version = "0.6.2", features = ["bytes"] }
14+
bytes = "1"
1415
wizer = "4.0.0"
1516
wasi-vfs-cli = { git = "https://github.com/kateinoigakukun/wasi-vfs/", tag = "0.5.2" }
1617
structopt = "0.3.26"
17-
wit-component = { git = "https://github.com/bytecodealliance/wasm-tools", rev = "refs/pull/1490/head" }
18+
wit-component = "0.203.0"

ext/ruby_wasm/src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use wizer::Wizer;
1212
static RUBY_WASM: value::Lazy<RModule> =
1313
value::Lazy::new(|ruby| ruby.define_module("RubyWasmExt").unwrap());
1414

15-
fn preinit(core_module: Vec<u8>) -> Result<Vec<u8>, Error> {
15+
fn preinit(core_module: bytes::Bytes) -> Result<bytes::Bytes, Error> {
1616
let rbwasm_error = eval("RubyWasmExt::Error")?;
1717
let rbwasm_error = ExceptionClass::from_value(rbwasm_error).unwrap();
1818
let mut wizer = Wizer::new();
@@ -26,6 +26,7 @@ fn preinit(core_module: Vec<u8>) -> Result<Vec<u8>, Error> {
2626
wizer
2727
.run(&core_module)
2828
.map_err(|e| Error::new(rbwasm_error, format!("failed to run wizer: {}", e)))
29+
.map(|output| output.into())
2930
}
3031

3132
struct WasiVfsInner {
@@ -53,14 +54,14 @@ impl WasiVfs {
5354
self.0.borrow_mut().map_dirs.push((guest_dir.into(), host_dir.into()));
5455
}
5556

56-
fn pack(&self, wasm_bytes: Vec<u8>) -> Result<Vec<u8>, Error> {
57+
fn pack(&self, wasm_bytes: bytes::Bytes) -> Result<bytes::Bytes, Error> {
5758
let output_bytes = wasi_vfs_cli::pack(&wasm_bytes, self.0.borrow().map_dirs.clone()).map_err(|e| {
5859
Error::new(
5960
exception::standard_error(),
6061
format!("failed to pack wasi vfs: {}", e),
6162
)
6263
})?;
63-
Ok(output_bytes)
64+
Ok(output_bytes.into())
6465
}
6566
}
6667

@@ -83,7 +84,7 @@ impl ComponentLink {
8384
Ok(())
8485
}
8586

86-
fn library(&self, name: String, module: Vec<u8>, dl_openable: bool) -> Result<(), Error> {
87+
fn library(&self, name: String, module: bytes::Bytes, dl_openable: bool) -> Result<(), Error> {
8788
self.linker(|linker| {
8889
linker.library(&name, &module, dl_openable).map_err(|e| {
8990
Error::new(
@@ -93,7 +94,7 @@ impl ComponentLink {
9394
})
9495
})
9596
}
96-
fn adapter(&self, name: String, module: Vec<u8>) -> Result<(), Error> {
97+
fn adapter(&self, name: String, module: bytes::Bytes) -> Result<(), Error> {
9798
self.linker(|linker| {
9899
linker.adapter(&name, &module).map_err(|e| {
99100
Error::new(
@@ -123,7 +124,7 @@ impl ComponentLink {
123124
Ok(linker.use_built_in_libdl(use_libdl))
124125
})
125126
}
126-
fn encode(&self) -> Result<Vec<u8>, Error> {
127+
fn encode(&self) -> Result<bytes::Bytes, Error> {
127128
// Take the linker out of the cell and consume it
128129
let linker = self.0.borrow_mut().take().ok_or_else(|| {
129130
Error::new(
@@ -137,7 +138,7 @@ impl ComponentLink {
137138
format!("failed to encode linker: {}", e),
138139
)
139140
})?;
140-
Ok(encoded)
141+
Ok(encoded.into())
141142
}
142143
}
143144

lib/ruby_wasm/cli.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,9 @@ def do_build(executor, tmpdir, packager, options)
323323
RubyWasm.logger.info "Size: #{SizeFormatter.format(wasm_bytes.size)}"
324324
case options[:output]
325325
when "-"
326-
@stdout.write wasm_bytes.pack("C*")
326+
@stdout.write wasm_bytes
327327
else
328-
File.binwrite(options[:output], wasm_bytes.pack("C*"))
328+
File.binwrite(options[:output], wasm_bytes)
329329
RubyWasm.logger.debug "Wrote #{options[:output]}"
330330
end
331331
end

lib/ruby_wasm/packager/core.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def build(executor, options)
3838
end
3939

4040
def build_and_link_exts(executor)
41+
raise NotImplementedError
4142
end
4243

4344
# Array of paths to extconf.rb files.
@@ -129,17 +130,15 @@ def link_exts(executor, build)
129130
libraries.each do |lib|
130131
# Non-DL openable libraries should be referenced as base name
131132
lib_name = File.basename(lib)
132-
# @type var module_bytes: Array[Integer]
133-
module_bytes = File.binread(lib).unpack("C*")
133+
module_bytes = File.binread(lib)
134134
RubyWasm.logger.info "Linking #{lib_name} (#{module_bytes.size} bytes)"
135135
linker.library(lib_name, module_bytes, false)
136136
end
137137

138138
dl_openable_libs.each do |lib|
139139
# DL openable lib_name should be a relative path from ruby_root
140140
lib_name = "/" + Pathname.new(lib).relative_path_from(Pathname.new(ruby_root)).to_s
141-
# @type var module_bytes: Array[Integer]
142-
module_bytes = File.binread(lib).unpack("C*")
141+
module_bytes = File.binread(lib)
143142
RubyWasm.logger.info "Linking #{lib_name} (#{module_bytes.size} bytes)"
144143
linker.library(lib_name, module_bytes, true)
145144
end
@@ -148,8 +147,7 @@ def link_exts(executor, build)
148147
adapter_name = File.basename(adapter)
149148
# e.g. wasi_snapshot_preview1.command.wasm -> wasi_snapshot_preview1
150149
adapter_name = adapter_name.split(".")[0]
151-
# @type var module_bytes: Array[Integer]
152-
module_bytes = File.binread(adapter).unpack("C*")
150+
module_bytes = File.binread(adapter)
153151
linker.adapter(adapter_name, module_bytes)
154152
end
155153
return linker.encode()

sig/ruby_wasm/ext.rbs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module RubyWasmExt
2-
def self.preinitialize: (Array[Integer] module_bytes) -> Array[Integer]
2+
type bytes = String
3+
4+
def self.preinitialize: (bytes module_bytes) -> bytes
35

46
class WasiVfs
57
def initialize: () -> void
@@ -8,17 +10,17 @@ module RubyWasmExt
810

911
def map_dir: (String guest_path, String host_path) -> void
1012

11-
def pack: (Array[Integer] module_bytes) -> Array[Integer]
13+
def pack: (bytes module_bytes) -> bytes
1214
end
1315

1416
class ComponentLink
1517
def initialize: () -> void
16-
def library: (String name, Array[Integer] | String module, bool dl_openable) -> void
17-
def adapter: (String name, Array[Integer] | String module) -> void
18+
def library: (String name, bytes module, bool dl_openable) -> void
19+
def adapter: (String name, bytes module) -> void
1820
def validate: (bool) -> void
1921
def stack_size: (Integer) -> void
2022
def stub_missing_functions: (bool) -> void
2123
def use_built_in_libdl: (bool) -> void
22-
def encode: () -> Array[Integer]
24+
def encode: () -> bytes
2325
end
2426
end

sig/ruby_wasm/packager.rbs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ class RubyWasm::Packager
2020

2121
type build_config = Hash[untyped, untyped]
2222

23+
type bytes = String
24+
2325
@definition: untyped
2426
@config: build_config
2527

2628
def initialize: (string root, build_config?, untyped? definition) -> void
2729

28-
def package: (RubyWasm::BuildExecutor, string dest_dir, untyped options) -> Array[Integer]
30+
def package: (RubyWasm::BuildExecutor, string dest_dir, untyped options) -> bytes
2931

3032
@ruby_core_build: RubyWasm::Packager::Core?
3133
def ruby_core_build: () -> RubyWasm::Packager::Core
@@ -48,7 +50,7 @@ class RubyWasm::Packager
4850
@packager: RubyWasm::Packager
4951
def initialize: (RubyWasm::Packager) -> void
5052
def build: (RubyWasm::BuildExecutor, untyped options) -> String
51-
def build_and_link_exts: (RubyWasm::BuildExecutor) -> Array[Integer]
53+
def build_and_link_exts: (RubyWasm::BuildExecutor) -> bytes
5254

5355
extend Forwardable
5456

@@ -64,7 +66,7 @@ class RubyWasm::Packager
6466
def initialize: (RubyWasm::Packager) -> void
6567
def build: (RubyWasm::BuildExecutor, untyped options) -> String
6668
def specs_with_extensions: () -> Array[[untyped, Array[string]]]
67-
def build_and_link_exts: (RubyWasm::BuildExecutor) -> void
69+
def build_and_link_exts: (RubyWasm::BuildExecutor) -> bytes
6870
end
6971

7072
class DynamicLinking < RubyWasm::Packager::Core::BuildStrategy
@@ -73,7 +75,7 @@ class RubyWasm::Packager
7375
def build_exts: (RubyWasm::BuildExecutor, RubyWasm::Build) -> void
7476
def name: () -> string
7577

76-
private def link_exts: (RubyWasm::BuildExecutor, RubyWasm::Build) -> Array[Integer]
78+
private def link_exts: (RubyWasm::BuildExecutor, RubyWasm::Build) -> bytes
7779
end
7880

7981
class StaticLinking < RubyWasm::Packager::Core::BuildStrategy

0 commit comments

Comments
 (0)