structify 0.0.1
structify: ^0.0.1 copied to clipboard
A powerful Dart library for working with C-style structs, providing memory alignment, efficient serialization, and safety features.
📦 Structify #
A powerful Dart library for working with C-style structs, providing memory-aligned data structures with efficient layout and access.
🌟 Features #
Core Features (Implemented) ✅ #
🔧 Memory Management
- Custom memory layout control
- Fixed-size array support
- Memory pooling
- Scope-based memory management
- Safe pointer handling
// Example: Using memory pool
final pool = PointPool(capacity: 1000);
final points = pool.allocateMany(5);
try {
for (final ptr in points) {
ptr.ref.x = 42;
ptr.ref.y = 42;
}
} finally {
pool.freeMany(points);
}
🏗️ Custom Alignment
- Explicit alignment control
- SIMD-optimized structures
- Packed structs support
- Alignment preservation
@Aligned(StructAlignment.align16)
final class SimdVector extends Struct {
@Array(4)
external Array<Float32> values;
static Pointer<SimdVector> alloc() {
return calloc.align16<SimdVector>();
}
}
🔄 Endianness Control
- Mixed endianness support
- Network byte order handling
- Platform-specific optimizations
final class NetworkPacket extends Struct {
@Int32()
@ByteOrder(StructEndian.network)
external int messageId;
@Int32()
@ByteOrder(StructEndian.host)
external int flags;
}
🔒 Memory Safety
- Bounds checking
- Memory corruption detection
- Reference counting
- Automatic cleanup
void example() {
final scope = StructMemory.createScope();
try {
final point = scope.allocPoint();
// Work with point...
} finally {
scope.dispose(); // Automatic cleanup
}
}
Features Under Development 🚧 #
📊 SIMD Operations
- Vector operations
- Parallel processing
- Performance optimizations
// Coming soon:
class SimdMath {
static void vectorAdd(SimdVector a, SimdVector b, SimdVector result) {
// SIMD-optimized addition
}
}
🌐 Protocol Buffer Integration
- Automatic serialization
- Schema definition
- Cross-platform compatibility
// Under development:
@proto
final class UserMessage extends Struct {
@ProtoField(1)
external String name;
@ProtoField(2)
external int id;
}
🎮 Game Development Tools
- Fast physics structs
- Collision detection
- Transform hierarchies
// Planned feature:
final class Transform3D extends Struct {
external Vector3 position;
external Quaternion rotation;
external Vector3 scale;
}
📚 Usage Guide #
🏃♂️ Quick Start #
- Add dependency to your pubspec.yaml:
dependencies:
structify: ^0.1.0
- Import the package:
import 'package:structify/structify.dart';
- Create your first struct:
final class Point extends Struct {
@Int32()
external int x;
@Int32()
external int y;
static Pointer<Point> alloc() => calloc<Point>();
}
🎯 Advanced Usage Examples #
🏊♂️ Memory Pool
final pool = PointPool(capacity: 1000);
// Allocate points
final points = pool.allocateMany(5);
// Use points
for (final ptr in points) {
ptr.ref.x = 42;
ptr.ref.y = 42;
}
// Free points
pool.freeMany(points);
🔀 Custom Alignment
@Aligned(StructAlignment.align8)
final class AlignedStruct extends Struct {
@Int32()
@Aligned(StructAlignment.align4)
external int intField;
@Float64()
@Aligned(StructAlignment.align8)
external double doubleField;
}
🌐 Network Protocol
final message = NetworkMessage.alloc();
message.ref
..messageId = 0x12345678 // Network byte order
..flags = 0xAABBCCDD // Host byte order
..setData([0xDE, 0xAD, 0xBE, 0xEF]);
📘 Technical Documentation #
🏗️ Architecture #
Core Components #
1. Memory Management System 🧠
class StructMemory {
// Scope-based memory management
static StructScope createScope();
static void disposeScope(String name);
}
Key features:
- Automatic resource tracking
- Hierarchical memory management
- Leak prevention
- Safe cleanup
2. Alignment System ⚖️
enum StructAlignment {
packed(1),
align2(2),
align4(4),
align8(8),
align16(16)
}
Capabilities:
- Custom alignment specifications
- Padding optimization
- SIMD support
- Cross-platform consistency
3. Type System 📊
Supported types:
- Numeric types (Int8 to Int64)
- Floating point (Float32, Float64)
- Arrays (fixed and dynamic)
- Nested structs
- Unions
🔍 Implementation Details #
Memory Pool Implementation #
class PointPool extends BaseStructPool {
// Allocation tracking
final List<Pointer<Point>> _pointers;
final Map<Pointer<Point>, int> _pointerToIndex;
// Pool operations
Pointer<Point>? allocate();
void free(Pointer<Point> pointer);
List<Pointer<Point>> allocateMany(int count);
}
Features:
- O(1) allocation/deallocation
- Memory reuse
- Fragmentation prevention
- Thread safety (planned)
SIMD Operations #
Current implementation:
final class SimdVector extends Struct {
@Array(4)
external Array<Float32> values;
}
Planned features:
- Vector operations
- Matrix multiplication
- Quaternion math
- Physics calculations
Network Protocol Support #
final class NetworkMessage extends Struct {
@Int32()
@ByteOrder(StructEndian.network)
external int messageId;
}
Features:
- Automatic endianness conversion
- Protocol buffer compatibility
- Stream support
- Checksumming
🔬 Performance Considerations #
Memory Layout Optimization #
┌────────────────────┐
│ Aligned Struct │
├────────┬───────────┤
│ Field │ Alignment │
├────────┼───────────┤
│ int32 │ 4 bytes │
│ double │ 8 bytes │
└────────┴───────────┘
Optimization techniques:
- Field reordering
- Padding minimization
- Cache line alignment
- SIMD optimization
Benchmarks #
Current performance metrics:
- Allocation: ~100ns
- Pool allocation: ~50ns
- SIMD operations: ~10ns per vector
- Serialization: ~500ns per struct
🛠️ Best Practices #
Memory Management #
DO:
final scope = StructMemory.createScope();
try {
// Work with memory
} finally {
scope.dispose();
}
DON'T:
final pointer = calloc<Point>(); // Raw allocation without tracking
Alignment #
DO:
@Aligned(StructAlignment.align8)
final class OptimizedStruct extends Struct {
// Fields...
}
DON'T:
@packed // Don't use packed unless necessary
final class UnalignedStruct extends Struct {
// Fields...
}
Error Handling #
DO:
try {
pool.allocateMany(count);
} on OutOfMemoryError {
// Handle allocation failure
}
DON'T:
final ptr = pool.allocate(); // Missing error handling
🔮 Future Plans #
Upcoming Features #
- Advanced SIMD Operations
class SimdMath {
static void vectorMultiply(SimdVector a, SimdVector b);
static void matrixMultiply(SimdMatrix a, SimdMatrix b);
}
- Memory Mapping
class MemoryMappedStruct extends Struct {
static Pointer<T> mapFile<T extends Struct>(String path);
}
- Zero-Copy Operations
class ZeroCopyBuffer {
void transferTo(NetworkMessage message);
void receiveFrom(NetworkMessage message);
}
Planned Optimizations #
- Cache-conscious layouts
- NUMA awareness
- Thread-local storage
- Lock-free algorithms
📊 Benchmarking Tools #
class StructifyBenchmark {
static void measureAllocation();
static void measurePoolPerformance();
static void measureSIMDOperations();
}
📚 Structify Examples and Tutorials #
🚀 Basic Examples #
1. Working with Points and Vectors #
import 'package:structify/structify.dart';
void main() {
// Create a memory scope
final scope = StructMemory.createScope();
try {
// Create a point
final point = scope.allocPoint().ref
..x = 10
..y = 20;
// Create a vector
final vector = SimdVector.alloc();
vector.ref.setValues([1.0, 2.0, 3.0, 4.0]);
print('Point: $point');
print('Vector: ${vector.ref.values[0]}, ${vector.ref.values[1]}, ...');
} finally {
scope.dispose();
}
}
2. Memory Pool Usage #
void poolExample() {
final pool = PointPool(capacity: 1000);
// Batch allocation
final points = pool.allocateMany(5);
// Initialize points
for (final ptr in points) {
ptr.ref
..x = 42
..y = 42;
}
// Process points in batch
for (final ptr in points) {
ptr.ref.x *= 2;
ptr.ref.y *= 2;
}
// Free memory
pool.freeMany(points);
}
3. Networking with Endianness Control #
void networkExample() {
final packet = NetworkPacket.alloc();
// Set message fields
packet.ref
..messageId = 0x12345678 // Network byte order
..flags = 0xAABBCCDD // Host byte order
..timestamp = DateTime.now().millisecondsSinceEpoch;
// Add payload
final payload = [0xDE, 0xAD, 0xBE, 0xEF];
packet.ref.setData(payload);
// Cleanup
NetworkPacket.free(packet);
}
🎓 Advanced Examples #
1. Custom SIMD Operations #
void simdExample() {
final vectorA = SimdVector.alloc();
final vectorB = SimdVector.alloc();
final result = SimdVector.alloc();
try {
// Initialize vectors
vectorA.ref.setValues([1.0, 2.0, 3.0, 4.0]);
vectorB.ref.setValues([5.0, 6.0, 7.0, 8.0]);
// Perform SIMD operations
for (var i = 0; i < 4; i++) {
result.ref.values[i] =
vectorA.ref.values[i] * vectorB.ref.values[i];
}
} finally {
// Cleanup
SimdVector.free(vectorA);
SimdVector.free(vectorB);
SimdVector.free(result);
}
}
2. Custom Alignment with Memory Analysis #
@Aligned(StructAlignment.align16)
final class CustomStruct extends Struct {
@Int32()
@Aligned(StructAlignment.align4)
external int field1;
@Float64()
@Aligned(StructAlignment.align8)
external double field2;
@Array(4)
external Array<Float32> field3;
static Pointer<CustomStruct> alloc() {
return calloc.align16<CustomStruct>();
}
}
void alignmentExample() {
final struct = CustomStruct.alloc();
try {
// Analyze memory layout
print('Struct size: ${sizeOf<CustomStruct>()} bytes');
analyzeStructLayout(struct.ref);
} finally {
calloc.free(struct);
}
}
3. Dynamic Memory Management #
void dynamicMemoryExample() {
final array = DynamicAlignedArray.create(
initialCapacity: 16,
elementSize: sizeOf<Int32>(),
alignment: 8,
);
try {
// Add elements
for (var i = 0; i < 20; i++) {
final value = calloc<Int32>()..value = i;
array.ref.add(value);
calloc.free(value);
}
// Array will automatically resize
print('Capacity: ${array.ref.capacity}');
print('Length: ${array.ref.length}');
} finally {
DynamicAlignedArray.free(array);
}
}
🔧 Real-World Examples #
1. Game Physics Engine #
final class PhysicsBody extends Struct {
external Vector3 position;
external Vector3 velocity;
external Quaternion rotation;
external Float32 mass;
void updatePosition(double deltaTime) {
position.x += velocity.x * deltaTime;
position.y += velocity.y * deltaTime;
position.z += velocity.z * deltaTime;
}
}
2. Network Protocol Handler #
final class ProtocolHandler {
final StructScope _scope;
final List<NetworkPacket> _packetPool;
void handleMessage(List<int> rawData) {
final packet = NetworkPacket.alloc();
try {
packet.ref.deserializeFromBytes(rawData);
processMessage(packet.ref);
} finally {
NetworkPacket.free(packet);
}
}
}
3. Image Processing #
final class ImageProcessor {
final SimdVector _colorVector;
final StructScope _scope;
void applyFilter(List<int> pixels) {
final vectorized = SimdVector.alloc();
try {
// Process 4 pixels at a time using SIMD
for (var i = 0; i < pixels.length; i += 4) {
vectorized.ref.setValues(pixels.sublist(i, i + 4)
.map((p) => p.toDouble()).toList());
// Apply filter...
}
} finally {
SimdVector.free(vectorized);
}
}
}
🎮 Interactive Examples #
1. Memory Pool Visualization #
void visualizePool() {
final pool = PointPool(capacity: 10);
print('Pool visualization:');
print('┌─────────────────────┐');
print('│ Memory Pool Status │');
print('├─────┬───────────────┤');
// Allocate and visualize
final points = pool.allocateMany(5);
for (var i = 0; i < 10; i++) {
final status = i < 5 ? '█' : '░';
print('│ $i │ $status │');
}
print('└─────┴───────────────┘');
pool.freeMany(points);
}
🤝 Contributing #
Contributions are welcome! Here are some ways you can contribute:
- 🐛 Report bugs
- 💡 Suggest features
- 📝 Improve documentation
- 🔧 Submit pull requests
📝 License #
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments #
Special thanks to:
- FFI team for the foundation
- Dart team for the amazing language
- All contributors
📞 Contact #
- 📧 Email: samuelkchris@gmail.com
- 🐦 Twitter: @samuelkchris