Browse Source

[ntex] optimization and fix (#10401)

fakeshadow 2 days ago
parent
commit
172fda7c83
1 changed files with 15 additions and 15 deletions
  1. 15 15
      frameworks/Rust/ntex/src/db.rs

+ 15 - 15
frameworks/Rust/ntex/src/db.rs

@@ -1,4 +1,4 @@
-use std::{borrow::Cow, cell::RefCell};
+use std::cell::RefCell;
 
 use nanorand::{Rng, WyRand};
 use ntex::util::{Bytes, BytesMut};
@@ -15,15 +15,15 @@ pub struct World {
 }
 
 #[derive(Debug, sonic_rs::Serialize)]
-pub struct Fortune {
+pub struct Fortune<'a> {
     pub id: i32,
-    pub message: Cow<'static, str>,
+    pub message: &'a str,
 }
 
 #[derive(yarte::TemplateBytes)]
 #[template(path = "fortune.hbs")]
-pub struct FortunesTemplate<'a> {
-    pub fortunes: &'a Vec<Fortune>,
+pub struct FortunesTemplate<'a, 'b> {
+    pub fortunes: &'a [Fortune<'b>],
 }
 
 /// Postgres interface
@@ -34,7 +34,6 @@ pub struct PgConnection {
     rng: WyRand,
     updates: Statement,
     buf: RefCell<BytesMut>,
-    fbuf: RefCell<Vec<Fortune>>,
 }
 
 impl PgConnection {
@@ -57,7 +56,6 @@ impl PgConnection {
             updates,
             rng: WyRand::new(),
             buf: RefCell::new(BytesMut::with_capacity(10 * 1024 * 1024)),
-            fbuf: RefCell::new(Vec::with_capacity(64)),
         }
     }
 }
@@ -125,11 +123,13 @@ impl PgConnection {
             });
             queries.push(self.cl.query_one(&self.world, &[&ids[idx]]));
         });
-        let _ = self
-            .cl
-            .query(&self.updates, &[&ids, &numbers])
-            .await
-            .unwrap();
+        let update = self.cl.query(&self.updates, &[&ids, &numbers]);
+
+        for query in queries {
+            let _rand: i32 = query.await.unwrap().get(1);
+        }
+
+        update.await.unwrap();
 
         let mut body = self.buf.borrow_mut();
         utils::reserve(&mut body, 2 * 1024);
@@ -140,14 +140,14 @@ impl PgConnection {
     pub async fn tell_fortune(&self) -> Bytes {
         let rows = self.cl.query_raw(&self.fortune, &[]).await.unwrap();
 
-        let mut fortunes = self.fbuf.borrow_mut();
+        let mut fortunes = Vec::with_capacity(16);
         fortunes.push(Fortune {
             id: 0,
-            message: Cow::Borrowed("Additional fortune added at request time."),
+            message: "Additional fortune added at request time.",
         });
         fortunes.extend(rows.iter().map(|row| Fortune {
             id: row.get(0),
-            message: Cow::Owned(row.get(1)),
+            message: row.get(1),
         }));
         fortunes.sort_by(|it, next| it.message.cmp(&next.message));