Skip to content

Conversation

@jmschonfeld
Copy link
Contributor

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.

@jmschonfeld jmschonfeld force-pushed the span-adoption/data-io branch from 72bf4f6 to 9e4ed05 Compare November 6, 2025 02:04
@jmschonfeld jmschonfeld marked this pull request as draft November 6, 2025 02:05
@jmschonfeld
Copy link
Contributor Author

Moving to draft to sort out a few miscellaneous test failures

@parkera
Copy link
Contributor

parkera commented Nov 6, 2025

Looks good to me in principle, although I didn't try to figure out what the test failures are about.

@jmschonfeld jmschonfeld marked this pull request as ready for review November 6, 2025 18:52
@jmschonfeld
Copy link
Contributor Author

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) {
Copy link
Contributor

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.

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.

3 participants