mirror of
https://github.com/denoland/deno.git
synced 2024-12-18 13:22:55 -05:00
perf(compile): improve FileBackedVfsFile (#27299)
This commit is contained in:
parent
59dd5d21d4
commit
21a9e2d42b
1 changed files with 12 additions and 15 deletions
|
@ -1,6 +1,7 @@
|
||||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
use std::cell::RefCell;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
@ -637,10 +638,9 @@ impl VfsRoot {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
struct FileBackedVfsFile {
|
struct FileBackedVfsFile {
|
||||||
file: VirtualFile,
|
file: VirtualFile,
|
||||||
pos: Arc<Mutex<u64>>,
|
pos: RefCell<u64>,
|
||||||
vfs: Arc<FileBackedVfs>,
|
vfs: Arc<FileBackedVfs>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -648,7 +648,7 @@ impl FileBackedVfsFile {
|
||||||
fn seek(&self, pos: SeekFrom) -> FsResult<u64> {
|
fn seek(&self, pos: SeekFrom) -> FsResult<u64> {
|
||||||
match pos {
|
match pos {
|
||||||
SeekFrom::Start(pos) => {
|
SeekFrom::Start(pos) => {
|
||||||
*self.pos.lock() = pos;
|
*self.pos.borrow_mut() = pos;
|
||||||
Ok(pos)
|
Ok(pos)
|
||||||
}
|
}
|
||||||
SeekFrom::End(offset) => {
|
SeekFrom::End(offset) => {
|
||||||
|
@ -659,7 +659,7 @@ impl FileBackedVfsFile {
|
||||||
.into(),
|
.into(),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
let mut current_pos = self.pos.lock();
|
let mut current_pos = self.pos.borrow_mut();
|
||||||
*current_pos = if offset >= 0 {
|
*current_pos = if offset >= 0 {
|
||||||
self.file.offset.len - (offset as u64)
|
self.file.offset.len - (offset as u64)
|
||||||
} else {
|
} else {
|
||||||
|
@ -669,7 +669,7 @@ impl FileBackedVfsFile {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SeekFrom::Current(offset) => {
|
SeekFrom::Current(offset) => {
|
||||||
let mut current_pos = self.pos.lock();
|
let mut current_pos = self.pos.borrow_mut();
|
||||||
if offset >= 0 {
|
if offset >= 0 {
|
||||||
*current_pos += offset as u64;
|
*current_pos += offset as u64;
|
||||||
} else if -offset as u64 > *current_pos {
|
} else if -offset as u64 > *current_pos {
|
||||||
|
@ -684,7 +684,7 @@ impl FileBackedVfsFile {
|
||||||
|
|
||||||
fn read_to_buf(&self, buf: &mut [u8]) -> FsResult<usize> {
|
fn read_to_buf(&self, buf: &mut [u8]) -> FsResult<usize> {
|
||||||
let read_pos = {
|
let read_pos = {
|
||||||
let mut pos = self.pos.lock();
|
let mut pos = self.pos.borrow_mut();
|
||||||
let read_pos = *pos;
|
let read_pos = *pos;
|
||||||
// advance the position due to the read
|
// advance the position due to the read
|
||||||
*pos = std::cmp::min(self.file.offset.len, *pos + buf.len() as u64);
|
*pos = std::cmp::min(self.file.offset.len, *pos + buf.len() as u64);
|
||||||
|
@ -698,7 +698,7 @@ impl FileBackedVfsFile {
|
||||||
|
|
||||||
fn read_to_end(&self) -> FsResult<Cow<'static, [u8]>> {
|
fn read_to_end(&self) -> FsResult<Cow<'static, [u8]>> {
|
||||||
let read_pos = {
|
let read_pos = {
|
||||||
let mut pos = self.pos.lock();
|
let mut pos = self.pos.borrow_mut();
|
||||||
let read_pos = *pos;
|
let read_pos = *pos;
|
||||||
// todo(dsherret): should this always set it to the end of the file?
|
// todo(dsherret): should this always set it to the end of the file?
|
||||||
if *pos < self.file.offset.len {
|
if *pos < self.file.offset.len {
|
||||||
|
@ -734,12 +734,9 @@ impl deno_io::fs::File for FileBackedVfsFile {
|
||||||
self: Rc<Self>,
|
self: Rc<Self>,
|
||||||
mut buf: BufMutView,
|
mut buf: BufMutView,
|
||||||
) -> FsResult<(usize, BufMutView)> {
|
) -> FsResult<(usize, BufMutView)> {
|
||||||
let inner = (*self).clone();
|
// this is fast, no need to spawn a task
|
||||||
tokio::task::spawn(async move {
|
let nread = self.read_to_buf(&mut buf)?;
|
||||||
let nread = inner.read_to_buf(&mut buf)?;
|
Ok((nread, buf))
|
||||||
Ok((nread, buf))
|
|
||||||
})
|
|
||||||
.await?
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_sync(self: Rc<Self>, _buf: &[u8]) -> FsResult<usize> {
|
fn write_sync(self: Rc<Self>, _buf: &[u8]) -> FsResult<usize> {
|
||||||
|
@ -763,8 +760,8 @@ impl deno_io::fs::File for FileBackedVfsFile {
|
||||||
self.read_to_end()
|
self.read_to_end()
|
||||||
}
|
}
|
||||||
async fn read_all_async(self: Rc<Self>) -> FsResult<Cow<'static, [u8]>> {
|
async fn read_all_async(self: Rc<Self>) -> FsResult<Cow<'static, [u8]>> {
|
||||||
let inner = (*self).clone();
|
// this is fast, no need to spawn a task
|
||||||
tokio::task::spawn_blocking(move || inner.read_to_end()).await?
|
self.read_to_end()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn chmod_sync(self: Rc<Self>, _pathmode: u32) -> FsResult<()> {
|
fn chmod_sync(self: Rc<Self>, _pathmode: u32) -> FsResult<()> {
|
||||||
|
|
Loading…
Reference in a new issue