Convert read file content directly to strings #1180
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I don't like unsafe, but in this case I believe it to be reasonable.
When reading from a filesystem, the returned bytes must necessarily never be touched by the underlying filesystem ever again. Otherwise, the caller would experience data races or be unable to modify the data. This means that in effect, the FS is guaranteed to no longer "own" that data, and the caller can do whatever it wants with it.
Our FS abstraction works with strings, but the underlying FSs all provide
[]byte
. Given the above, we can skip the typicalstring
to[]byte
copy and "safely" useunsafe.String
to directly convert it.This nets about a 50% improvement in time and memory allocation on non-trivial files. It's not much, but for larger files like
checker.ts
, it means going from a 1 ms read to a 0.5 ms read.I'm not totally sure if we should do this (other stuff is more expensive), but this is an improvement, so...