Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions .JuliaFormatter.toml

This file was deleted.

41 changes: 41 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Project Overview

MemoryViews.jl provides `MemoryView`, a low-level view into `Memory{T}` for Julia ≥ 1.11. It's a `DenseVector{T}` subtype representing a `MemoryRef{T}` + length, with static mutability tracking via type parameter (`Mutable`/`Immutable`). The package also defines the `MemoryKind` trait for dispatch on memory-backed types.

## Commands

```bash
# Run tests
JULIA_TEST_FAILFAST=true julia --project -e 'using Pkg; Pkg.test()'

# Format code
runic -i .
```

## Architecture

**Core types** (defined in `src/MemoryViews.jl`):
- `MemoryView{T, M}` where `M ∈ {Mutable, Immutable}` — the main type
- `MemoryKind` trait: `IsMemory{T}` / `NotMemory` for dispatch

**Source files**:
- `construction.jl` — constructors from Array, Memory, String, SubArray, CodeUnits
- `basic.jl` — indexing, slicing (returns views, not copies), copying, find operations with memchr/memrchr C calls, comparison via memcmp
- `experimental.jl` — `split_first`, `split_last`, `split_at`, `split_unaligned`
- `delimited.jl` — `split_each` delimiter iterator
- `base_arrays.jl` — Vector/Memory conversion, append
- `io.jl` — `readbytes!`

**Extensions** (`ext/`): StringViews, FixedSizeArrays, LibDeflate integration.

## Key Patterns

- Slicing creates views into the same memory (no allocation)
- Performance-critical paths use `@ccall` to libc (`memset`, `memcmp`, `memchr`, `memrchr`) with `GC.@preserve`
- Version-conditional code for Julia 1.12+ vs 1.13+ (e.g., `Base.memoryindex` for `parentindices`)
- Trait-based dispatch pattern: define `foo(x)` → `foo(MemoryKind(typeof(x)), x)` → specialized on `IsMemory`/`NotMemory`
- `@boundscheck`/`@inbounds` used throughout for safe-by-default with opt-in elision
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "MemoryViews"
uuid = "a791c907-b98b-4e44-8f4d-e4c2362c6b2f"
version = "0.3.6"
version = "0.3.7"
authors = ["Jakob Nybo Nissen <jakobnybonissen@gmail.com>"]

[weakdeps]
Expand Down
3 changes: 2 additions & 1 deletion src/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,11 @@ function Base.:(==)(a::Mem, b::Mem) where {Mem <: BitMemory}
length(a) == length(b) || return false
(eltype(a) === Union{} || Base.issingletontype(eltype(a))) && return true
a.ref === b.ref && return true
nbytes = length(a) * sizeof(eltype(a))
GC.@preserve a b begin
aptr = Ptr{Nothing}(pointer(a))
bptr = Ptr{Nothing}(pointer(b))
y = @ccall memcmp(aptr::Ptr{Nothing}, bptr::Ptr{Nothing}, length(a)::Int)::Cint
y = @ccall memcmp(aptr::Ptr{Nothing}, bptr::Ptr{Nothing}, nbytes::Csize_t)::Cint
end
return iszero(y)
end
Expand Down
2 changes: 1 addition & 1 deletion src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ function Base.readbytes!(io::IO, v::MutableMemoryView{UInt8}, nb::Integer = leng
GC.@preserve v unsafe_read(io, Base.unsafe_convert(Ptr{UInt8}, remaining), ba % UInt)
remaining = remaining[(ba + 1):end]
end
return length(v) - length(remaining)
return min(nb, length(v) - length(remaining))
end
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ end
data = b"Hello, world!"
buf = IOBuffer(data)
v = fill(0xaa, 25)
readbytes!(buf, MemoryView(v), 7)
@test readbytes!(buf, MemoryView(v), 7) == 7
@test v[1:8] == b"Hello, \xaa"

# With nb being higher than the vector length
Expand Down
Loading