Browse Source

Updated FaF framework dependencies and made a few performance tweaks (#6966)

* add faf framework

* add faf framework

* clean up code and slight reorg

* add HTTP methods and cleanup

* converted to a library format, updated docker to point to a tag, generally simplified

* simplified README.md

* fix pipelining issue

* fix release settings in Cargo.toml

* fix release settings in Cargo.toml

* faf: updated clang and made a few performance tweaks to the project

* updated FaF to match full '/plaintext' path
James Bates 3 years ago
parent
commit
8e1ae4234f

+ 0 - 81
frameworks/Rust/faf/Cargo.lock

@@ -1,81 +0,0 @@
-# This file is automatically @generated by Cargo.
-# It is not intended for manual editing.
-[[package]]
-name = "cc"
-version = "1.0.67"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e3c69b077ad434294d3ce9f1f6143a2a4b89a8a2d54ef813d85003a4fd1137fd"
-
-[[package]]
-name = "faf"
-version = "0.1.1"
-source = "git+https://github.com/errantmind/faf#013c81162f09ce376c0b761ed5531d1d47e33c94"
-dependencies = [
- "faf-pico-sys",
- "num_cpus",
- "sys-call",
-]
-
-[[package]]
-name = "faf-ex"
-version = "0.1.0"
-dependencies = [
- "faf",
- "mimalloc",
-]
-
-[[package]]
-name = "faf-pico-sys"
-version = "0.1.1"
-source = "git+https://github.com/errantmind/faf-pico-sys#7f273a13302a04ba93f4edbfda5ad3073a7832f9"
-dependencies = [
- "cc",
-]
-
-[[package]]
-name = "hermit-abi"
-version = "0.1.18"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c"
-dependencies = [
- "libc",
-]
-
-[[package]]
-name = "libc"
-version = "0.2.92"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "56d855069fafbb9b344c0f962150cd2c1187975cb1c22c1522c240d8c4986714"
-
-[[package]]
-name = "libmimalloc-sys"
-version = "0.1.21"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2396cf99d2f58611cd69f0efeee4af3d2e2c7b61bed433515029163aa567e65c"
-dependencies = [
- "cc",
-]
-
-[[package]]
-name = "mimalloc"
-version = "0.1.25"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1e7c6b11afd1e5e689ac96b6d18b1fc763398fe3d7eed99e8773426bc2033dfb"
-dependencies = [
- "libmimalloc-sys",
-]
-
-[[package]]
-name = "num_cpus"
-version = "1.13.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3"
-dependencies = [
- "hermit-abi",
- "libc",
-]
-
-[[package]]
-name = "sys-call"
-version = "0.1.1"
-source = "git+https://github.com/errantmind/sys-call-rs#46fc51c2dbc3c54e60424d695eae615910b2e99c"

+ 17 - 5
frameworks/Rust/faf/Cargo.toml

@@ -1,17 +1,29 @@
 [package]
 name = "faf-ex"
-version = "0.1.0"
+version = "0.2.0"
 authors = ["James Bates <[email protected]>"]
-edition = "2018"
+edition = "2021"
+
+
+[profile.dev]
+opt-level = 0
+debug = true
+debug-assertions = true
+overflow-checks = true
+lto = false
+panic = 'unwind'
+incremental = true
+codegen-units = 256
+rpath = false
 
 [profile.release]
 opt-level = 3
-panic = 'unwind'
+panic = 'abort'
 codegen-units = 1
 lto = "thin"
 debug = false
+incremental = false
 overflow-checks = false
 
 [dependencies]
-mimalloc = { version = "*", default-features = false }
-faf = { git = "https://github.com/errantmind/faf" }
+faf = { git = "https://github.com/errantmind/faf.git" } #, features = ["faf_debug"] }

+ 10 - 8
frameworks/Rust/faf/faf.dockerfile

@@ -1,14 +1,16 @@
-FROM errantmind/debian-faf:v2
+FROM errantmind/debian-faf:v3
 
 COPY ./Cargo.toml ./Cargo.toml
 COPY ./src ./src
-COPY ./merged.profdata ./merged.profdata
-ENV CC=/usr/bin/clang-12
-ENV CXX=/usr/bin/clang++-12
+ENV CC=/usr/bin/clang-13
+ENV CXX=/usr/bin/clang++-13
 RUN /root/.cargo/bin/cargo update
-RUN RUSTFLAGS="-Ctarget-cpu=native -Clinker=/usr/bin/clang-12 -Clink-arg=-fuse-ld=lld-12 -Clink-arg=-flto=thin \
-   -Clto=thin -Cembed-bitcode=yes -Copt-level=3 -Ccodegen-units=1 -Cforce-frame-pointers=n -Cprofile-use=/faf/merged.profdata" \
-   /root/.cargo/bin/cargo build --verbose --release && strip --strip-all target/release/faf-ex
+RUN RUSTFLAGS="-Ctarget-cpu=native -Ztune-cpu=native -Zmutable-noalias=yes -Clink-arg=-fexperimental-new-pass-manager \
+   -Clinker=/usr/bin/clang-13 -Clink-arg=-fuse-ld=/usr/bin/ld.lld-13 -Clink-arg=-flto=thin -Clto=thin -Copt-level=3 \
+   -Ccodegen-units=1 -Cpanic=abort -Cembed-bitcode=yes -Cforce-frame-pointers=n -Cdebug-assertions=no -Coverflow-checks=no \
+   -Ccontrol-flow-guard=no -Clink-dead-code=no -Zno-parallel-llvm" \
+   /root/.cargo/bin/cargo build --release --target x86_64-unknown-linux-gnu -Zbuild-std=panic_abort,core,std,alloc,proc_macro,compiler_builtins \
+   && strip ./target/x86_64-unknown-linux-gnu/release/faf-ex
 
 EXPOSE 8089
-CMD ./target/release/faf-ex
+CMD ./target/x86_64-unknown-linux-gnu/release/faf-ex

BIN
frameworks/Rust/faf/merged.profdata


+ 20 - 39
frameworks/Rust/faf/src/main.rs

@@ -1,19 +1,14 @@
-#![feature(core_intrinsics)]
-
-#[global_allocator]
-static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
+#![feature(core_intrinsics, asm)]
+#![feature(start, lang_items)]
+#![allow(clippy::missing_safety_doc, unused_imports, dead_code)]
 
+use core::intrinsics::likely;
 use faf::const_concat_bytes;
-use faf::const_config::*;
 use faf::const_http::*;
-use faf::extern_http_date;
 use faf::util::{const_len, memcmp};
-use std::intrinsics::likely;
 
-const ROUTE_PLAINTEXT: &[u8] = b"/p";
+const ROUTE_PLAINTEXT: &[u8] = b"/plaintext";
 const ROUTE_PLAINTEXT_LEN: usize = const_len(ROUTE_PLAINTEXT);
-// const ROUTE_JSON: &[u8] = b"/j";
-// const ROUTE_JSON_LEN: usize = const_len(ROUTE_JSON);
 
 const TEXT_PLAIN_CONTENT_TYPE: &[u8] = b"Content-Type: text/plain";
 const CONTENT_LENGTH: &[u8] = b"Content-Length: ";
@@ -35,35 +30,30 @@ const PLAINTEXT_BASE: &[u8] = const_concat_bytes!(
 
 const PLAINTEXT_BASE_LEN: usize = const_len(PLAINTEXT_BASE);
 
+const PLAINTEXT_TEST: &[u8] = b"HTTP/1.1 200 OK\r\nServer: F\r\nContent-Type: text/plain\r\nContent-Length: 13\r\nDate: Thu, 18 Nov 2021 23:15:07 GMT\r\n\r\nHello, World!";
+const PLAINTEXT_TEST_LEN: usize = const_len(PLAINTEXT_TEST);
+
 #[inline]
 fn cb(
-   method: *const i8,
+   method: *const u8,
    method_len: usize,
-   path: *const i8,
+   path: *const u8,
    path_len: usize,
-   _headers: &[faf::phr_header; MAX_HEADERS_TO_PARSE],
-   _num_headers: usize,
    response_buffer: *mut u8,
+   date_buff: *const u8,
 ) -> usize {
    unsafe {
       if likely(method_len >= GET_LEN && path_len >= ROUTE_PLAINTEXT_LEN) {
-         if likely(memcmp(GET.as_ptr() as *const i8, method, GET_LEN) == 0) {
-            // For performance purposes, this will successfully match '/p' to '/plaintext' and '/pickle'. Use with caution
-            if likely(memcmp(ROUTE_PLAINTEXT.as_ptr() as *const i8, path, ROUTE_PLAINTEXT_LEN) == 0) {
-               let mut date_buff = crate::extern_http_date::get_buff_with_date();
-               extern_http_date::get_http_date(&mut date_buff);
-               std::ptr::copy_nonoverlapping(PLAINTEXT_BASE.as_ptr(), response_buffer, PLAINTEXT_BASE_LEN);
-               std::ptr::copy_nonoverlapping(
-                  date_buff.as_ptr(),
-                  response_buffer.add(PLAINTEXT_BASE_LEN),
-                  DATE_LEN,
-               );
-               std::ptr::copy_nonoverlapping(
+         if likely(memcmp(GET.as_ptr(), method, GET_LEN) == 0) {
+            if likely(memcmp(ROUTE_PLAINTEXT.as_ptr(), path, ROUTE_PLAINTEXT_LEN) == 0) {
+               core::ptr::copy_nonoverlapping(PLAINTEXT_BASE.as_ptr(), response_buffer, PLAINTEXT_BASE_LEN);
+               core::ptr::copy_nonoverlapping(date_buff, response_buffer.add(PLAINTEXT_BASE_LEN), DATE_LEN);
+               core::ptr::copy_nonoverlapping(
                   CRLFCRLF.as_ptr(),
                   response_buffer.add(PLAINTEXT_BASE_LEN + DATE_LEN),
                   CRLFCRLF_LEN,
                );
-               std::ptr::copy_nonoverlapping(
+               core::ptr::copy_nonoverlapping(
                   PLAINTEXT_BODY.as_ptr(),
                   response_buffer.add(PLAINTEXT_BASE_LEN + DATE_LEN + CRLFCRLF_LEN),
                   PLAINTEXT_BODY_LEN,
@@ -71,19 +61,11 @@ fn cb(
 
                PLAINTEXT_BASE_LEN + DATE_LEN + CRLFCRLF_LEN + PLAINTEXT_BODY_LEN
             } else {
-               std::ptr::copy_nonoverlapping(
-                  HTTP_404_NOTFOUND.as_ptr(),
-                  response_buffer,
-                  HTTP_404_NOTFOUND_LEN,
-               );
+               core::ptr::copy_nonoverlapping(HTTP_404_NOTFOUND.as_ptr(), response_buffer, HTTP_404_NOTFOUND_LEN);
                HTTP_404_NOTFOUND_LEN
             }
          } else {
-            std::ptr::copy_nonoverlapping(
-               HTTP_405_NOTALLOWED.as_ptr(),
-               response_buffer,
-               HTTP_405_NOTALLOWED_LEN,
-            );
+            core::ptr::copy_nonoverlapping(HTTP_405_NOTALLOWED.as_ptr(), response_buffer, HTTP_405_NOTALLOWED_LEN);
             HTTP_405_NOTALLOWED_LEN
          }
       } else {
@@ -92,7 +74,6 @@ fn cb(
    }
 }
 
-#[inline]
-fn main() {
+pub fn main() {
    faf::epoll::go(8089, cb);
 }