Skip to main content

StaticHead

Struct StaticHead 

Source
pub struct StaticHead { /* private fields */ }
Expand description

A response serialized once, with a Date slot patched at most once a second: the fast path for fixed replies. The body (possibly empty) follows the head in the same buffer, so the whole reply is one slice.

Implementations§

Source§

impl StaticHead

Source

pub fn new( status: u16, headers: &[(&[u8], &[u8])], body: &[u8], ) -> Result<Self, Error>

Serializes status, headers, Content-Length: body.len(), a Date slot and the body.

Examples found in repository?
examples/bench_parse.rs (line 96)
60fn main() {
61    let twenty = twenty_headers();
62    let cases: [(&str, &[u8]); 3] = [("curl (3 fields)", CURL), ("browser (15 fields)", BROWSER), ("20 fields", &twenty)];
63    let best = level();
64    println!("scan level detected: {best:?}");
65    println!("{:<22} {:>6} {:>12} {:>12} {:>14} {:>9}", "request", "bytes", "simd ns", "scalar ns", "cameleer ns", "GB/s simd");
66    for (name, req) in cases {
67        assert!(cameleer::parse(req).is_ok());
68        force(best);
69        let simd = measure(|| {
70            black_box(simoom(req));
71        });
72        force(Level::Scalar);
73        let scalar = measure(|| {
74            black_box(simoom(req));
75        });
76        force(best);
77        let cam = measure(|| {
78            black_box(cameleer::parse(black_box(req)).ok());
79        });
80        println!(
81            "{name:<22} {:>6} {simd:>12.1} {scalar:>12.1} {cam:>14.1} {:>9.2}",
82            req.len(),
83            req.len() as f64 / simd
84        );
85    }
86    // Serialization.
87    let date = DateCache::new(1_700_000_000);
88    let mut out = Vec::with_capacity(256);
89    let head = measure(|| {
90        out.clear();
91        let mut w = HeadWriter::response(&mut out, 200);
92        w.header(b"Content-Type", b"application/json").unwrap().date(&date).content_length(1234);
93        w.finish();
94        black_box(&out);
95    });
96    let mut fixed = StaticHead::new(200, &[(b"Content-Type", b"text/plain"), (b"Server", b"simoom")], b"Hello, World!").unwrap();
97    let mut secs = 1_700_000_000;
98    let mut dc = DateCache::new(secs);
99    let stat = measure(|| {
100        black_box(fixed.full(&dc).len());
101    });
102    let stat_tick = measure(|| {
103        secs += 1;
104        dc.update(secs);
105        black_box(fixed.full(&dc).len());
106    });
107    println!("HeadWriter (status, 1 field, Date, Content-Length): {head:.1} ns");
108    println!("StaticHead::full (fixed reply, same second): {stat:.1} ns; with a new second every call: {stat_tick:.1} ns");
109}
Source

pub fn full(&mut self, date: &DateCache) -> &[u8] ⓘ

The whole reply, head and body.

Examples found in repository?
examples/bench_parse.rs (line 100)
60fn main() {
61    let twenty = twenty_headers();
62    let cases: [(&str, &[u8]); 3] = [("curl (3 fields)", CURL), ("browser (15 fields)", BROWSER), ("20 fields", &twenty)];
63    let best = level();
64    println!("scan level detected: {best:?}");
65    println!("{:<22} {:>6} {:>12} {:>12} {:>14} {:>9}", "request", "bytes", "simd ns", "scalar ns", "cameleer ns", "GB/s simd");
66    for (name, req) in cases {
67        assert!(cameleer::parse(req).is_ok());
68        force(best);
69        let simd = measure(|| {
70            black_box(simoom(req));
71        });
72        force(Level::Scalar);
73        let scalar = measure(|| {
74            black_box(simoom(req));
75        });
76        force(best);
77        let cam = measure(|| {
78            black_box(cameleer::parse(black_box(req)).ok());
79        });
80        println!(
81            "{name:<22} {:>6} {simd:>12.1} {scalar:>12.1} {cam:>14.1} {:>9.2}",
82            req.len(),
83            req.len() as f64 / simd
84        );
85    }
86    // Serialization.
87    let date = DateCache::new(1_700_000_000);
88    let mut out = Vec::with_capacity(256);
89    let head = measure(|| {
90        out.clear();
91        let mut w = HeadWriter::response(&mut out, 200);
92        w.header(b"Content-Type", b"application/json").unwrap().date(&date).content_length(1234);
93        w.finish();
94        black_box(&out);
95    });
96    let mut fixed = StaticHead::new(200, &[(b"Content-Type", b"text/plain"), (b"Server", b"simoom")], b"Hello, World!").unwrap();
97    let mut secs = 1_700_000_000;
98    let mut dc = DateCache::new(secs);
99    let stat = measure(|| {
100        black_box(fixed.full(&dc).len());
101    });
102    let stat_tick = measure(|| {
103        secs += 1;
104        dc.update(secs);
105        black_box(fixed.full(&dc).len());
106    });
107    println!("HeadWriter (status, 1 field, Date, Content-Length): {head:.1} ns");
108    println!("StaticHead::full (fixed reply, same second): {stat:.1} ns; with a new second every call: {stat_tick:.1} ns");
109}
Source

pub fn head(&mut self, date: &DateCache) -> &[u8] ⓘ

The head alone (for HEAD requests).

Trait Implementations§

Source§

impl Clone for StaticHead

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for StaticHead

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.