bevy_debugger_mcp 0.1.8

AI-assisted debugging for Bevy games through Claude Code using Model Context Protocol
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
409
410
411
412
413
414
415
416
417
418
/// Compile-time optimization hints and configuration
/// 
/// This module provides compile-time optimizations through feature flags,
/// inline hints, and conditional compilation to minimize overhead when
/// debugging features are disabled.

use std::hint;

/// Force inline critical performance functions
#[inline(always)]
pub fn inline_hot_path<F, R>(f: F) -> R
where
    F: FnOnce() -> R,
{
    f()
}

/// Cold path optimization - hint that this code is rarely executed
#[cold]
#[inline(never)]
pub fn cold_path<F, R>(f: F) -> R
where
    F: FnOnce() -> R,
{
    f()
}

/// Likely branch prediction hint
#[inline(always)]
pub fn likely<T>(b: T) -> T
where
    T: Copy,
{
    // Note: Actual branch prediction hints require compiler-specific intrinsics
    // This is a placeholder that doesn't affect correctness
    b
}

/// Unlikely branch prediction hint
#[inline(always)]
pub fn unlikely<T>(b: T) -> T
where
    T: Copy,
{
    b
}

/// Zero-overhead feature flag checks
#[macro_export]
macro_rules! feature_enabled {
    ($feature:literal) => {
        cfg!(feature = $feature)
    };
}

/// Conditional compilation based on optimization level
#[macro_export]
macro_rules! if_optimized {
    ($optimized:block, $debug:block) => {
        #[cfg(not(debug_assertions))]
        $optimized
        
        #[cfg(debug_assertions)]
        $debug
    };
}

/// Zero-cost abstraction for debug-only operations
#[macro_export]
macro_rules! debug_only {
    ($expr:expr) => {
        #[cfg(debug_assertions)]
        $expr
    };
}

/// Zero-cost abstraction for release-only operations
#[macro_export]
macro_rules! release_only {
    ($expr:expr) => {
        #[cfg(not(debug_assertions))]
        $expr
    };
}

/// Prefetch data for better cache performance
#[inline(always)]
pub fn prefetch_read<T>(data: *const T) {
    #[cfg(target_arch = "x86_64")]
    {
        unsafe {
            std::arch::x86_64::_mm_prefetch(data as *const i8, std::arch::x86_64::_MM_HINT_T0);
        }
    }
}

/// Memory fence for ordering guarantees
#[inline(always)]
pub fn memory_fence() {
    std::sync::atomic::fence(std::sync::atomic::Ordering::SeqCst);
}

/// Fast path for common cases with minimal overhead
#[macro_export]
macro_rules! fast_path {
    ($condition:expr, $fast:block, $slow:block) => {
        if likely($condition) {
            inline_hot_path(|| $fast)
        } else {
            cold_path(|| $slow)
        }
    };
}

/// Compile-time configuration for different build profiles
pub struct CompileConfig;

impl CompileConfig {
    /// Check if we're in a debug build
    pub const fn is_debug() -> bool {
        cfg!(debug_assertions)
    }
    
    /// Check if we're in a release build
    pub const fn is_release() -> bool {
        !cfg!(debug_assertions)
    }
    
    /// Check if profiling is enabled
    pub const fn profiling_enabled() -> bool {
        cfg!(feature = "profiling") || cfg!(debug_assertions)
    }
    
    /// Check if caching is enabled
    pub const fn caching_enabled() -> bool {
        cfg!(feature = "caching") || cfg!(feature = "optimizations")
    }
    
    /// Check if pooling is enabled
    pub const fn pooling_enabled() -> bool {
        cfg!(feature = "pooling") || cfg!(feature = "optimizations")
    }
    
    /// Check if lazy initialization is enabled
    pub const fn lazy_init_enabled() -> bool {
        cfg!(feature = "lazy-init") || cfg!(feature = "optimizations")
    }
    
    /// Get optimization level hint
    pub const fn optimization_level() -> u8 {
        if cfg!(feature = "optimizations") {
            3 // Maximum optimization
        } else if cfg!(not(debug_assertions)) {
            2 // Release optimization
        } else {
            0 // Debug build
        }
    }
}

/// Optimized hash map types based on build configuration
pub mod collections {
    #[cfg(feature = "ahash")]
    pub type FastHashMap<K, V> = std::collections::HashMap<K, V, ahash::RandomState>;
    
    #[cfg(not(feature = "ahash"))]
    pub type FastHashMap<K, V> = std::collections::HashMap<K, V>;
    
    #[cfg(feature = "ahash")]
    pub type FastHashSet<T> = std::collections::HashSet<T, ahash::RandomState>;
    
    #[cfg(not(feature = "ahash"))]
    pub type FastHashSet<T> = std::collections::HashSet<T>;
}

/// Branch prediction hints for hot paths
#[macro_export]
macro_rules! likely_if {
    ($condition:expr, $then:block) => {
        if $crate::compile_opts::likely($condition) {
            $then
        }
    };
    ($condition:expr, $then:block, $else:block) => {
        if $crate::compile_opts::likely($condition) {
            $then
        } else {
            $else
        }
    };
}

#[macro_export]
macro_rules! unlikely_if {
    ($condition:expr, $then:block) => {
        if $crate::compile_opts::unlikely($condition) {
            $then
        }
    };
    ($condition:expr, $then:block, $else:block) => {
        if $crate::compile_opts::unlikely($condition) {
            $then
        } else {
            $else
        }
    };
}

