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
/*
* Copyright (c) 2023-2025 R3BL LLC
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use ;
/// Buffer for building text efficiently.
///
/// We use `String` as the backing storage after performance testing showed:
/// - `SmallString<[u8; 64]>` had slightly worse performance due to stack allocation
/// overhead.
/// - `SmallString<[u8; 256]>` had even worse performance for small strings.
/// - Plain `String` provides the best balance of performance across all test cases.
///
/// This type alias allows us to easily experiment with different string-like data
/// structures in the future (e.g., `SmallString`, `String`, custom implementations)
/// without impacting the rest of the codebase.
pub type BufTextStorage = String;
/// Trait for efficiently writing text to a buffer.
///
/// ## Why `WriteToBuf` instead of Display/Formatter?
///
/// The standard [`std::fmt::Display`] trait uses [`std::fmt::Formatter`] which has
/// significant overhead:
/// 1. **Formatter State Machine**: Each [`write!`] call goes through the formatter's
/// internal state machine, checking formatting flags (alignment, padding, precision,
/// etc).
/// 2. **Multiple Function Calls**: Each [`write!`] has method call overhead, vtable
/// lookups for trait objects, and repeated bounds checking.
/// 3. **Buffer Management**: The formatter may need to reallocate its internal buffer
/// multiple times for many small writes.
///
/// By using `WriteToBuf` with a [`BufTextStorage`] buffer, we:
/// - Make direct string concatenations without formatter overhead.
/// - Batch all content into a single buffer.
/// - Make only ONE write to the formatter in the Display implementation using
/// [`core::fmt::Formatter::write_str`].
/// - Reduce the overhead from ~16% to ~5-8% in performance profiles.
///
/// The [`std::fmt::Display`] trait implementations still exist for API compatibility but
/// delegate to `WriteToBuf`. The [`std::fmt::Display`] trait will have to use use
/// [`core::fmt::Formatter::write_str`] to actually write the `acc` buffer.