@@ -105,6 +105,20 @@ impl Default for FileSystemOs {
105105 }
106106}
107107
108+ fn read_to_string ( path : & Path ) -> io:: Result < String > {
109+ // `simdutf8` is faster than `std::str::from_utf8` which `fs::read_to_string` uses internally
110+ let bytes = std:: fs:: read ( path) ?;
111+ if simdutf8:: basic:: from_utf8 ( & bytes) . is_err ( ) {
112+ // Same error as `fs::read_to_string` produces (`io::Error::INVALID_UTF8`)
113+ return Err ( io:: Error :: new (
114+ io:: ErrorKind :: InvalidData ,
115+ "stream did not contain valid UTF-8" ,
116+ ) ) ;
117+ }
118+ // SAFETY: `simdutf8` has ensured it's a valid UTF-8 string
119+ Ok ( unsafe { String :: from_utf8_unchecked ( bytes) } )
120+ }
121+
108122impl FileSystem for FileSystemOs {
109123 fn read_to_string ( & self , path : & Path ) -> io:: Result < String > {
110124 cfg_if ! {
@@ -113,11 +127,11 @@ impl FileSystem for FileSystemOs {
113127 VPath :: Zip ( info) => {
114128 self . pnp_lru. read_to_string( info. physical_base_path( ) , info. zip_path)
115129 }
116- VPath :: Virtual ( info) => fs :: read_to_string( info. physical_base_path( ) ) ,
117- VPath :: Native ( path) => fs :: read_to_string( path) ,
130+ VPath :: Virtual ( info) => read_to_string( & info. physical_base_path( ) ) ,
131+ VPath :: Native ( path) => read_to_string( & path) ,
118132 }
119133 } else {
120- fs :: read_to_string( path)
134+ read_to_string( path)
121135 }
122136 }
123137 }
0 commit comments