High-Performance Zero-Copy Cognitive Graph for Advanced Code Analysis
A high-performance C# library implementing the Zero-Copy Cognitive Graph architecture for advanced code analysis. This library unifies syntactic ambiguity handling through Shared Packed Parse Forests (SPPF) with semantic analysis via Code Property Graphs (CPG) in a memory-efficient, zero-copy data structure.
readonly ref struct and Span<T> for allocation-free access patternsdotnet add package DevelApp.CognitiveGraph
using DevelApp.CognitiveGraph;
using DevelApp.CognitiveGraph.Builder;
using DevelApp.CognitiveGraph.Schema;
// Create a graph for source code
using var builder = new CognitiveGraphBuilder();
// Add properties for semantic information
var properties = new List<(string key, PropertyValueType type, object value)>
{
("NodeType", PropertyValueType.String, "BinaryExpression"),
("Operator", PropertyValueType.String, "+")
};
// Create a node representing "hello + world"
var rootNodeOffset = builder.WriteSymbolNode(
symbolId: 1, // Expression
nodeType: 200, // BinaryExpression
sourceStart: 0,
sourceLength: 13,
properties: properties
);
// Build the final graph
var buffer = builder.Build(rootNodeOffset, "hello + world");
// Use the graph for analysis
using var graph = new CognitiveGraph(buffer);
Console.WriteLine($"Source: {graph.GetSourceText()}");
Console.WriteLine($"Parsed successfully: {graph.IsFullyParsed}");
var rootNode = graph.GetRootNode();
Console.WriteLine($"Node type: {rootNode.NodeType}");
// Access properties
if (rootNode.TryGetProperty("Operator", out var op))
{
Console.WriteLine($"Operator: {op.AsString()}");
}
The Cognitive Graph consists of several key components:
readonly ref struct types for stack-only allocationReadOnlySpan<T> for safe memory access// Check if a node has multiple interpretations
if (node.IsAmbiguous)
{
var interpretations = node.GetPackedNodes();
foreach (var interpretation in interpretations)
{
// Analyze each possible parse
var edges = interpretation.GetCpgEdges();
// ...
}
}
// Traverse semantic relationships
foreach (var edge in node.GetCpgEdges().OfType(EdgeType.DATA_FLOW))
{
var target = edge.GetTargetNode();
// Analyze data flow relationships
}
// Type-safe property access
if (node.TryGetProperty("LineNumber", out var lineNum))
{
int line = lineNum.AsInt32();
}
This project is licensed under the AGPL 3.0 License - see the LICENSE file for details.
Contributions are welcome! Please see the project repository for guidelines.
For detailed documentation on the architecture and implementation, see the PDF specification in the docs/ folder of the repository.