Skip to content

Commit 8429dc7

Browse files
authored
Merge pull request #254 from Berrysoft/bench-improve
2 parents dbccc93 + 40ccaed commit 8429dc7

File tree

28 files changed

+519
-661
lines changed

28 files changed

+519
-661
lines changed

compio-buf/src/io_buf.rs

+46-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#[cfg(feature = "allocator_api")]
22
use std::alloc::Allocator;
3-
use std::mem::MaybeUninit;
3+
use std::{mem::MaybeUninit, rc::Rc, sync::Arc};
44

55
use crate::*;
66

@@ -126,7 +126,7 @@ unsafe impl<B: IoBuf + ?Sized> IoBuf for &'static mut B {
126126
}
127127

128128
unsafe impl<B: IoBuf + ?Sized, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoBuf
129-
for box_alloc!(B, A)
129+
for t_alloc!(Box, B, A)
130130
{
131131
fn as_buf_ptr(&self) -> *const u8 {
132132
(**self).as_buf_ptr()
@@ -141,7 +141,41 @@ unsafe impl<B: IoBuf + ?Sized, #[cfg(feature = "allocator_api")] A: Allocator +
141141
}
142142
}
143143

144-
unsafe impl<#[cfg(feature = "allocator_api")] A: Allocator + 'static> IoBuf for vec_alloc!(u8, A) {
144+
unsafe impl<B: IoBuf + ?Sized, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoBuf
145+
for t_alloc!(Rc, B, A)
146+
{
147+
fn as_buf_ptr(&self) -> *const u8 {
148+
(**self).as_buf_ptr()
149+
}
150+
151+
fn buf_len(&self) -> usize {
152+
(**self).buf_len()
153+
}
154+
155+
fn buf_capacity(&self) -> usize {
156+
(**self).buf_capacity()
157+
}
158+
}
159+
160+
unsafe impl<B: IoBuf + ?Sized, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoBuf
161+
for t_alloc!(Arc, B, A)
162+
{
163+
fn as_buf_ptr(&self) -> *const u8 {
164+
(**self).as_buf_ptr()
165+
}
166+
167+
fn buf_len(&self) -> usize {
168+
(**self).buf_len()
169+
}
170+
171+
fn buf_capacity(&self) -> usize {
172+
(**self).buf_capacity()
173+
}
174+
}
175+
176+
unsafe impl<#[cfg(feature = "allocator_api")] A: Allocator + 'static> IoBuf
177+
for t_alloc!(Vec, u8, A)
178+
{
145179
fn as_buf_ptr(&self) -> *const u8 {
146180
self.as_ptr()
147181
}
@@ -314,15 +348,15 @@ unsafe impl<B: IoBufMut + ?Sized> IoBufMut for &'static mut B {
314348
}
315349

316350
unsafe impl<B: IoBufMut + ?Sized, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoBufMut
317-
for box_alloc!(B, A)
351+
for t_alloc!(Box, B, A)
318352
{
319353
fn as_buf_mut_ptr(&mut self) -> *mut u8 {
320354
(**self).as_buf_mut_ptr()
321355
}
322356
}
323357

