# Correct way to perform direct socket io, i.e no memory copy

**URL:** https://discourse.julialang.org/t/correct-way-to-perform-direct-socket-io-i-e-no-memory-copy/104758
**Category:** Performance
**Tags:** sockets, io
**Created:** [October 9, 2023, 4:46pm UTC](https://discourse.julialang.org/t/correct-way-to-perform-direct-socket-io-i-e-no-memory-copy/104758 "2023-10-09T16:46:59Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![L\_Adam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/l_adam/32/12086_2.png) [@L\_Adam](https://discourse.julialang.org/u/L_Adam)
#### Post date: [October 9, 2023, 4:46pm UTC](https://discourse.julialang.org/t/correct-way-to-perform-direct-socket-io-i-e-no-memory-copy/104758/1 "2023-10-09T16:46:59Z")

</div>

currently, socket read/write are through stream interface, and it’s buffered. and to read that, there is an extra memory copy with `take!`.

what is the proper way to do direct socket io, without memory copy?(or is direct socket bad, thus designed to be buffered? or such copy should not be a major concern)

should i replace those buffer?

```julia
# for read 
mybuffer = IOBuffer()
socket.buffer = mybuffer
Base.wait_readnb(socket)
# then check out my buffer
mybuffer.data

# for write
mybuffer = IOBuffer()
# write something to mybuffer
socket.sendbuf = nothing # perform flush if necessary
Base.unsafe_write(socket, pointer(mybuffer.data), nb)

```

this does not look good/correct to me. is there simple way to do this?

ideally i want something like

```julia
# for read 
buffer = zeros(UInt8, maxsize)
while should_continue 
  n_read = readbytes!(socket, buffer, maxsize; all = false) # fill at most maxsize
  # do something with buffer 
  # clean up if necessary
end

# for write 
buffer = zeros(UInt8, maxsize)
while should_continue
  # write something to buffer
  n_written = write(socket, buffer, nbytes)
  # clear buffer if necessary
end

```

---

<div class="post-metadata">

### Author: ![AMJ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amj/32/214096_2.png) [@AMJ](https://discourse.julialang.org/u/AMJ)
#### Post date: [October 10, 2023, 7:26pm UTC](https://discourse.julialang.org/t/correct-way-to-perform-direct-socket-io-i-e-no-memory-copy/104758/2 "2023-10-10T19:26:43Z")

</div>

I don’t think there is one in Julia. Anyhow, zero copy socket is hard to debug and needs more care when implementing, if you want to implement one in linux check out `vmsplice` and `sockmap` with which you can implement zero copy socks(these are linux os APIs, so you will encounter some form of explicit memory management).
