High-Performance Zero-Copy Cognitive Graph for Advanced Code Analysis
High-Performance Zero-Copy Cognitive Graph for Advanced Code Analysis
A revolutionary approach to code analysis that unifies syntactic ambiguity handling with semantic analysis through a zero-copy memory architecture. Built for .NET 8, it addresses limitations of traditional AST-based approaches by combining Shared Packed Parse Forest (SPPF) and Code Property Graph (CPG) technologies.
Span<T> and readonly ref struct for stack-only accessdotnet add package DevelApp.CognitiveGraph
using DevelApp.CognitiveGraph;
using DevelApp.CognitiveGraph.Builder;
using DevelApp.CognitiveGraph.Schema;
// Create a graph for a simple expression
using var builder = new CognitiveGraphBuilder();
var properties = new List<(string key, PropertyValueType type, object value)>
{
("NodeType", PropertyValueType.String, "BinaryExpression"),
("Operator", PropertyValueType.String, "+"),
("IsAmbiguous", PropertyValueType.Boolean, false)
};
var rootNodeOffset = builder.WriteSymbolNode(
symbolId: 1,
nodeType: 200,
sourceStart: 0,
sourceLength: 13,
properties: properties
);
var buffer = builder.Build(rootNodeOffset, "hello + world");
// Read the graph with zero-copy access
using var graph = new CognitiveGraph(buffer);
var rootNode = graph.GetRootNode();
// Access properties with type safety
if (rootNode.TryGetProperty("Operator", out var op))
{
Console.WriteLine($"Operator: {op.AsString()}"); // Output: Operator: +
}
// Check for syntactic ambiguity
if (rootNode.IsAmbiguous)
{
var interpretations = rootNode.GetPackedNodes();
Console.WriteLine($"Found {interpretations.Count} parse interpretations");
}
| Platform | Architecture | Status | Performance |
|---|---|---|---|
| Windows | x64, x86, ARM64 | ā Full Support | āāāāā |
| Linux | x64, ARM64 | ā Full Support | āāāāā |
| macOS | x64, ARM64 (M1/M2) | ā Full Support | āāāāā |
| Operation | Time | Memory | |āāāā|āā|āāā| | Graph Creation (1K nodes) | <1ms | ~50KB | | Property Access | ~10ns | 0 bytes | | Ambiguity Resolution | ~100ns | 0 bytes | | Thread-safe Reading | ~15ns | 0 bytes |
// Create an ambiguous expression: "a+b*c" can be parsed as ((a+b)*c) or (a+(b*c))
var packed1 = builder.WritePackedNode(ruleId: 1); // First interpretation
var packed2 = builder.WritePackedNode(ruleId: 2); // Second interpretation
var ambiguousNode = builder.WriteSymbolNode(
symbolId: 1,
nodeType: 200,
sourceStart: 0,
sourceLength: 5,
packedNodeOffsets: new List<uint> { packed1, packed2 }
);
// Later, analyze all possible interpretations
if (node.IsAmbiguous)
{
foreach (var interpretation in node.GetPackedNodes())
{
Console.WriteLine($"Rule ID: {interpretation.RuleID}");
// Process each possible parse tree
}
}
// For analyzing huge codebases
using var mmf = MemoryMappedFile.CreateFromFile("huge-graph.bin");
using var accessor = mmf.CreateViewAccessor();
unsafe
{
byte* ptr = (byte*)accessor.SafeMemoryMappedViewHandle.DangerousGetHandle();
var buffer = new CognitiveGraphBuffer(new ReadOnlySpan<byte>(ptr, (int)accessor.Capacity));
using var graph = new CognitiveGraph(buffer);
// Process without loading entire file into memory
}
ReadOnlySpan<T>git checkout -b feature/amazing-featuredotnet testgit commit -m 'Add amazing feature'git push origin feature/amazing-featureThis project is licensed under the AGPL 3.0 License - see the LICENSE file for details.
Built with ā¤ļø for the developer community by DevelApp-ai