Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gluten-mirage: fix read and writev functions #32

Merged
merged 3 commits into from
Aug 5, 2022

Conversation

ansiwen
Copy link
Contributor

@ansiwen ansiwen commented Jul 31, 2022

This change fixes both the read and writev functions of the gluten-mirage package:

  • read now respects the length parameter of the receiving buffer and if the received data is too large, it stores the rest of the data until the next read call.
  • writev creates a copy of the data now, because the Flow.writev implementation takes ownership of the data buffers, and can't be reused.

The logic of read code has been confirmed in this thread: mirage/mirage-flow#46 (comment)

The socket type of the Client and Server is not a pure Mirage_flow.S.flow anymore. Instead it must be created with Gluten_mirage.create_socket flow, with flow being a value of Mirage_flow.S.flow.

Signed-off-by: Sven Anderson <sven@anderson.de>
@ansiwen ansiwen changed the title master gluten-mirage: fix read and writev functions Jul 31, 2022
@ansiwen ansiwen force-pushed the master branch 3 times, most recently from 69cafa1 to 9617676 Compare August 1, 2022 08:24
Signed-off-by: Sven Anderson <sven@anderson.de>
let cstruct_iovecs =
List.map
(fun { Faraday.buffer; off; len } ->
Cstruct.of_bigarray ~off ~len buffer)
iovecs
in
let len = Cstruct.lenv cstruct_iovecs in
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be easier to create the copy inside the List.map function above. I'll propose a suggestion.

Comment on lines 96 to 97
(fun { Faraday.buffer; off; len } ->
Cstruct.of_bigarray ~off ~len buffer)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(fun { Faraday.buffer; off; len } ->
Cstruct.of_bigarray ~off ~len buffer)
(fun { Faraday.buffer; off; len } ->
let copy = Bigstringaf.copy ~off ~len buffer in
Cstruct.of_bigarray ~off:0 ~len copy)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that (potentially) allocates many small Cstructs, my version allocates only one bigger Cstruct and copies everything into it.

Lwt.catch
(fun () ->
Flow.writev flow cstruct_iovecs >|= fun x ->
Flow.write sock.flow data >|= fun x ->
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Flow.write sock.flow data >|= fun x ->
Flow.write sock.flow cstruct_iovecs >|= fun x ->

Comment on lines +100 to +102
let len = Cstruct.lenv cstruct_iovecs in
let data = Cstruct.create_unsafe len in
let _, _ = Cstruct.fillv ~src:cstruct_iovecs ~dst:data in
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let len = Cstruct.lenv cstruct_iovecs in
let data = Cstruct.create_unsafe len in
let _, _ = Cstruct.fillv ~src:cstruct_iovecs ~dst:data in

@anmonteiro anmonteiro merged commit 3435604 into anmonteiro:master Aug 5, 2022
@anmonteiro
Copy link
Owner

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants