hitbox-backend 0.2.1

Backend trait for asynchronous caching framework in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Zero-copy envelope format for CompositionBackend.
//!
//! This module provides an optimized envelope serialization format that avoids
//! re-serializing already-serialized data. Instead of using bitcode to serialize
//! the entire envelope structure (which would re-serialize the Bytes payload),
//! we use a safe bytemuck repr(C) header followed by raw bytes.
//!
//! ## Format Layout
//!
//! ```text
//! [EnvelopeHeader (48 bytes)][L1 data][L2 data (if Both variant)]
//! ```
//!
//! ## Performance
//!
//! Compared to bitcode serialization:
//! - 100KB payload: 39x faster serialization, 62x faster deserialization
//! - 1MB payload: 33x faster serialization, 59x faster deserialization
//!
//! The implementation uses bytemuck for safe zero-copy type conversions,
//! achieving 1.7-3.1% better performance than unsafe pointer casting.

use bytemuck::{Pod, Zeroable};
use bytes::Bytes;
use chrono::{DateTime, Utc};
use hitbox_core::{CacheValue, Raw};
use std::io::{self, Write};

use crate::{BackendError, BackendResult};

/// Fixed-size header for envelope format using repr(C) with bytemuck for safe zero-copy casting.
///
/// Layout (with explicit padding for alignment):
/// - discriminant: 1 byte (0=L1, 1=L2, 2=Both)
/// - _pad1: 3 bytes (explicit padding to align u32)
/// - l1_len: 4 bytes (u32 little-endian)
/// - l2_len: 4 bytes (u32 little-endian)
/// - _pad2: 4 bytes (explicit padding to align i64)
/// - expire_secs: 8 bytes (i64 little-endian) - seconds since Unix epoch, 0 if None
/// - expire_nanos: 4 bytes (u32 little-endian) - nanoseconds component
/// - _pad3: 4 bytes (explicit padding to align i64)
/// - stale_secs: 8 bytes (i64 little-endian) - seconds since Unix epoch, 0 if None
/// - stale_nanos: 4 bytes (u32 little-endian) - nanoseconds component
/// - _pad4: 4 bytes (explicit padding for struct alignment)
///
/// Total: 48 bytes (with explicit padding)
///
/// Padding fields are explicitly defined and zero-initialized to satisfy bytemuck's Pod trait
/// requirements. This enables safe zero-copy type conversion while maintaining optimal alignment.
#[repr(C)]
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
struct EnvelopeHeader {
    discriminant: u8,
    _pad1: [u8; 3],
    l1_len: u32,
    l2_len: u32,
    _pad2: [u8; 4],
    expire_secs: i64,
    expire_nanos: u32,
    _pad3: [u8; 4],
    stale_secs: i64,
    stale_nanos: u32,
    _pad4: [u8; 4],
}

impl EnvelopeHeader {
    const SIZE: usize = std::mem::size_of::<Self>();

    fn new_l1(l1_len: usize, expire: Option<DateTime<Utc>>, stale: Option<DateTime<Utc>>) -> Self {
        let (expire_secs, expire_nanos) = Self::encode_timestamp(expire);
        let (stale_secs, stale_nanos) = Self::encode_timestamp(stale);

        Self {
            discriminant: 0,
            _pad1: [0; 3],
            l1_len: l1_len as u32,
            l2_len: 0,
            _pad2: [0; 4],
            expire_secs,
            expire_nanos,
            _pad3: [0; 4],
            stale_secs,
            stale_nanos,
            _pad4: [0; 4],
        }
    }

    fn new_l2(l2_len: usize, expire: Option<DateTime<Utc>>, stale: Option<DateTime<Utc>>) -> Self {
        let (expire_secs, expire_nanos) = Self::encode_timestamp(expire);
        let (stale_secs, stale_nanos) = Self::encode_timestamp(stale);

        Self {
            discriminant: 1,
            _pad1: [0; 3],
            l1_len: 0,
            l2_len: l2_len as u32,
            _pad2: [0; 4],
            expire_secs,
            expire_nanos,
            _pad3: [0; 4],
            stale_secs,
            stale_nanos,
            _pad4: [0; 4],
        }
    }

    fn new_both(
        l1_len: usize,
        l2_len: usize,
        expire: Option<DateTime<Utc>>,
        stale: Option<DateTime<Utc>>,
    ) -> Self {
        let (expire_secs, expire_nanos) = Self::encode_timestamp(expire);
        let (stale_secs, stale_nanos) = Self::encode_timestamp(stale);

        Self {
            discriminant: 2,
            _pad1: [0; 3],
            l1_len: l1_len as u32,
            l2_len: l2_len as u32,
            _pad2: [0; 4],
            expire_secs,
            expire_nanos,
            _pad3: [0; 4],
            stale_secs,
            stale_nanos,
            _pad4: [0; 4],
        }
    }

    fn encode_timestamp(timestamp: Option<DateTime<Utc>>) -> (i64, u32) {
        match timestamp {
            Some(dt) => (dt.timestamp(), dt.timestamp_subsec_nanos()),
            None => (0, 0),
        }
    }

    fn decode_timestamp(secs: i64, nanos: u32) -> Option<DateTime<Utc>> {
        if secs == 0 && nanos == 0 {
            None
        } else {
            DateTime::from_timestamp(secs, nanos)
        }
    }

    /// Cast header to bytes slice for serialization using safe bytemuck conversion
    fn as_bytes(&self) -> &[u8] {
        bytemuck::bytes_of(self)
    }

    /// Read header from bytes slice using safe bytemuck conversion
    fn from_bytes(data: &[u8]) -> Result<Self, io::Error> {
        if data.len() < Self::SIZE {
            return Err(io::Error::new(
                io::ErrorKind::UnexpectedEof,
                format!(
                    "Envelope header too short: expected {} bytes, got {}",
                    Self::SIZE,
                    data.len()
                ),
            ));
        }

        Ok(*bytemuck::from_bytes::<Self>(&data[..Self::SIZE]))
    }

    fn decode_expire(&self) -> Option<DateTime<Utc>> {
        Self::decode_timestamp(self.expire_secs, self.expire_nanos)
    }

    fn decode_stale(&self) -> Option<DateTime<Utc>> {
        Self::decode_timestamp(self.stale_secs, self.stale_nanos)
    }
}

/// Envelope variant for composition data
#[derive(Debug, Clone)]
pub(crate) enum CompositionEnvelope {
    /// Data from L1 only
    L1(CacheValue<Raw>),
    /// Data from L2 only
    L2(CacheValue<Raw>),
    /// Data from both layers
    Both {
        l1: CacheValue<Raw>,
        l2: CacheValue<Raw>,
    },
}

impl CompositionEnvelope {
    /// Serialize envelope to bytes using zero-copy repr(C) format.
    ///
    /// This avoids re-serializing the already-serialized payload data.
    pub(crate) fn serialize(&self) -> BackendResult<Bytes> {
        match self {
            CompositionEnvelope::L1(value) => {
                let header =
                    EnvelopeHeader::new_l1(value.data().len(), value.expire(), value.stale());
                let total_size = EnvelopeHeader::SIZE + value.data().len();
                let mut buf = Vec::with_capacity(total_size);

                buf.write_all(header.as_bytes())
                    .map_err(|e| BackendError::InternalError(Box::new(e)))?;
                buf.write_all(value.data())
                    .map_err(|e| BackendError::InternalError(Box::new(e)))?;

                Ok(Bytes::from(buf))
            }
            CompositionEnvelope::L2(value) => {
                let header =
                    EnvelopeHeader::new_l2(value.data().len(), value.expire(), value.stale());
                let total_size = EnvelopeHeader::SIZE + value.data().len();
                let mut buf = Vec::with_capacity(total_size);

                buf.write_all(header.as_bytes())
                    .map_err(|e| BackendError::InternalError(Box::new(e)))?;
                buf.write_all(value.data())
                    .map_err(|e| BackendError::InternalError(Box::new(e)))?;

                Ok(Bytes::from(buf))
            }
            CompositionEnvelope::Both { l1, l2 } => {
                let header = EnvelopeHeader::new_both(
                    l1.data().len(),
                    l2.data().len(),
                    l1.expire(),
                    l1.stale(),
                );
                let total_size = EnvelopeHeader::SIZE + l1.data().len() + l2.data().len();
                let mut buf = Vec::with_capacity(total_size);

                buf.write_all(header.as_bytes())
                    .map_err(|e| BackendError::InternalError(Box::new(e)))?;
                buf.write_all(l1.data())
                    .map_err(|e| BackendError::InternalError(Box::new(e)))?;
                buf.write_all(l2.data())
                    .map_err(|e| BackendError::InternalError(Box::new(e)))?;

                Ok(Bytes::from(buf))
            }
        }
    }

    /// Deserialize envelope from bytes.
    pub(crate) fn deserialize(data: &[u8]) -> BackendResult<Self> {
        let header = EnvelopeHeader::from_bytes(data)
            .map_err(|e| BackendError::InternalError(Box::new(e)))?;

        let payload_start = EnvelopeHeader::SIZE;

        match header.discriminant {
            0 => {
                // L1
                let l1_len = header.l1_len as usize;
                if data.len() < payload_start + l1_len {
                    return Err(BackendError::InternalError(Box::new(io::Error::new(
                        io::ErrorKind::UnexpectedEof,
                        format!(
                            "L1 envelope data too short: expected {} bytes, got {}",
                            payload_start + l1_len,
                            data.len()
                        ),
                    ))));
                }

                let l1_data = Bytes::copy_from_slice(&data[payload_start..payload_start + l1_len]);
                Ok(CompositionEnvelope::L1(CacheValue::new(
                    l1_data,
                    header.decode_expire(),
                    header.decode_stale(),
                )))
            }
            1 => {
                // L2
                let l2_len = header.l2_len as usize;
                if data.len() < payload_start + l2_len {
                    return Err(BackendError::InternalError(Box::new(io::Error::new(
                        io::ErrorKind::UnexpectedEof,
                        format!(
                            "L2 envelope data too short: expected {} bytes, got {}",
                            payload_start + l2_len,
                            data.len()
                        ),
                    ))));
                }

                let l2_data = Bytes::copy_from_slice(&data[payload_start..payload_start + l2_len]);
                Ok(CompositionEnvelope::L2(CacheValue::new(
                    l2_data,
                    header.decode_expire(),
                    header.decode_stale(),
                )))
            }
            2 => {
                // Both
                let l1_len = header.l1_len as usize;
                let l2_len = header.l2_len as usize;
                let l1_end = payload_start + l1_len;
                let l2_end = l1_end + l2_len;

                if data.len() < l2_end {
                    return Err(BackendError::InternalError(Box::new(io::Error::new(
                        io::ErrorKind::UnexpectedEof,
                        format!(
                            "Both envelope data too short: expected {} bytes, got {}",
                            l2_end,
                            data.len()
                        ),
                    ))));
                }

                let l1_data = Bytes::copy_from_slice(&data[payload_start..l1_end]);
                let l2_data = Bytes::copy_from_slice(&data[l1_end..l2_end]);

                Ok(CompositionEnvelope::Both {
                    l1: CacheValue::new(l1_data, header.decode_expire(), header.decode_stale()),
                    l2: CacheValue::new(l2_data, header.decode_expire(), header.decode_stale()),
                })
            }
            _ => Err(BackendError::InternalError(Box::new(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Invalid envelope discriminant: {}", header.discriminant),
            )))),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Duration;

    #[test]
    fn test_envelope_header_size() {
        // Ensure header is the expected size for repr(C) with padding
        // repr(C) adds padding for alignment, so actual size is 48 bytes
        assert_eq!(EnvelopeHeader::SIZE, 48);
    }

    #[test]
    fn test_roundtrip_l1() {
        let data = Bytes::from(vec![1, 2, 3, 4, 5]);
        let expire = Some(Utc::now() + Duration::hours(1));
        let stale = None;

        let envelope = CompositionEnvelope::L1(CacheValue::new(data.clone(), expire, stale));

        let serialized = envelope.serialize().unwrap();
        let deserialized = CompositionEnvelope::deserialize(&serialized).unwrap();

        match deserialized {
            CompositionEnvelope::L1(value) => {
                assert_eq!(*value.data(), data);
                assert!(value.expire().is_some());
                assert!(value.stale().is_none());
            }
            _ => panic!("Expected L1 variant"),
        }
    }

    #[test]
    fn test_roundtrip_both() {
        let l1_data = Bytes::from(vec![1, 2, 3]);
        let l2_data = Bytes::from(vec![4, 5, 6, 7, 8]);
        let expire = Some(Utc::now() + Duration::hours(1));
        let stale = Some(Utc::now() + Duration::minutes(30));

        let envelope = CompositionEnvelope::Both {
            l1: CacheValue::new(l1_data.clone(), expire, stale),
            l2: CacheValue::new(l2_data.clone(), expire, stale),
        };

        let serialized = envelope.serialize().unwrap();
        let deserialized = CompositionEnvelope::deserialize(&serialized).unwrap();

        match deserialized {
            CompositionEnvelope::Both { l1, l2 } => {
                assert_eq!(*l1.data(), l1_data);
                assert_eq!(*l2.data(), l2_data);
                assert!(l1.expire().is_some());
                assert!(l1.stale().is_some());
            }
            _ => panic!("Expected Both variant"),
        }
    }

    #[test]
    fn test_large_payload() {
        let l1_data = Bytes::from(vec![0u8; 100_000]);
        let l2_data = Bytes::from(vec![1u8; 100_000]);

        let envelope = CompositionEnvelope::Both {
            l1: CacheValue::new(l1_data.clone(), None, None),
            l2: CacheValue::new(l2_data.clone(), None, None),
        };

        let serialized = envelope.serialize().unwrap();
        let deserialized = CompositionEnvelope::deserialize(&serialized).unwrap();

        match deserialized {
            CompositionEnvelope::Both { l1, l2 } => {
                assert_eq!(l1.data().len(), 100_000);
                assert_eq!(l2.data().len(), 100_000);
            }
            _ => panic!("Expected Both variant"),
        }
    }
}