File tree Expand file tree Collapse file tree 4 files changed +138
-0
lines changed Expand file tree Collapse file tree 4 files changed +138
-0
lines changed Original file line number Diff line number Diff line change
1
+ /target
2
+ ** /* .rs.bk
3
+ Cargo.lock
Original file line number Diff line number Diff line change
1
+ [package ]
2
+ name = " byte-pool"
3
+ version = " 0.1.0"
4
+ authors = [
" dignifiedquire <[email protected] >" ]
5
+ edition = " 2018"
6
+
7
+ # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8
+
9
+ [dependencies ]
Original file line number Diff line number Diff line change
1
+ #![ feature( test) ]
2
+
3
+ extern crate test;
4
+
5
+ use test:: Bencher ;
6
+
7
+ use byte_pool:: BytePool ;
8
+
9
+ #[ bench]
10
+ fn base_line_vec_4k ( b : & mut Bencher ) {
11
+ let size = 4 * 1024 ;
12
+
13
+ b. bytes = size as u64 ;
14
+
15
+ b. iter ( || {
16
+ // alloc
17
+ let mut buf = vec ! [ 0u8 ; size] ;
18
+ // write manual, simulating a Write::write call
19
+ for i in 0 ..size {
20
+ buf[ i] = 1 ;
21
+ }
22
+ } ) ;
23
+ }
24
+
25
+ #[ bench]
26
+ fn byte_pool_4k ( b : & mut Bencher ) {
27
+ let size = 4 * 1024 ;
28
+
29
+ b. bytes = size as u64 ;
30
+ let pool = BytePool :: new ( ) ;
31
+
32
+ b. iter ( || {
33
+ // alloc
34
+ let mut buf = pool. alloc ( size) ;
35
+ // write manual, simulating a Write::write call
36
+ for i in 0 ..size {
37
+ buf[ i] = 1 ;
38
+ }
39
+ } ) ;
40
+ }
Original file line number Diff line number Diff line change
1
+ use std:: ops:: { Deref , DerefMut } ;
2
+
3
+ pub struct BytePool {
4
+ // list: Vec<Block>,
5
+ }
6
+
7
+ pub struct Block < ' a > {
8
+ pool : & ' a BytePool ,
9
+ data : Box < [ u8 ] > ,
10
+ }
11
+
12
+ impl Default for BytePool {
13
+ fn default ( ) -> Self {
14
+ BytePool { /*list: Vec::new()*/ }
15
+ }
16
+ }
17
+
18
+ impl BytePool {
19
+ pub fn new ( ) -> Self {
20
+ BytePool :: default ( )
21
+ }
22
+
23
+ pub fn alloc ( & self , size : usize ) -> Block < ' _ > {
24
+ // TODO: reuse blocks
25
+
26
+ Block :: new ( size, & self )
27
+ }
28
+ }
29
+
30
+ impl < ' a > Block < ' a > {
31
+ fn new ( size : usize , pool : & ' a BytePool ) -> Self {
32
+ // TODO: use manual allocation
33
+
34
+ Block {
35
+ pool,
36
+ data : vec ! [ 0u8 ; size] . into_boxed_slice ( ) ,
37
+ }
38
+ }
39
+ }
40
+
41
+ impl < ' a > Deref for Block < ' a > {
42
+ type Target = [ u8 ] ;
43
+
44
+ #[ inline]
45
+ fn deref ( & self ) -> & Self :: Target {
46
+ & * self . data
47
+ }
48
+ }
49
+
50
+ impl < ' a > DerefMut for Block < ' a > {
51
+ #[ inline]
52
+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
53
+ & mut * self . data
54
+ }
55
+ }
56
+
57
+ #[ cfg( test) ]
58
+ mod tests {
59
+ use super :: * ;
60
+
61
+ #[ test]
62
+ fn basics ( ) {
63
+ let pool = BytePool :: new ( ) ;
64
+
65
+ for i in 0 ..100 {
66
+ let mut block_1k = pool. alloc ( 1 * 1024 ) ;
67
+ let mut block_4k = pool. alloc ( 4 * 1024 ) ;
68
+
69
+ for el in block_1k. deref_mut ( ) {
70
+ * el = i as u8 ;
71
+ }
72
+
73
+ for el in block_4k. deref_mut ( ) {
74
+ * el = i as u8 ;
75
+ }
76
+
77
+ for el in block_1k. deref ( ) {
78
+ assert_eq ! ( * el, i as u8 ) ;
79
+ }
80
+
81
+ for el in block_4k. deref ( ) {
82
+ assert_eq ! ( * el, i as u8 ) ;
83
+ }
84
+ }
85
+ }
86
+ }
You can’t perform that action at this time.
0 commit comments