generic.rs


Overview

The `generic.rs` file provides a highly optimized, SIMD-accelerated function for formatting and escaping string data in memory. Specifically, it implements a generic 128-bit SIMD-based routine to escape special characters (`\`, `"`, and control characters below ASCII 32) in a byte string, wrapping the result in double quotes (`"`). The function writes the escaped string directly to a provided output buffer pointer and returns the total number of bytes written.

This file focuses on performance by leveraging architecture-specific SIMD instructions (notably ARM NEON when available on AArch64 targets) to process 16-byte chunks in parallel. It falls back to scalar processing for small inputs or trailing bytes that don't fill a SIMD vector. The implementation is `unsafe` due to raw pointer manipulation and direct memory writes, requiring careful use to avoid undefined behavior.


Detailed API Documentation

Function: format_escaped_str_impl_generic_128

pub(crate) unsafe fn format_escaped_str_impl_generic_128(
    odst: *mut u8,
    value_ptr: *const u8,
    value_len: usize,
) -> usize

Description

Formats a raw byte slice as a double-quoted, escaped string and writes the result into a destination buffer. It escapes backslashes (`\`), double quotes (`"`), and all ASCII control characters (code points less than 32). The function uses 128-bit SIMD vectors (`u8x16`) to accelerate processing of large inputs.

Parameters

Returns

Usage Example

let input = b"Hello\nWorld\"Test\\String";
let mut output = [0u8; 64];
let written = unsafe {
    format_escaped_str_impl_generic_128(output.as_mut_ptr(), input.as_ptr(), input.len())
};
let escaped_str = std::str::from_utf8(&output[..written]).unwrap();
println!("{}", escaped_str);
// Output: "Hello\nWorld\"Test\\String"

*Note*: The above example assumes the existence of the scalar fallback macro `impl_format_scalar!` and the macro `write_escape!` which are part of the larger codebase for escaping characters.


Implementation Details and Algorithm

SIMD-based Escaping Strategy

Important Macros (from context)

Safety and Performance Considerations


Interaction with Other System Components


Visual Diagram: Function Workflow Flowchart

flowchart TD
    Start[Start: Write leading quote (")] --> CheckLen{Is input length < 16?}
    
    CheckLen -- Yes --> Scalar[Call scalar escaping implementation]
    Scalar --> WriteEnd[Write trailing quote (")]
    
    CheckLen -- No --> SIMDLoop[Process 16-byte chunks with SIMD]
    
    SIMDLoop -->|No escapes detected| CopyChunk[Copy chunk directly to output]
    SIMDLoop -->|Escapes detected| EscapeByte[Find first byte to escape]
    EscapeByte --> WriteEscape[Write escape sequence]
    WriteEscape --> SIMDLoop
    
    SIMDLoop --> LastStride[Process last partial stride with scratch buffer]
    LastStride --> WriteEnd
    
    WriteEnd --> End[Return total bytes written]

Summary

This file exemplifies a balance between performance (via SIMD) and correctness (correct escaping) for string formatting in a low-level systems or serialization context.