Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit60f37d3

Browse files
committed
perf(wasm): remove pos Mutex
1 parentba3f0ae commit60f37d3

File tree

5 files changed

+73
-51
lines changed

5 files changed

+73
-51
lines changed

‎Cargo.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name ="tokio-fs-ext"
3-
version ="0.5.2"
3+
version ="0.5.5"
44
edition ="2024"
55
authors = ["xusd320"]
66
description ="Extend tokio fs to be compatible with native and wasm"

‎src/fs/wasm/file.rs‎

Lines changed: 68 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::{
22
io::{self,SeekFrom},
33
path::Path,
44
pin::Pin,
5-
sync::Mutex,
65
task::{Context,Poll},
76
};
87

@@ -18,7 +17,7 @@ use super::{
1817
#[derive(Debug)]
1918
pubstructFile{
2019
pub(super)sync_access_handle:FileSystemSyncAccessHandle,
21-
pub(super)pos:Mutex<u64>,
20+
pub(super)pos:Option<u64>,
2221
}
2322

2423
implFile{
@@ -81,27 +80,53 @@ impl File {
8180
}
8281

8382
implFile{
84-
pub(crate)fnread_with_buf(&self,buf:&mut[u8]) -> io::Result<u64>{
85-
let options =FileSystemReadWriteOptions::new();
86-
options.set_at(*self.pos.lock().unwrap()asf64);
87-
let size =self
88-
.sync_access_handle
89-
.read_with_u8_array_and_options(buf,&options)
90-
.map_err(|err|OpfsError::from(err).into_io_err())?asu64;
91-
Ok(size)
92-
}
93-
94-
pub(crate)fnwrite_with_buf(&self,buf:implAsRef<[u8]>) -> io::Result<u64>{
95-
let options =FileSystemReadWriteOptions::new();
96-
options.set_at(*self.pos.lock().unwrap()asf64);
97-
let size =self
98-
.sync_access_handle
99-
.write_with_u8_array_and_options(buf.as_ref(),&options)
100-
.map_err(|err|OpfsError::from(err).into_io_err())?asu64;
101-
Ok(size)
102-
}
103-
104-
pub(crate)fnflush(&self) -> io::Result<()>{
83+
pub(crate)fnread_to_buf(&mutself,buf:&mut[u8]) -> io::Result<u64>{
84+
matchself.pos{
85+
Some(pos) =>{
86+
let options =FileSystemReadWriteOptions::new();
87+
options.set_at(posasf64);
88+
let size =self
89+
.sync_access_handle
90+
.read_with_u8_array_and_options(buf,&options)
91+
.map_err(|err|OpfsError::from(err).into_io_err())?
92+
asu64;
93+
Ok(size)
94+
}
95+
None =>{
96+
let size =self
97+
.sync_access_handle
98+
.read_with_u8_array(buf)
99+
.map_err(|err|OpfsError::from(err).into_io_err())?
100+
asu64;
101+
Ok(size)
102+
}
103+
}
104+
}
105+
106+
pub(crate)fnwrite_with_buf(&mutself,buf:implAsRef<[u8]>) -> io::Result<u64>{
107+
matchself.pos{
108+
Some(pos) =>{
109+
let options =FileSystemReadWriteOptions::new();
110+
options.set_at(posasf64);
111+
let size =self
112+
.sync_access_handle
113+
.write_with_u8_array_and_options(buf.as_ref(),&options)
114+
.map_err(|err|OpfsError::from(err).into_io_err())?
115+
asu64;
116+
Ok(size)
117+
}
118+
None =>{
119+
let size =self
120+
.sync_access_handle
121+
.write_with_u8_array(buf.as_ref())
122+
.map_err(|err|OpfsError::from(err).into_io_err())?
123+
asu64;
124+
Ok(size)
125+
}
126+
}
127+
}
128+
129+
pub(super)fnflush(&self) -> io::Result<()>{
105130
self.sync_access_handle
106131
.flush()
107132
.map_err(|err|OpfsError::from(err).into_io_err())
@@ -120,29 +145,27 @@ impl Drop for File {
120145

121146
implAsyncReadforFile{
122147
fnpoll_read(
123-
self:Pin<&mutSelf>,
148+
mutself:Pin<&mutSelf>,
124149
_cx:&mutContext<'_>,
125150
buf:&mut[u8],
126151
) ->Poll<io::Result<usize>>{
127-
letsize =self.read_with_buf(buf)?;
152+
letoffset =self.read_to_buf(buf)?;
128153

129-
letmut pos =self.pos.lock().unwrap();
130-
*pos += size;
154+
self.pos =Some(self.pos.unwrap_or_default() + offset);
131155

132-
Poll::Ready(Ok(sizeasusize))
156+
Poll::Ready(Ok(offsetasusize))
133157
}
134158
}
135159

136160
implAsyncWriteforFile{
137161
fnpoll_write(
138-
self:Pin<&mutSelf>,
162+
mutself:Pin<&mutSelf>,
139163
_cx:&mutContext<'_>,
140164
buf:&[u8],
141165
) ->Poll<Result<usize, io::Error>>{
142166
let offset =self.write_with_buf(buf)?;
143167

144-
letmut pos =self.pos.lock().unwrap();
145-
*pos += offset;
168+
self.pos =Some(self.pos.unwrap_or_default() + offset);
146169

147170
Poll::Ready(Ok(offsetasusize))
148171
}
@@ -160,27 +183,30 @@ impl AsyncWrite for File {
160183

161184
implAsyncSeekforFile{
162185
fnpoll_seek(
163-
self:Pin<&mutSelf>,
186+
mutself:Pin<&mutSelf>,
164187
_cx:&mutContext<'_>,
165188
position:SeekFrom,
166189
) ->Poll<io::Result<u64>>{
167-
letmut pos =self.pos.lock().unwrap();
168190
match position{
169191
SeekFrom::Start(offset) =>{
170-
*pos = offset;
192+
self.pos =Some(offset);
171193
}
172194
SeekFrom::End(offset) =>{
173-
*pos =self
174-
.size()?
175-
.checked_add_signed(offset)
176-
.ok_or(io::Error::from(io::ErrorKind::InvalidInput))?;
195+
self.pos =Some(
196+
self.size()?
197+
.checked_add_signed(offset)
198+
.ok_or(io::Error::from(io::ErrorKind::InvalidInput))?,
199+
);
177200
}
178201
SeekFrom::Current(offset) =>{
179-
*pos = pos
180-
.checked_add_signed(offset)
181-
.ok_or(io::Error::from(io::ErrorKind::InvalidInput))?;
202+
self.pos =Some(
203+
self.pos
204+
.unwrap_or_default()
205+
.checked_add_signed(offset)
206+
.ok_or(io::Error::from(io::ErrorKind::InvalidInput))?,
207+
);
182208
}
183209
}
184-
Poll::Ready(Ok(*pos))
210+
Poll::Ready(Ok(self.pos.unwrap_or_default()))
185211
}
186212
}

‎src/fs/wasm/opfs/open_file.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{io, path::Path, sync::Mutex};
1+
use std::{io, path::Path};
22

33
use js_sys::{Function,Promise,Reflect};
44
use wasm_bindgen::{JsCast,JsValue};
@@ -55,7 +55,7 @@ pub(crate) async fn open_file(
5555
};
5656
Ok(File{
5757
sync_access_handle,
58-
pos:Mutex::new(0),
58+
pos:None,
5959
})
6060
}
6161

‎src/fs/wasm/read.rs‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
use std::{io, path::Path};
22

3-
use futures::io::AsyncSeekExt;
4-
53
usesuper::OpenOptions;
64

75
pubasyncfnread(path:implAsRef<Path>) -> io::Result<Vec<u8>>{
86
letmut file =OpenOptions::new().read(true).open(path).await?;
97

10-
file.seek(io::SeekFrom::Start(0)).await?;
11-
128
let file_size = file.size()?;
139

1410
letmut buf =vec![0; file_sizeasusize];
1511

16-
file.read_with_buf(&mut buf)?;
12+
file.read_to_buf(&mut buf)?;
1713

1814
Ok(buf)
1915
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp