@@ -68,7 +68,7 @@ impl PgConnection {
68
68
world,
69
69
updates,
70
70
rng : WyRand :: new ( ) ,
71
- buf : RefCell :: new ( BytesMut :: with_capacity ( 65535 ) ) ,
71
+ buf : RefCell :: new ( BytesMut :: with_capacity ( 10 * 1024 * 1024 ) ) ,
72
72
}
73
73
}
74
74
}
@@ -80,7 +80,7 @@ impl PgConnection {
80
80
let row = self . cl . query_one ( & self . world , & [ & random_id] ) . await . unwrap ( ) ;
81
81
82
82
let mut body = self . buf . borrow_mut ( ) ;
83
- utils:: reserve ( & mut body, 8 * 1024 ) ;
83
+ utils:: reserve ( & mut body, 1024 ) ;
84
84
World {
85
85
id : row. get ( 0 ) ,
86
86
randomnumber : row. get ( 1 ) ,
@@ -107,7 +107,7 @@ impl PgConnection {
107
107
}
108
108
109
109
let mut body = self . buf . borrow_mut ( ) ;
110
- utils:: reserve ( & mut body, 8 * 1024 ) ;
110
+ utils:: reserve ( & mut body, 2 * 1024 ) ;
111
111
body. put_u8 ( b'[' ) ;
112
112
worlds. iter ( ) . for_each ( |w| {
113
113
w. to_bytes_mut ( & mut * body) ;
@@ -119,7 +119,7 @@ impl PgConnection {
119
119
}
120
120
121
121
pub async fn update ( & self , num : usize ) -> Bytes {
122
- let mut rng = self . rng . clone ( ) ;
122
+ let mut rng = nanorand :: tls_rng ( ) ;
123
123
let mut queries = SmallVec :: < [ _ ; 32 ] > :: new ( ) ;
124
124
( 0 ..num) . for_each ( |_| {
125
125
let w_id = ( rng. generate :: < u32 > ( ) % 10_000 + 1 ) as i32 ;
@@ -146,7 +146,7 @@ impl PgConnection {
146
146
let _ = self . cl . query ( & self . updates [ num - 1 ] , & params) . await ;
147
147
148
148
let mut body = self . buf . borrow_mut ( ) ;
149
- utils:: reserve ( & mut body, 8 * 1024 ) ;
149
+ utils:: reserve ( & mut body, 2 * 1024 ) ;
150
150
body. put_u8 ( b'[' ) ;
151
151
worlds. iter ( ) . for_each ( |w| {
152
152
w. to_bytes_mut ( & mut * body) ;
@@ -158,25 +158,20 @@ impl PgConnection {
158
158
}
159
159
160
160
pub async fn tell_fortune ( & self ) -> Bytes {
161
- let fut = self . cl . query_raw ( & self . fortune , & [ ] ) ;
161
+ let rows = self . cl . query_raw ( & self . fortune , & [ ] ) . await . unwrap ( ) ;
162
162
163
- let rows = fut. await . unwrap ( ) ;
164
163
let mut fortunes: SmallVec < [ _ ; 32 ] > = smallvec:: smallvec![ Fortune {
165
164
id: 0 ,
166
165
message: Cow :: Borrowed ( "Additional fortune added at request time." ) ,
167
166
} ] ;
168
-
169
- for row in rows {
170
- fortunes. push ( Fortune {
171
- id : row. get ( 0 ) ,
172
- message : Cow :: Owned ( row. get ( 1 ) ) ,
173
- } ) ;
174
- }
175
-
167
+ fortunes. extend ( rows. iter ( ) . map ( |row| Fortune {
168
+ id : row. get ( 0 ) ,
169
+ message : Cow :: Owned ( row. get ( 1 ) ) ,
170
+ } ) ) ;
176
171
fortunes. sort_by ( |it, next| it. message . cmp ( & next. message ) ) ;
177
172
178
173
let mut body = std:: mem:: replace ( & mut * self . buf . borrow_mut ( ) , BytesMut :: new ( ) ) ;
179
- utils:: reserve ( & mut body, 8 * 1024 ) ;
174
+ utils:: reserve ( & mut body, 4 * 1024 ) ;
180
175
ywrite_html ! ( body, "{{> fortune }}" ) ;
181
176
let result = body. split ( ) . freeze ( ) ;
182
177
let _ = std:: mem:: replace ( & mut * self . buf . borrow_mut ( ) , body) ;
0 commit comments