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

Avoid needless allocation in read-bytevector! #950

Merged
merged 4 commits into from
Mar 18, 2024

Conversation

wasamasa
Copy link
Contributor

This change switches the implementation strategy to basing read-bytevector on top of read-bytevector! rather than the other way around. Bonus: It's a bit less code than previously and is less surprising to the reader.

This change switches the implementation strategy to basing
read-bytevector on top of read-bytevector! rather than the other way
around.
@wasamasa wasamasa force-pushed the optimize-read-bytevector branch from 228edb8 to d0e6dc7 Compare March 18, 2024 00:14
Copy link
Owner

@ashinn ashinn left a comment

Choose a reason for hiding this comment

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

Thanks! I'm not sure why I did it this way - it should really be done in the FFI but your approach is better until I get around to that.

lib/scheme/extras.scm Outdated Show resolved Hide resolved
@@ -135,36 +135,31 @@
(if (zero? n)
#u8()
(let ((in (if (pair? o) (car o) (current-input-port)))
Copy link
Owner

Choose a reason for hiding this comment

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

let*

Copy link
Owner

@ashinn ashinn left a comment

Choose a reason for hiding this comment

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

Thanks!

(res (read-bytevector! vec in)))
(cond ((eof-object? res) res)
((< res n) (subbytes vec 0 i))
(else res)))))
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
(else res)))))
(else vec)))))

(vec (make-bytevector n))
(res (read-bytevector! vec in)))
(cond ((eof-object? res) res)
((< res n) (subbytes vec 0 i))
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
((< res n) (subbytes vec 0 i))
((< res n) (subbytes vec 0 res))

@ashinn ashinn merged commit 5b19aab into ashinn:master Mar 18, 2024
3 checks passed
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