blit

Fast byte-level blit, fill, match, and uninit allocation for MoonBit

blit
memcpy
memmove
bytes
moon add bikallem/blit@0.2.2
Download zip
Author
Version
0.2.2
License
Apache-2.0
Last updated
4 months ago
Downloads
10K
README

#blit

Fast byte-level blit, fill, match, uninit allocation, and growable byte buffer for MoonBit.

On the native backend, operations use C FFI (memmove, memset, 8-byte chunk matching) for vectorized performance. On JS/Wasm backends, pure MoonBit fallback implementations are used.

#Install

moon add bikallem/blit

#API

#Blit

// Copy between FixedArray[Byte] buffers (safe for overlapping regions)
@blit.blit_fixed_array(dst, dst_offset, src, src_offset, length)

// Copy from Bytes into FixedArray[Byte]
@blit.blit_bytes(dst, dst_offset, src, src_offset, length)

// Copy from BytesView into FixedArray[Byte]
@blit.blit_bytesview(dst, dst_offset, src, src_offset, length)

#Fill

// Fill a region with a single byte value
@blit.fill_bytes(dst, dst_offset, value, length)

#Match Length

// Count matching bytes between two FixedArray[Byte] regions
@blit.match_length(a, a_offset, b, b_offset, max_len)

// Count matching bytes between two Bytes regions
@blit.match_length_bytes(a, a_offset, b, b_offset, max_len)

// Count matching bytes within a BytesView
@blit.match_length_bv(src, a, b, max_len)

#Buffer

Growable byte buffer backed by FixedArray[Byte]. All internal access is bounds-check free. Uses uninit allocation and blit-based growth.

let buf = @blit.Buffer::new(size_hint=1024)

// Write
buf.write_byte(b'a')
buf.write_bytes(b"hello")
buf.write_bytesview(some_bytes[1:4])
buf.write_fixed(fixed_arr, offset, len)

// Unchecked write (caller must ensure capacity)
buf.ensure_capacity(10)
buf.write_byte_unchecked(b'x')

// Read
buf.get_byte(0) // bounds-check free read
buf.length() // current byte count
buf.data() // backing FixedArray[Byte]

// Internal copy (handles overlapping regions)
buf.copy_from_self(src_pos, len)

// Finalize: make_uninit(pos) + blit + unsafe_reinterpret_as_bytes
let bytes = buf.to_bytes()

// Reuse allocated memory
buf.reset()

#Uninit Allocation

Allocate without zeroing memory. The caller must fully initialize all elements before reading.

@blit.make_uninit(len) // -> FixedArray[Byte]
@blit.make_uninit_int(len) // -> FixedArray[Int]
@blit.make_uninit_uint(len) // -> FixedArray[UInt]

#License

Apache-2.0

#
Buffer

type Buffer

Growable byte buffer backed by FixedArray[Byte]. All internal access is bounds-check free.

#
Buffer::copy_from_self

fn Buffer::copy_from_self(self : Buffer, src_pos : Int, len : Int) -> Unit

Copy len bytes from src_pos in the buffer to the current write position. Handles overlapping regions correctly.

#
Buffer::data

fn Buffer::data(self : Buffer) -> FixedArray[Byte]

Direct access to backing array.

#
Buffer::ensure_capacity

fn Buffer::ensure_capacity(self : Buffer, additional : Int) -> Unit

Ensure space for at least additional more bytes. Grows with doubling strategy.

#
Buffer::get_byte

fn Buffer::get_byte(self : Buffer, pos : Int) -> Byte

Read byte at absolute position. Bounds-check free.

#
Buffer::length

fn Buffer::length(self : Buffer) -> Int

Current number of bytes written.

#
Buffer::new

fn Buffer::new(size_hint? : Int) -> Buffer

Create a new buffer with the given initial capacity.

#
Buffer::reset

fn Buffer::reset(self : Buffer) -> Unit

Reset write position to 0. Reuses allocated memory.

#
Buffer::to_bytes

fn Buffer::to_bytes(self : Buffer) -> Bytes

Finalize: allocate exact-size uninit array, blit, reinterpret as Bytes.

#
Buffer::unsafe_advance

fn Buffer::unsafe_advance(self : Buffer, n : Int) -> Unit

Advance write position by n. Caller must have written directly to data().

#
Buffer::write_byte

fn Buffer::write_byte(self : Buffer, b : Byte) -> Unit

Append a single byte.

#
Buffer::write_byte_unchecked

fn Buffer::write_byte_unchecked(self : Buffer, b : Byte) -> Unit

Append a single byte without capacity check. Caller must ensure capacity.

#
Buffer::write_bytes

fn Buffer::write_bytes(self : Buffer, src : Bytes) -> Unit

Append all bytes from src.

#
Buffer::write_bytesview

fn Buffer::write_bytesview(self : Buffer, src : BytesView) -> Unit

Append all bytes from src.

#
Buffer::write_fixed

fn Buffer::write_fixed(self : Buffer, src : FixedArray[Byte], offset : Int, len : Int) -> Unit

Append len bytes from src starting at offset.

#
blit_bytes

fn blit_bytes(dst : FixedArray[Byte], dst_offset : Int, src : Bytes, src_offset : Int, length : Int) -> Unit

Copy length bytes from src (Bytes) at src_offset into dst (FixedArray) at dst_offset.

#
blit_bytesview

fn blit_bytesview(dst : FixedArray[Byte], dst_offset : Int, src : BytesView, src_offset : Int, length : Int) -> Unit

Copy length bytes from src (BytesView) at src_offset into dst (FixedArray) at dst_offset. Note: src_offset is relative to the underlying Bytes, not the view's start.

#
blit_fixed_array

fn blit_fixed_array(dst : FixedArray[Byte], dst_offset : Int, src : FixedArray[Byte], src_offset : Int, length : Int) -> Unit

Copy length bytes between FixedArray[Byte] buffers. Safe for overlapping regions within the same array.

#
fill_bytes

fn fill_bytes(dst : FixedArray[Byte], dst_offset : Int, value : Byte, length : Int) -> Unit

Fill length bytes in dst starting at dst_offset with value.

#
make_uninit

fn make_uninit(len : Int) -> FixedArray[Byte]

Allocate a FixedArray[Byte] without zeroing memory. The caller MUST fully initialize the buffer before reading from it.

#
make_uninit_int

fn make_uninit_int(len : Int) -> FixedArray[Int]

#
make_uninit_uint

fn make_uninit_uint(len : Int) -> FixedArray[UInt]

#
match_length

fn match_length(a : FixedArray[Byte], a_offset : Int, b : FixedArray[Byte], b_offset : Int, max_len : Int) -> Int

Word-at-a-time match length comparison (byte-by-byte fallback for non-native).

#
match_length_bv

fn match_length_bv(src : BytesView, a : Int, b : Int, max_len : Int) -> Int

Word-at-a-time match length comparison on BytesView. Returns the number of matching bytes between src[a..] and src[b..], up to max_len.

#
match_length_bytes

fn match_length_bytes(a : Bytes, a_offset : Int, b : Bytes, b_offset : Int, max_len : Int) -> Int

Word-at-a-time match length comparison on Bytes (byte-by-byte fallback for non-native).

#
shift_int_array

fn shift_int_array(arr : FixedArray[Int], len : Int, delta : Int, floor : Int) -> Unit

Subtract delta from each of len elements, clamping to floor.

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io