Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
bytes-sgx
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Markus Becker
bytes-sgx
Commits
17637665
Commit
17637665
authored
10 years ago
by
Carl Lerche
Browse files
Options
Downloads
Patches
Plain Diff
Add a quick benchmark
parent
7276213c
No related branches found
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
Cargo.toml
+1
-1
1 addition, 1 deletion
Cargo.toml
bench/bench.rs
+34
-0
34 additions, 0 deletions
bench/bench.rs
src/byte_buf.rs
+29
-16
29 additions, 16 deletions
src/byte_buf.rs
src/byte_str.rs
+1
-1
1 addition, 1 deletion
src/byte_str.rs
src/lib.rs
+16
-9
16 additions, 9 deletions
src/lib.rs
with
81 additions
and
27 deletions
Cargo.toml
+
1
−
1
View file @
17637665
...
...
@@ -9,7 +9,7 @@ license = "MIT"
[dev-dependencies]
rand
=
"0.1.2"
#
iobuf = "*"
iobuf
=
"*"
[[bench]]
...
...
This diff is collapsed.
Click to expand it.
bench/bench.rs
0 → 100644
+
34
−
0
View file @
17637665
#![feature(test,
core)]
use
bytes
::
ByteBuf
;
use
bytes
::
traits
::
*
;
use
iobuf
::{
RWIobuf
};
use
test
::
Bencher
;
extern
crate
bytes
;
extern
crate
iobuf
;
extern
crate
test
;
const
SIZE
:
usize
=
4_096
;
#[bench]
pub
fn
bench_byte_buf_fill_4kb
(
b
:
&
mut
Bencher
)
{
b
.iter
(||
{
let
mut
buf
=
ByteBuf
::
mut_with_capacity
(
SIZE
);
for
_
in
0
..
SIZE
{
buf
.write_slice
(
&
[
0
]);
}
});
}
#[bench]
pub
fn
bench_rw_iobuf_fill_4kb
(
b
:
&
mut
Bencher
)
{
b
.iter
(||
{
let
mut
buf
=
RWIobuf
::
new
(
SIZE
);
for
_
in
0
..
SIZE
{
let
_
=
buf
.fill
(
&
[
0
]);
}
});
}
This diff is collapsed.
Click to expand it.
src/byte_buf.rs
+
29
−
16
View file @
17637665
use
{
alloc
,
Bytes
,
SeqByteStr
,
BufResult
,
BufError
,
MAX_CAPACITY
};
use
traits
::{
Buf
,
MutBuf
,
ByteStr
};
use
{
alloc
,
Bytes
,
SeqByteStr
,
MAX_CAPACITY
};
use
traits
::{
Buf
,
MutBuf
,
MutBufExt
,
ByteStr
};
use
std
::{
cmp
,
ptr
};
use
std
::
num
::
UnsignedInt
;
...
...
@@ -9,6 +9,7 @@ use std::num::UnsignedInt;
*
*/
/// A `Buf` backed by a contiguous region of memory.
pub
struct
ByteBuf
{
mem
:
alloc
::
MemRef
,
cap
:
u32
,
...
...
@@ -17,6 +18,7 @@ pub struct ByteBuf {
}
impl
ByteBuf
{
/// Create a new `ByteBuf` by copying the contents of the given slice.
pub
fn
from_slice
(
bytes
:
&
[
u8
])
->
ByteBuf
{
let
mut
buf
=
ByteBuf
::
mut_with_capacity
(
bytes
.len
());
buf
.write
(
bytes
)
.ok
()
.expect
(
"unexpected failure"
);
...
...
@@ -105,45 +107,46 @@ impl ByteBuf {
}
}
#[inline]
pub
fn
to_bytes
(
self
)
->
Bytes
{
Bytes
::
of
(
self
.to_seq_byte_str
())
}
#[inline]
fn
pos
(
&
self
)
->
usize
{
self
.pos
as
usize
}
#[inline]
fn
lim
(
&
self
)
->
usize
{
self
.lim
as
usize
}
#[inline]
fn
remaining_u32
(
&
self
)
->
u32
{
self
.lim
-
self
.pos
}
fn
ensure_remaining
(
&
self
,
cnt
:
usize
)
->
BufResult
<
()
>
{
if
cnt
>
self
.remaining
()
{
return
Err
(
BufError
::
Overflow
);
}
Ok
(())
}
}
impl
Buf
for
ByteBuf
{
#[inline]
fn
remaining
(
&
self
)
->
usize
{
self
.remaining_u32
()
as
usize
}
#[inline]
fn
bytes
<
'a
>
(
&
'a
self
)
->
&
'a
[
u8
]
{
&
self
.mem
.bytes
()[
self
.pos
()
..
self
.lim
()]
}
#[inline]
fn
advance
(
&
mut
self
,
mut
cnt
:
usize
)
{
cnt
=
cmp
::
min
(
cnt
,
self
.remaining
());
self
.pos
+=
cnt
as
u32
;
}
#[inline]
fn
read_slice
(
&
mut
self
,
dst
:
&
mut
[
u8
])
->
usize
{
ByteBuf
::
read_slice
(
self
,
dst
)
}
...
...
@@ -225,18 +228,28 @@ impl MutByteBuf {
self
.buf.lim
=
self
.buf.cap
;
}
pub
fn
write_slice
(
&
mut
self
,
src
:
&
[
u8
])
->
BufResult
<
()
>
{
try
!
(
self
.buf
.ensure_remaining
(
src
.len
()));
#[inline]
pub
fn
write_slice
(
&
mut
self
,
src
:
&
[
u8
])
->
usize
{
let
cnt
=
src
.len
()
as
u32
;
let
rem
=
self
.buf
.remaining_u32
();
if
rem
<
cnt
{
self
.write_ptr
(
src
.as_ptr
(),
rem
)
}
else
{
self
.write_ptr
(
src
.as_ptr
(),
cnt
)
}
}
#[inline]
fn
write_ptr
(
&
mut
self
,
src
:
*
const
u8
,
len
:
u32
)
->
usize
{
unsafe
{
ptr
::
copy_nonoverlapping_memory
(
self
.buf.mem
.ptr
()
.offset
(
self
.buf.pos
as
isize
),
src
.as_ptr
(),
src
.len
());
}
src
,
len
as
usize
);
self
.buf.pos
+=
cnt
;
return
Ok
(());
self
.buf.pos
+=
len
;
len
as
usize
}
}
}
...
...
This diff is collapsed.
Click to expand it.
src/byte_str.rs
+
1
−
1
View file @
17637665
use
{
alloc
,
Bytes
,
ByteBuf
,
ROByteBuf
};
use
traits
::{
Buf
,
MutBuf
,
ByteStr
};
use
traits
::{
Buf
,
MutBuf
,
MutBufExt
,
ByteStr
};
use
std
::{
cmp
,
ops
};
/*
...
...
This diff is collapsed.
Click to expand it.
src/lib.rs
+
16
−
9
View file @
17637665
...
...
@@ -24,12 +24,12 @@ mod rope;
mod
slice
;
pub
mod
traits
{
pub
use
{
Buf
,
BufExt
,
MutBuf
,
ByteStr
};
pub
use
{
Buf
,
BufExt
,
MutBuf
,
MutBufExt
,
ByteStr
};
}
const
MAX_CAPACITY
:
usize
=
u32
::
MAX
as
usize
;
/// A trait for
object
s that provide random and sequential access to bytes.
/// A trait for
value
s that provide random and sequential access to bytes.
pub
trait
Buf
{
/// Returns the number of bytes that can be accessed from the Buf
...
...
@@ -113,12 +113,6 @@ pub trait MutBuf : Sized {
/// length between 0 and `Buf::remaining()`.
fn
mut_bytes
<
'a
>
(
&
'a
mut
self
)
->
&
'a
mut
[
u8
];
/// Read bytes from this Buf into the given sink and advance the cursor by
/// the number of bytes read.
fn
write
<
S
:
Source
>
(
&
mut
self
,
src
:
S
)
->
Result
<
usize
,
S
::
Error
>
{
src
.fill
(
self
)
}
/// Read bytes from this Buf into the given slice and advance the cursor by
/// the number of bytes read.
///
...
...
@@ -164,6 +158,13 @@ pub trait MutBuf : Sized {
}
}
pub
trait
MutBufExt
{
/// Write bytes from the given source into the current `MutBuf` and advance
/// the cursor by the number of bytes written.
fn
write
<
S
:
Source
>
(
&
mut
self
,
src
:
S
)
->
Result
<
usize
,
S
::
Error
>
;
}
/*
*
* ===== ByteStr =====
...
...
@@ -239,13 +240,19 @@ impl<B: Buf> BufExt for B {
}
}
impl
<
B
:
MutBuf
>
MutBufExt
for
B
{
fn
write
<
S
:
Source
>
(
&
mut
self
,
src
:
S
)
->
Result
<
usize
,
S
::
Error
>
{
src
.fill
(
self
)
}
}
/*
*
* ===== Sink / Source =====
*
*/
/// An
object
that reads bytes from a Buf into itself
/// An
value
that reads bytes from a Buf into itself
pub
trait
Sink
{
type
Error
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment