-
Notifications
You must be signed in to change notification settings - Fork 207
Adopt span in Data IO implementation #1576
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
Adopt span in Data IO implementation #1576
Conversation
72bf4f6 to
9e4ed05
Compare
|
Moving to draft to sort out a few miscellaneous test failures |
|
Looks good to me in principle, although I didn't try to figure out what the test failures are about. |
Found it - an accidental "while not empty" instead of "while not full" when reading bytes from a file into a buffer 🙂 |
| if !ReadFile(hFile, pBuffer, dwBytesToRead, &dwBytesRead, nil) { | ||
| throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: true) | ||
| try pBuffer.withUnsafeMutableBytes { bytes, initializedCount in | ||
| if !ReadFile(hFile, bytes.baseAddress!.advanced(by: initializedCount), dwBytesToRead, &dwBytesRead, nil) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes me wonder if we want a variant of withUnsafeMutableBytes that gives you only the uninitialized portion of the OutputRawSpan.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like it could be beneficial for C interop where you repeatedly call a function that fills in a buffer!
This adopts Span types throughout the Data reading/writing implementation. There are still a few cases where we use unsafe buffers directly (getting unsafe buffers from spans directly at the call site for an unsafe C API, and storing unsafe pointers that are the result of
mmap-like calls) but this removes the majority of unsafe buffers passed around in our code and adopts span as the internal currency type wherever possible. Additionally, this allows us to adopt Span types in the API surface in the future now that the internal implementations are based upon span. I ran our benchmarks before and after this change and found no difference in performance when switching from unsafe buffers to spans.