CognitiveGraph - High-Performance Zero-Copy Cognitive Graph

High-Performance Zero-Copy Cognitive Graph for Advanced Code Analysis

CognitiveGraph

High-Performance Zero-Copy Cognitive Graph for Advanced Code Analysis

CI/CD Pipeline NuGet Package Platform Support

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.

šŸš€ Key Features

Zero-Copy Architecture

Advanced Code Analysis

Production Ready

šŸ“¦ Installation

dotnet add package DevelApp.CognitiveGraph

šŸ”§ Quick Start

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");
}

šŸ“š Documentation

šŸŒ Platform Compatibility

Platform Architecture Status Performance
Windows x64, x86, ARM64 āœ… Full Support ⭐⭐⭐⭐⭐
Linux x64, ARM64 āœ… Full Support ⭐⭐⭐⭐⭐
macOS x64, ARM64 (M1/M2) āœ… Full Support ⭐⭐⭐⭐⭐

Requirements

šŸ“Š Performance Characteristics

Benchmarks

| 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 |

šŸ›  Advanced Usage

Handling Syntactic Ambiguity

// 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
    }
}

Memory-Mapped Files for Large Datasets

// 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
}

šŸ”’ Security and Safety

šŸ¤ Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Make your changes and add tests
  4. Ensure all tests pass: dotnet test
  5. Commit your changes: git commit -m 'Add amazing feature'
  6. Push to the branch: git push origin feature/amazing-feature
  7. Open a Pull Request

šŸ“„ License

This project is licensed under the AGPL 3.0 License - see the LICENSE file for details.

🌟 Why Choose CognitiveGraph?


Built with ā¤ļø for the developer community by DevelApp-ai