Skip to content

Commit 21a9e2d

Browse files
authored
perf(compile): improve FileBackedVfsFile (#27299)
1 parent 59dd5d2 commit 21a9e2d

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

cli/standalone/virtual_fs.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
22

33
use std::borrow::Cow;
4+
use std::cell::RefCell;
45
use std::collections::HashMap;
56
use std::collections::HashSet;
67
use std::fs::File;
@@ -637,18 +638,17 @@ impl VfsRoot {
637638
}
638639
}
639640

640-
#[derive(Clone)]
641641
struct FileBackedVfsFile {
642642
file: VirtualFile,
643-
pos: Arc<Mutex<u64>>,
643+
pos: RefCell<u64>,
644644
vfs: Arc<FileBackedVfs>,
645645
}
646646

647647
impl FileBackedVfsFile {
648648
fn seek(&self, pos: SeekFrom) -> FsResult<u64> {
649649
match pos {
650650
SeekFrom::Start(pos) => {
651-
*self.pos.lock() = pos;
651+
*self.pos.borrow_mut() = pos;
652652
Ok(pos)
653653
}
654654
SeekFrom::End(offset) => {
@@ -659,7 +659,7 @@ impl FileBackedVfsFile {
659659
.into(),
660660
)
661661
} else {
662-
let mut current_pos = self.pos.lock();
662+
let mut current_pos = self.pos.borrow_mut();
663663
*current_pos = if offset >= 0 {
664664
self.file.offset.len - (offset as u64)
665665
} else {
@@ -669,7 +669,7 @@ impl FileBackedVfsFile {
669669
}
670670
}
671671
SeekFrom::Current(offset) => {
672-
let mut current_pos = self.pos.lock();
672+
let mut current_pos = self.pos.borrow_mut();
673673
if offset >= 0 {
674674
*current_pos += offset as u64;
675675
} else if -offset as u64 > *current_pos {
@@ -684,7 +684,7 @@ impl FileBackedVfsFile {
684684

685685
fn read_to_buf(&self, buf: &mut [u8]) -> FsResult<usize> {
686686
let read_pos = {
687-
let mut pos = self.pos.lock();
687+
let mut pos = self.pos.borrow_mut();
688688
let read_pos = *pos;
689689
// advance the position due to the read
690690
*pos = std::cmp::min(self.file.offset.len, *pos + buf.len() as u64);
@@ -698,7 +698,7 @@ impl FileBackedVfsFile {
698698

699699
fn read_to_end(&self) -> FsResult<Cow<'static, [u8]>> {
700700
let read_pos = {
701-
let mut pos = self.pos.lock();
701+
let mut pos = self.pos.borrow_mut();
702702
let read_pos = *pos;
703703
// todo(dsherret): should this always set it to the end of the file?
704704
if *pos < self.file.offset.len {
@@ -734,12 +734,9 @@ impl deno_io::fs::File for FileBackedVfsFile {
734734
self: Rc<Self>,
735735
mut buf: BufMutView,
736736
) -> FsResult<(usize, BufMutView)> {
737-
let inner = (*self).clone();
738-
tokio::task::spawn(async move {
739-
let nread = inner.read_to_buf(&mut buf)?;
740-
Ok((nread, buf))
741-
})
742-
.await?
737+
// this is fast, no need to spawn a task
738+
let nread = self.read_to_buf(&mut buf)?;
739+
Ok((nread, buf))
743740
}
744741

745742
fn write_sync(self: Rc<Self>, _buf: &[u8]) -> FsResult<usize> {
@@ -763,8 +760,8 @@ impl deno_io::fs::File for FileBackedVfsFile {
763760
self.read_to_end()
764761
}
765762
async fn read_all_async(self: Rc<Self>) -> FsResult<Cow<'static, [u8]>> {
766-
let inner = (*self).clone();
767-
tokio::task::spawn_blocking(move || inner.read_to_end()).await?
763+
// this is fast, no need to spawn a task
764+
self.read_to_end()
768765
}
769766

770767
fn chmod_sync(self: Rc<Self>, _pathmode: u32) -> FsResult<()> {

0 commit comments

Comments
 (0)