tiny.pluck.toplevel.result
Defined in toplevel.
API (4)
Actions
Public operations.
Types and contracts
Public types and contracts.
Source
Source: lib/pluck/src/toplevel/result.zig
zig
const std = @import("std");const pluck = @import("../root.zig");const Allocator = std.mem.Allocator;const evaluator = pluck.evaluator;const LazyKCStats = evaluator.LazyKCStats;const LimitReason = evaluator.LimitReason;const top_types = @import("types.zig");const QueryOutcome = top_types.QueryOutcome;const SourceLocation = top_types.SourceLocation;const QueryOutcomeProbabilityOrder = struct { fn lessThan(_: void, a: QueryOutcome, b: QueryOutcome) bool { return a.probability > b.probability; }};pub const QueryResult = struct { outcomes: []QueryOutcome, stats: LazyKCStats, limit_reason: ?LimitReason, program_error: bool, allocator: Allocator, source_location: ?SourceLocation = null, pub fn deinit(self: *QueryResult) void { for (self.outcomes) |outcome| { self.allocator.free(outcome.value_str); } self.allocator.free(self.outcomes); } pub fn clone(self: *const QueryResult, allocator: Allocator) !QueryResult { const new_outcomes = try allocator.alloc(QueryOutcome, self.outcomes.len); var allocated_count: usize = 0; errdefer { for (new_outcomes[0..allocated_count]) |outcome| { allocator.free(outcome.value_str); } allocator.free(new_outcomes); } for (self.outcomes, 0..) |outcome, i| { new_outcomes[i] = .{ .value_str = try allocator.dupe(u8, outcome.value_str), .probability = outcome.probability, }; allocated_count += 1; } return .{ .outcomes = new_outcomes, .stats = self.stats, .limit_reason = self.limit_reason, .program_error = self.program_error, .allocator = allocator, .source_location = self.source_location, }; } fn formatProbability(writer: anytype, value_str: []const u8, prob: f64) !void { if (prob >= 1e-4 or prob == 0.0) { try writer.print("{s}: {d:.6}\n", .{ value_str, prob }); } else { try writer.print("{s}: {e}\n", .{ value_str, prob }); } } pub fn print(self: *const QueryResult, writer: anytype) !void { if (self.limit_reason) |reason| { try writer.print("Query hit limit: {s}\n", .{reason.message()}); } if (self.program_error) { try writer.writeAll("Query encountered program error\n"); return; } if (self.outcomes.len == 0) { try writer.writeAll("No outcomes (probability 0)\n"); return; } const sorted = self.allocator.alloc(QueryOutcome, self.outcomes.len) catch { for (self.outcomes) |outcome| { try formatProbability(writer, outcome.value_str, outcome.probability); } return; }; defer self.allocator.free(sorted); @memcpy(sorted, self.outcomes); std.mem.sort(QueryOutcome, sorted, {}, QueryOutcomeProbabilityOrder.lessThan); for (sorted) |outcome| { if (outcome.probability > 0) { try formatProbability(writer, outcome.value_str, outcome.probability); } } const time_ms = @as(f64, @floatFromInt(self.stats.time_ns)) / 1_000_000.0; try writer.print("\nTime: {d:.3}ms\n", .{time_ms}); if (self.stats.wmc_time_ns > 0) { const wmc_ms = @as(f64, @floatFromInt(self.stats.wmc_time_ns)) / 1_000_000.0; try writer.print("WMC time: {d:.3}ms\n", .{wmc_ms}); } if (self.stats.refinement_time_ns > 0) { const refine_ms = @as(f64, @floatFromInt(self.stats.refinement_time_ns)) / 1_000_000.0; try writer.print("Refinement time: {d:.3}ms ({d} refinements)\n", .{ refine_ms, self.stats.refinement_count, }); } if (self.stats.num_recursive_calls > 0) { try writer.print("ITE calls: {d}\n", .{self.stats.num_recursive_calls}); } if (self.stats.num_forward_calls > 0) { try writer.print("Forward calls: {d}\n", .{self.stats.num_forward_calls}); } if (self.stats.variable_count > 0) { try writer.print("BDD variables: {d}\n", .{self.stats.variable_count}); } if (self.stats.node_count > 0) { try writer.print("BDD nodes: {d}\n", .{self.stats.node_count}); } if (self.stats.unique_table_grows > 0) { try writer.print("BDD unique table grows: {d}\n", .{self.stats.unique_table_grows}); } if (self.stats.ite_cache_grows > 0) { try writer.print("ITE cache grows: {d}\n", .{self.stats.ite_cache_grows}); } const total_cache = self.stats.ite_cache_hits + self.stats.ite_cache_misses; if (total_cache > 0) { const hit_ratio = @as(f64, @floatFromInt(self.stats.ite_cache_hits)) / @as(f64, @floatFromInt(total_cache)) * 100.0; try writer.print("ITE cache: {d} hits, {d} misses ({d:.1}% hit rate)\n", .{ self.stats.ite_cache_hits, self.stats.ite_cache_misses, hit_ratio, }); } const total_thunks = self.stats.thunk_reuse_hits + self.stats.thunk_reuse_misses; if (total_thunks > 0) { const reuse_ratio = @as(f64, @floatFromInt(self.stats.thunk_reuse_hits)) / @as(f64, @floatFromInt(total_thunks)) * 100.0; try writer.print("Thunk creation: {d} reused, {d} new ({d:.1}% reuse)\n", .{ self.stats.thunk_reuse_hits, self.stats.thunk_reuse_misses, reuse_ratio, }); } if (self.stats.thunk_evaluations > 0) { const cache_ratio = @as(f64, @floatFromInt(self.stats.thunk_cache_hits)) / @as(f64, @floatFromInt(self.stats.thunk_evaluations)) * 100.0; try writer.print("Thunk eval: {d} total, {d} cache hits ({d:.1}% hit rate)\n", .{ self.stats.thunk_evaluations, self.stats.thunk_cache_hits, cache_ratio, }); } }};Source: lib/pluck/src/toplevel/root.zig:2
zig
pub const result = @import("result.zig");Audit
| Definitions | 5 |
|---|---|
| Public names | 9 |
| Members | 6 |
| Version | 26.7.0 |
| Revision | daab053ee433 |