324358
unsafe impl<#[cfg(feature = "allocator_api")] A: Allocator + 'static> IoBufMut
325-
for vec_alloc!(u8, A)
359+
for t_alloc!(Vec, u8, A)
326360
{
327361
fn as_buf_mut_ptr(&mut self) -> *mut u8 {
328362
self.as_mut_ptr()
@@ -449,7 +483,7 @@ impl<T: IoBuf, const N: usize> IoVectoredBuf for [T; N] {
449483
}
450484

451485
impl<T: IoBuf, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoVectoredBuf
452-
for vec_alloc!(T, A)
486+
for t_alloc!(Vec, T, A)
453487
{
454488
fn as_dyn_bufs(&self) -> impl Iterator<Item = &dyn IoBuf> {
455489
self.iter().map(|buf| buf as &dyn IoBuf)
@@ -540,7 +574,7 @@ impl<T: IoBufMut, const N: usize> IoVectoredBufMut for [T; N] {
540574
}
541575

542576
impl<T: IoBufMut, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoVectoredBufMut
543-
for vec_alloc!(T, A)
577+
for t_alloc!(Vec, T, A)
544578
{
545579
fn as_dyn_mut_bufs(&mut self) -> impl Iterator<Item = &mut dyn IoBufMut> {
546580
self.iter_mut().map(|buf| buf as &mut dyn IoBufMut)
@@ -599,7 +633,7 @@ impl<T: IoBuf, const N: usize> IoIndexedBuf for [T; N] {
599633
}
600634

601635
impl<T: IoBuf, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoIndexedBuf
602-
for vec_alloc!(T, A)
636+
for t_alloc!(Vec, T, A)
603637
{
604638
fn buf_nth(&self, n: usize) -> Option<&dyn IoBuf> {
605639
self.get(n).map(|b| b as _)
@@ -638,7 +672,7 @@ impl<T: IoBufMut, const N: usize> IoIndexedBufMut for [T; N] {
638672
}
639673

640674
impl<T: IoBufMut, #[cfg(feature = "allocator_api")] A: Allocator + 'static> IoIndexedBufMut
641-
for vec_alloc!(T, A)
675+
for t_alloc!(Vec, T, A)
642676
{
643677
fn buf_nth_mut(&mut self, n: usize) -> Option<&mut dyn IoBufMut> {
644678
self.get_mut(n).map(|b| b as _)
@@ -670,14 +704,14 @@ impl<B: SetBufInit + ?Sized> SetBufInit for &'static mut B {
670704
}
671705

672706
impl<B: SetBufInit + ?Sized, #[cfg(feature = "allocator_api")] A: Allocator + 'static> SetBufInit
673-
for box_alloc!(B, A)
707+
for t_alloc!(Box, B, A)
674708
{
675709
unsafe fn set_buf_init(&mut self, len: usize) {
676710
(**self).set_buf_init(len)
677711
}
678712
}
679713

680-
impl<#[cfg(feature = "allocator_api")] A: Allocator + 'static> SetBufInit for vec_alloc!(u8, A) {
714+
impl<#[cfg(feature = "allocator_api")] A: Allocator + 'static> SetBufInit for t_alloc!(Vec, u8, A) {
681715
unsafe fn set_buf_init(&mut self, len: usize) {
682716
if (**self).buf_len() < len {
683717
self.set_len(len);
@@ -738,7 +772,7 @@ impl<T: IoBufMut, const N: usize> SetBufInit for [T; N] {
738772
}
739773

740774
impl<T: IoBufMut, #[cfg(feature = "allocator_api")] A: Allocator + 'static> SetBufInit
741-
for vec_alloc!(T, A)
775+
for t_alloc!(Vec, T, A)
742776
{
743777
unsafe fn set_buf_init(&mut self, len: usize) {
744778
default_set_buf_init(self.iter_mut(), len)

compio-buf/src/lib.rs

+6-24
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,17 @@ pub trait IntoInner {
4444
#[cfg(not(feature = "allocator_api"))]
4545
#[macro_export]
4646
#[doc(hidden)]
47-
macro_rules! vec_alloc {
48-
($t:ty, $a:ident) => {
49-
Vec<$t>
47+
macro_rules! t_alloc {
48+
($b:tt, $t:ty, $a:ident) => {
49+
$b<$t>
5050
};
5151
}
5252

5353
#[cfg(feature = "allocator_api")]
5454
#[macro_export]
5555
#[doc(hidden)]
56-
macro_rules! vec_alloc {
57-
($t:ty, $a:ident) => {
58-
Vec<$t, $a>
59-
};
60-
}
61-
62-
#[cfg(feature = "allocator_api")]
63-
#[macro_export]
64-
#[doc(hidden)]
65-
macro_rules! box_alloc {
66-
($t:ty, $a:ident) => {
67-
Box<$t, $a>
68-
};
69-
}
70-
71-
#[cfg(not(feature = "allocator_api"))]
72-
#[macro_export]
73-
#[doc(hidden)]
74-
macro_rules! box_alloc {
75-
($t:ty, $a:ident) => {
76-
Box<$t>
56+
macro_rules! t_alloc {
57+
($b:tt, $t:ty, $a:ident) => {
58+
$b<$t, $a>
7759
};
7860
}

compio-dispatcher/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@ impl Dispatcher {
5656
.build()
5757
.expect("cannot create compio runtime")
5858
.block_on(async move {
59-
let rt = Runtime::current();
6059
while let Ok(f) = receiver.recv_async().await {
6160
let fut = (f)();
6261
if builder.concurrent {
63-
rt.spawn(fut).detach()
62+
compio_runtime::spawn(fut).detach()
6463
} else {
6564
fut.await
6665
}

compio-driver/src/iour/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,11 @@ impl Driver {
135135
}
136136

137137
fn poll_entries(&mut self, entries: &mut impl Extend<Entry>) -> bool {
138-
while let Some(entry) = self.pool_completed.pop() {
139-
entries.extend(Some(entry));
138+
// Cheaper than pop.
139+
if !self.pool_completed.is_empty() {
140+
while let Some(entry) = self.pool_completed.pop() {
141+
entries.extend(Some(entry));
142+
}
140143
}
141144

142145
let mut cqueue = self.inner.completion();

compio-fs/src/async_fd.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use compio_driver::{
1212
AsRawFd, SharedFd, ToSharedFd,
1313
};
1414
use compio_io::{AsyncRead, AsyncWrite};
15-
use compio_runtime::{Attacher, Runtime};
15+
use compio_runtime::Attacher;
1616
#[cfg(unix)]
1717
use {
1818
compio_buf::{IoVectoredBuf, IoVectoredBufMut},
@@ -63,22 +63,14 @@ impl<T: AsRawFd + 'static> AsyncRead for &AsyncFd<T> {
6363
async fn read<B: IoBufMut>(&mut self, buf: B) -> BufResult<usize, B> {
6464
let fd = self.inner.to_shared_fd();
6565
let op = Recv::new(fd, buf);
66-
Runtime::current()
67-
.submit(op)
68-
.await
69-
.into_inner()
70-
.map_advanced()
66+
compio_runtime::submit(op).await.into_inner().map_advanced()
7167
}
7268

7369
#[cfg(unix)]
7470
async fn read_vectored<V: IoVectoredBufMut>(&mut self, buf: V) -> BufResult<usize, V> {
7571
let fd = self.inner.to_shared_fd();
7672
let op = RecvVectored::new(fd, buf);
77-
Runtime::current()
78-
.submit(op)
79-
.await
80-
.into_inner()
81-
.map_advanced()
73+
compio_runtime::submit(op).await.into_inner().map_advanced()
8274
}
8375
}
8476

@@ -109,14 +101,14 @@ impl<T: AsRawFd + 'static> AsyncWrite for &AsyncFd<T> {
109101
async fn write<B: IoBuf>(&mut self, buf: B) -> BufResult<usize, B> {
110102
let fd = self.inner.to_shared_fd();
111103
let op = Send::new(fd, buf);
112-
Runtime::current().submit(op).await.into_inner()
104+
compio_runtime::submit(op).await.into_inner()
113105
}
114106

115107
#[cfg(unix)]
116108
async fn write_vectored<V: IoVectoredBuf>(&mut self, buf: V) -> BufResult<usize, V> {
117109
let fd = self.inner.to_shared_fd();
118110
let op = SendVectored::new(fd, buf);
119-
Runtime::current().submit(op).await.into_inner()
111+
compio_runtime::submit(op).await.into_inner()
120112
}
121113

122114
async fn flush(&mut self) -> io::Result<()> {

compio-fs/src/file.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use compio_driver::{
77
ToSharedFd,
88
};
99
use compio_io::{AsyncReadAt, AsyncWriteAt};
10-
use compio_runtime::{Attacher, Runtime};
10+
use compio_runtime::Attacher;
1111
#[cfg(unix)]
1212
use {
1313
compio_buf::{IoVectoredBuf, IoVectoredBufMut},
@@ -71,7 +71,7 @@ impl File {
7171
.await;
7272
if let Some(fd) = fd {
7373
let op = CloseFile::new(fd.into());
74-
Runtime::current().submit(op).await.0?;
74+
compio_runtime::submit(op).await.0?;
7575
}
7676
Ok(())
7777
}
@@ -90,7 +90,7 @@ impl File {
9090
#[cfg(unix)]
9191
pub async fn metadata(&self) -> io::Result<Metadata> {
9292
let op = FileStat::new(self.to_shared_fd());
93-
let BufResult(res, op) = Runtime::current().submit(op).await;
93+
let BufResult(res, op) = compio_runtime::submit(op).await;
9494
res.map(|_| Metadata::from_stat(op.into_inner()))
9595
}
9696

@@ -121,7 +121,7 @@ impl File {
121121

122122
async fn sync_impl(&self, datasync: bool) -> io::Result<()> {
123123
let op = Sync::new(self.to_shared_fd(), datasync);
124-
Runtime::current().submit(op).await.0?;
124+
compio_runtime::submit(op).await.0?;
125125
Ok(())
126126
}
127127

@@ -153,11 +153,7 @@ impl AsyncReadAt for File {
153153
async fn read_at<T: IoBufMut>(&self, buffer: T, pos: u64) -> BufResult<usize, T> {
154154
let fd = self.inner.to_shared_fd();
155155
let op = ReadAt::new(fd, pos, buffer);
156-
Runtime::current()
157-
.submit(op)
158-
.await
159-
.into_inner()
160-
.map_advanced()
156+
compio_runtime::submit(op).await.into_inner().map_advanced()
161157
}
162158

163159
#[cfg(unix)]
@@ -168,11 +164,7 @@ impl AsyncReadAt for File {
168164
) -> BufResult<usize, T> {
169165
let fd = self.inner.to_shared_fd();
170166
let op = ReadVectoredAt::new(fd, pos, buffer);
171-
Runtime::current()
172-
.submit(op)
173-
.await
174-
.into_inner()
175-
.map_advanced()
167+
compio_runtime::submit(op).await.into_inner().map_advanced()
176168
}
177169
}
178170

@@ -197,7 +189,7 @@ impl AsyncWriteAt for &File {
197189
async fn write_at<T: IoBuf>(&mut self, buffer: T, pos: u64) -> BufResult<usize, T> {
198190
let fd = self.inner.to_shared_fd();
199191
let op = WriteAt::new(fd, pos, buffer);
200-
Runtime::current().submit(op).await.into_inner()
192+
compio_runtime::submit(op).await.into_inner()
201193
}
202194

203195
#[cfg(unix)]
@@ -208,7 +200,7 @@ impl AsyncWriteAt for &File {
208200
) -> BufResult<usize, T> {
209201
let fd = self.inner.to_shared_fd();
210202
let op = WriteVectoredAt::new(fd, pos, buffer);
211-
Runtime::current().submit(op).await.into_inner()
203+
compio_runtime::submit(op).await.into_inner()
212204
}
213205
}
214206

compio-fs/src/metadata/unix.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ use std::{
88

99
use compio_buf::{BufResult, IntoInner};
1010
use compio_driver::{op::PathStat, syscall};
11-
use compio_runtime::Runtime;
1211

1312
use crate::path_string;
1413

1514
async fn metadata_impl(path: impl AsRef<Path>, follow_symlink: bool) -> io::Result<Metadata> {
1615
let path = path_string(path)?;
1716
let op = PathStat::new(path, follow_symlink);
18-
let BufResult(res, op) = Runtime::current().submit(op).await;
17+
let BufResult(res, op) = compio_runtime::submit(op).await;
1918
res.map(|_| Metadata::from_stat(op.into_inner()))
2019
}
2120

compio-fs/src/named_pipe.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::{ffi::OsStr, io, os::windows::io::FromRawHandle, ptr::null};
99
use compio_buf::{BufResult, IoBuf, IoBufMut};
1010
use compio_driver::{impl_raw_fd, op::ConnectNamedPipe, syscall, AsRawFd, RawFd, ToSharedFd};
1111
use compio_io::{AsyncRead, AsyncReadAt, AsyncWrite, AsyncWriteAt};
12-
use compio_runtime::Runtime;
1312
use widestring::U16CString;
1413
use windows_sys::Win32::{
1514
Storage::FileSystem::{
@@ -142,7 +141,7 @@ impl NamedPipeServer {
142141
/// ```
143142
pub async fn connect(&self) -> io::Result<()> {
144143
let op = ConnectNamedPipe::new(self.handle.to_shared_fd());
145-
Runtime::current().submit(op).await.0?;
144+
compio_runtime::submit(op).await.0?;
146145
Ok(())
147146
}
148147

compio-fs/src/open_options/unix.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::{io, os::fd::FromRawFd, path::Path};
22

33
use compio_driver::{op::OpenFile, RawFd};
4-
use compio_runtime::Runtime;
54

65
use crate::{path_string, File};
76

@@ -87,7 +86,7 @@ impl OpenOptions {
8786
| (self.custom_flags as libc::c_int & !libc::O_ACCMODE);
8887
let p = path_string(p)?;
8988
let op = OpenFile::new(p, flags, self.mode);
90-
let fd = Runtime::current().submit(op).await.0? as RawFd;
89+
let fd = compio_runtime::submit(op).await.0? as RawFd;
9190
File::from_std(unsafe { std::fs::File::from_raw_fd(fd) })
9291
}
9392
}

0 commit comments

Comments
 (0)