/// Conditional compilation for different feature sets
#[macro_export]
macro_rules! with_feature {
    ($feature:literal, $code:block) => {
        #[cfg(feature = $feature)]
        $code
    };
}

#[macro_export]
macro_rules! without_feature {
    ($feature:literal, $code:block) => {
        #[cfg(not(feature = $feature))]
        $code
    };
}

/// No-op implementations for disabled features
#[macro_export]
macro_rules! feature_stub {
    ($feature:literal, $fn_name:ident, $return_type:ty, $default_value:expr) => {
        #[cfg(feature = $feature)]
        pub fn $fn_name() -> $return_type {
            // Real implementation would go here
            $default_value
        }
        
        #[cfg(not(feature = $feature))]
        #[inline(always)]
        pub fn $fn_name() -> $return_type {
            $default_value
        }
    };
}

/// Compile-time string concatenation for efficiency
#[macro_export]
macro_rules! concat_strs {
    ($($str:expr),*) => {
        concat!($($str),*)
    };
}

/// Alignment hints for better memory layout
#[repr(align(64))] // Cache line alignment
pub struct CacheLineAligned<T>(pub T);

#[repr(align(16))] // SIMD alignment
pub struct SimdAligned<T>(pub T);

/// CPU feature detection for runtime optimization
pub mod cpu_features {
    #[cfg(target_arch = "x86_64")]
    pub fn has_sse4_2() -> bool {
        is_x86_feature_detected!("sse4.2")
    }
    
    #[cfg(target_arch = "x86_64")]
    pub fn has_avx2() -> bool {
        is_x86_feature_detected!("avx2")
    }
    
    #[cfg(not(target_arch = "x86_64"))]
    pub fn has_sse4_2() -> bool { false }
    
    #[cfg(not(target_arch = "x86_64"))]
    pub fn has_avx2() -> bool { false }
}

/// Link-time optimization hints (Linux/Unix)
#[cfg(not(target_os = "macos"))]
#[link_section = ".text.hot"]
pub fn hot_function() {
    // Functions marked as hot will be placed in a special section
    // for better cache locality
}

/// Link-time optimization hints (macOS - no special section)
#[cfg(target_os = "macos")]
pub fn hot_function() {
    // Functions marked as hot (no special section on macOS due to mach-o limitations)
}

#[cfg(not(target_os = "macos"))]
#[link_section = ".text.cold"]
pub fn cold_function() {
    // Functions marked as cold will be placed away from hot code
}

#[cfg(target_os = "macos")]
pub fn cold_function() {
    // Functions marked as cold (no special section on macOS)
}

/// Compile-time assertions for configuration validation
#[macro_export]
macro_rules! static_assert_config {
    ($condition:expr, $message:expr) => {
        const _: () = assert!($condition, $message);
    };
}

// Compile-time configuration validation
static_assert_config!(
    CompileConfig::optimization_level() <= 3,
    "Invalid optimization level"
);

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_compile_config() {
        // Test that configuration methods work
        assert!(CompileConfig::optimization_level() <= 3);
        
        #[cfg(debug_assertions)]
        assert!(CompileConfig::is_debug());
        
        #[cfg(not(debug_assertions))]
        assert!(CompileConfig::is_release());
    }
    
    #[test]
    fn test_feature_macros() {
        // Test that feature macros compile
        debug_only!({
            // Test debug macro compilation
        });
        
        release_only!({
            // This will only run in release builds
        });
        
        if_optimized!({
            // Optimized version
        }, {
            // Debug version
        });
    }
    
    #[test]
    fn test_branch_hints() {
        let condition = true;
        
        likely_if!(condition, {
            // This branch is likely
        });
        
        unlikely_if!(!condition, {
            // This branch is unlikely
        });
    }
    
    #[test]
    fn test_collections() {
        let mut map = collections::FastHashMap::default();
        map.insert("key", "value");
        assert_eq!(map.get("key"), Some(&"value"));
        
        let mut set = collections::FastHashSet::default();
        set.insert(42);
        assert!(set.contains(&42));
    }
}

// Documentation for optimization techniques used:
// 
// 1. **Inline Hints**: Functions marked with #[inline(always)] or #[inline(never)]
//    to control inlining decisions.
// 
// 2. **Branch Prediction**: likely() and unlikely() hints to help the processor
//    predict branches more accurately.
// 
// 3. **Memory Layout**: Alignment hints for better cache performance and SIMD operations.
// 
// 4. **Feature Flags**: Conditional compilation to exclude code when features are disabled.
// 
// 5. **Zero-Cost Abstractions**: Macros that compile to nothing when features are disabled.
// 
// 6. **CPU Feature Detection**: Runtime detection of CPU capabilities for optimal code paths.
// 
// 7. **Link-Time Optimization**: Section hints for better code locality.
// 
// 8. **Collections**: Using faster hash algorithms when optimization features are enabled.
// 
// 9. **Prefetching**: Manual cache prefetching for performance-critical data access.
// 
// 10. **Memory Fences**: Explicit memory ordering when needed.
//
// Usage in performance-critical code:
// ```rust
// // Hot path optimization
// fast_path!(common_condition, {
//     // Fast implementation
// }, {
//     // Slow fallback
// });
// 
// // Feature-gated functionality
// with_feature!("caching", {
//     // Only compiled when caching feature is enabled
// });
// 
// // Branch prediction
// likely_if!(cache_hit, {
//     return cached_value;
// });
// ```