1
1
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2
2
3
3
use std:: borrow:: Cow ;
4
+ use std:: cell:: RefCell ;
4
5
use std:: collections:: HashMap ;
5
6
use std:: collections:: HashSet ;
6
7
use std:: fs:: File ;
@@ -637,18 +638,17 @@ impl VfsRoot {
637
638
}
638
639
}
639
640
640
- #[ derive( Clone ) ]
641
641
struct FileBackedVfsFile {
642
642
file : VirtualFile ,
643
- pos : Arc < Mutex < u64 > > ,
643
+ pos : RefCell < u64 > ,
644
644
vfs : Arc < FileBackedVfs > ,
645
645
}
646
646
647
647
impl FileBackedVfsFile {
648
648
fn seek ( & self , pos : SeekFrom ) -> FsResult < u64 > {
649
649
match pos {
650
650
SeekFrom :: Start ( pos) => {
651
- * self . pos . lock ( ) = pos;
651
+ * self . pos . borrow_mut ( ) = pos;
652
652
Ok ( pos)
653
653
}
654
654
SeekFrom :: End ( offset) => {
@@ -659,7 +659,7 @@ impl FileBackedVfsFile {
659
659
. into ( ) ,
660
660
)
661
661
} else {
662
- let mut current_pos = self . pos . lock ( ) ;
662
+ let mut current_pos = self . pos . borrow_mut ( ) ;
663
663
* current_pos = if offset >= 0 {
664
664
self . file . offset . len - ( offset as u64 )
665
665
} else {
@@ -669,7 +669,7 @@ impl FileBackedVfsFile {
669
669
}
670
670
}
671
671
SeekFrom :: Current ( offset) => {
672
- let mut current_pos = self . pos . lock ( ) ;
672
+ let mut current_pos = self . pos . borrow_mut ( ) ;
673
673
if offset >= 0 {
674
674
* current_pos += offset as u64 ;
675
675
} else if -offset as u64 > * current_pos {
@@ -684,7 +684,7 @@ impl FileBackedVfsFile {
684
684
685
685
fn read_to_buf ( & self , buf : & mut [ u8 ] ) -> FsResult < usize > {
686
686
let read_pos = {
687
- let mut pos = self . pos . lock ( ) ;
687
+ let mut pos = self . pos . borrow_mut ( ) ;
688
688
let read_pos = * pos;
689
689
// advance the position due to the read
690
690
* pos = std:: cmp:: min ( self . file . offset . len , * pos + buf. len ( ) as u64 ) ;
@@ -698,7 +698,7 @@ impl FileBackedVfsFile {
698
698
699
699
fn read_to_end ( & self ) -> FsResult < Cow < ' static , [ u8 ] > > {
700
700
let read_pos = {
701
- let mut pos = self . pos . lock ( ) ;
701
+ let mut pos = self . pos . borrow_mut ( ) ;
702
702
let read_pos = * pos;
703
703
// todo(dsherret): should this always set it to the end of the file?
704
704
if * pos < self . file . offset . len {
@@ -734,12 +734,9 @@ impl deno_io::fs::File for FileBackedVfsFile {
734
734
self : Rc < Self > ,
735
735
mut buf : BufMutView ,
736
736
) -> 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) )
743
740
}
744
741
745
742
fn write_sync ( self : Rc < Self > , _buf : & [ u8 ] ) -> FsResult < usize > {
@@ -763,8 +760,8 @@ impl deno_io::fs::File for FileBackedVfsFile {
763
760
self . read_to_end ( )
764
761
}
765
762
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 ( )
768
765
}
769
766
770
767
fn chmod_sync ( self : Rc < Self > , _pathmode : u32 ) -> FsResult < ( ) > {
0 commit comments