Skip to main content
Glama

estimate_compute_units

Analyze Zig code to estimate computational complexity and resource usage, enabling better optimization decisions and efficient code implementation.

Instructions

Estimate computational complexity and resource usage

Input Schema

NameRequiredDescriptionDefault
codeYesZig code to analyze

Input Schema (JSON Schema)

{ "properties": { "code": { "description": "Zig code to analyze", "type": "string" } }, "required": [ "code" ], "type": "object" }

Implementation Reference

  • The main handler function that executes the 'estimate_compute_units' tool logic. It analyzes the provided Zig code using ZigCodeAnalyzer utilities to estimate memory usage, time complexity, and allocations, then formats the results into a markdown report.
    private async estimateComputeUnits(code: string): Promise<string> { Logger.debug('Estimating compute units for code'); // Analyze code for computational complexity using new utility classes const analysis: CodeAnalysisResult = { memoryUsage: ZigCodeAnalyzer.analyzeMemoryUsage(code), timeComplexity: ZigCodeAnalyzer.analyzeTimeComplexity(code), allocations: ZigCodeAnalyzer.analyzeAllocations(code), }; return ` # Compute Units Estimation ## Memory Usage: ${analysis.memoryUsage} ## Time Complexity: ${analysis.timeComplexity} ## Allocation Analysis: ${analysis.allocations} ## Recommendations: - Consider using arena allocators for batch allocations - Profile memory usage with --enable-logging - Use comptime evaluation to reduce runtime overhead - Consider memory pool allocation for frequent allocations `.trim(); }
  • src/index.ts:203-216 (registration)
    Tool registration in the ListToolsRequestSchema response, defining the tool name, description, and input schema.
    name: 'estimate_compute_units', description: 'Estimate computational complexity and resource usage with detailed analysis', inputSchema: { type: 'object', properties: { code: { type: 'string', description: 'Zig code to analyze', }, }, required: ['code'], }, },
  • Input schema definition for the 'estimate_compute_units' tool, specifying the required 'code' parameter as a string.
    inputSchema: { type: 'object', properties: { code: { type: 'string', description: 'Zig code to analyze', }, }, required: ['code'], },
  • ZigCodeAnalyzer class providing static helper methods analyzeMemoryUsage, analyzeTimeComplexity, and analyzeAllocations used by the handler. Includes regex patterns for code analysis.
    export class ZigCodeAnalyzer { static readonly MEMORY_PATTERNS: MemoryPattern = { heapAlloc: /std\.(ArrayList|StringHashMap|AutoHashMap)/g, stackAlloc: /var\s+\w+\s*:\s*\[(\d+)\]/g, slices: /\[\](?:u8|i32|f64)/g, }; static readonly TIME_COMPLEXITY_PATTERNS: TimeComplexityPattern = { loops: /(?:while|for)\s*\(/g, nestedLoops: /(?:while|for)[^{]*\{[^}]*(?:while|for)/g, recursion: /fn\s+\w+[^{]*\{[^}]*\w+\s*\([^)]*\)/g, }; static readonly ALLOCATION_PATTERNS: AllocationPattern = { comptime: /comptime\s/g, arena: /std\.heap\.ArenaAllocator/g, fixedBuf: /std\.heap\.FixedBufferAllocator/g, }; /** * Analyzes memory usage patterns in Zig code */ static analyzeMemoryUsage(code: string): string { const heapAllocs = (code.match(this.MEMORY_PATTERNS.heapAlloc) || []).length; const stackAllocs = (code.match(this.MEMORY_PATTERNS.stackAlloc) || []).length; const sliceUsage = (code.match(this.MEMORY_PATTERNS.slices) || []).length; return ` - Heap Allocations: ${heapAllocs} detected - Stack Allocations: ${stackAllocs} detected - Slice Usage: ${sliceUsage} instances - Memory Profile: ${heapAllocs > stackAllocs ? 'Heap-heavy' : 'Stack-optimized'} `.trim(); } /** * Analyzes time complexity patterns */ static analyzeTimeComplexity(code: string): string { const loops = (code.match(this.TIME_COMPLEXITY_PATTERNS.loops) || []).length; const nestedLoops = (code.match(this.TIME_COMPLEXITY_PATTERNS.nestedLoops) || []).length; const recursion = (code.match(this.TIME_COMPLEXITY_PATTERNS.recursion) || []).length; let complexity = 'O(1)'; if (nestedLoops > 0) { complexity = 'O(n²)'; } else if (loops > 0) { complexity = 'O(n)'; } if (recursion > 0) { complexity += ' with recursive calls'; } return ` - Estimated Complexity: ${complexity} - Loop Count: ${loops} - Nested Loops: ${nestedLoops} - Recursive Patterns: ${recursion} detected `.trim(); } /** * Analyzes allocation patterns */ static analyzeAllocations(code: string): string { const comptimeUsage = (code.match(this.ALLOCATION_PATTERNS.comptime) || []).length; const arenaAlloc = (code.match(this.ALLOCATION_PATTERNS.arena) || []).length; const fixedBufAlloc = (code.match(this.ALLOCATION_PATTERNS.fixedBuf) || []).length; return ` - Comptime Evaluations: ${comptimeUsage} - Arena Allocators: ${arenaAlloc} - Fixed Buffer Allocators: ${fixedBufAlloc} - Allocation Strategy: ${this.determineAllocStrategy(arenaAlloc, fixedBufAlloc)} `.trim(); } private static determineAllocStrategy(arenaCount: number, fixedBufCount: number): string { if (arenaCount > 0 && fixedBufCount > 0) { return 'Mixed allocation strategy'; } if (arenaCount > 0) { return 'Arena-based allocation'; } if (fixedBufCount > 0) { return 'Fixed buffer allocation'; } return 'Default allocator usage'; } }
  • Dispatcher case in CallToolRequestSchema handler that validates input and invokes the estimateComputeUnits method.
    case 'estimate_compute_units': this.validateStringParam(args?.code, 'code'); return { content: [ { type: 'text', text: await this.estimateComputeUnits(args.code), }, ], };

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/openSVM/zig-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server