Bläddra i källkod

may_minihttp use mimalloc as default allocator (#4917)

* may_minihttp use mimalloc as default allocator

* may_minihttp install cmake for mimalloc build in dockerfile
Xudong Huang 6 år sedan
förälder
incheckning
3b9b83db02

+ 1 - 0
frameworks/Rust/may-minihttp/Cargo.toml

@@ -4,6 +4,7 @@ version = "0.1.0"
 authors = ["Xudong Huang <[email protected]>"]
 
 [dependencies]
+mimalloc = "0.1"
 num_cpus = "1.0"
 serde = { version = "1.0", features = ["derive"] }
 serde_derive = "1.0"

+ 2 - 0
frameworks/Rust/may-minihttp/may-minihttp.dockerfile

@@ -1,5 +1,7 @@
 FROM rust:1.36
 
+RUN apt update -yqq && apt install -yqq cmake
+
 ADD ./ /may
 WORKDIR /may
 

+ 17 - 8
frameworks/Rust/may-minihttp/src/main.rs

@@ -1,4 +1,5 @@
 extern crate may;
+extern crate mimalloc;
 extern crate num_cpus;
 #[macro_use]
 extern crate serde_derive;
@@ -8,6 +9,11 @@ extern crate serde_json;
 use may_minihttp::{HttpServer, HttpService, Request, Response};
 use std::io;
 
+use mimalloc::MiMalloc;
+
+#[global_allocator]
+static GLOBAL: MiMalloc = MiMalloc;
+
 #[derive(Serialize)]
 struct Message<'a> {
     message: &'a str,
@@ -21,19 +27,22 @@ impl HttpService for Techempower {
 
         // Bare-bones router
         match req.path() {
-            "/json" => {
-                resp.header("Content-Type", "application/json");
-                *resp.body_mut() = serde_json::to_vec(&Message {
-                    message: "Hello, World!",
-                })
-                .unwrap();
-            }
             "/plaintext" => {
                 resp.header("Content-Type", "text/plain")
                     .body("Hello, World!");
             }
+            "/json" => {
+                resp.header("Content-Type", "application/json");
+                serde_json::to_writer(
+                    resp.body_mut(),
+                    &Message {
+                        message: "Hello, World!",
+                    },
+                )
+                .unwrap();
+            }
             _ => {
-                resp.status_code(404, "Not Found");
+                resp.status_code("404", "Not Found");
             }
         }