tiny.profiling.ingest.matching
Defined in ingest.
API (10)
Actions
Public operations.
Matcher.eventBenchEndMatcher.feedMatcher.kindMatcher.kindsMatcher.schemaMetricMatcher.summaryMemoryMatcher.valuesfeedToken
Types and contracts
Public types and contracts.
Source
Source: src/profiling/ingest/matching.zig
zig
const std = @import("std");const ingest = @import("root.zig");pub const StringPart = enum(u8) { partial, terminal,};const Family = enum(u8) { kind, schema, event, summary,};comptime { std.debug.assert(kind_matches.len <= @bitSizeOf(u32)); std.debug.assert(kind_matches[0].name.len > 0);}pub const Matcher = struct { index: usize, mask: u32, family: Family, pub fn kinds() Matcher { const result = Matcher{ .index = 0, .mask = fullMask(kind_matches.len), .family = .kind }; std.debug.assert(result.mask == fullMask(kind_matches.len)); std.debug.assert(result.index == 0); return result; } pub fn values(source_field: ingest.event.Field) Matcher { return switch (source_field) { .kind => kinds(), .schema => single(.schema), .event => single(.event), .summary => single(.summary), else => single(.summary), }; } pub fn feed(self: *Matcher, bytes: []const u8) void { std.debug.assert(bytes.len <= std.math.maxInt(usize) - self.index); const initial_index = self.index; const initial_mask = self.mask; for (bytes) |byte| { self.feedByte(byte); self.index += 1; } std.debug.assert(self.index == initial_index + bytes.len); std.debug.assert(self.mask & initial_mask == self.mask); } pub fn kind(self: Matcher) ?ingest.event.EventKind { std.debug.assert(self.family == .kind); inline for (kind_matches, 0..) |entry, index| { if (self.matches(entry.name, index)) return entry.value; } return null; } pub fn schemaMetric(self: Matcher) bool { std.debug.assert(self.family == .schema); return self.matches(metric_schema, 0); } pub fn eventBenchEnd(self: Matcher) bool { std.debug.assert(self.family == .event); return self.matches(bench_end, 0); } pub fn summaryMemory(self: Matcher) bool { std.debug.assert(self.family == .summary); return self.matches(memory, 0); } fn feedByte(self: *Matcher, byte: u8) void { std.debug.assert(self.mask <= fullMask(kind_matches.len)); std.debug.assert(self.index <= std.math.maxInt(usize) - 1); switch (self.family) { .kind => inline for (kind_matches, 0..) |entry, index| { if (self.index >= entry.name.len or entry.name[self.index] != byte) { self.remove(index); } }, .schema => if (self.index >= metric_schema.len or metric_schema[self.index] != byte) self.remove(0), .event => if (self.index >= bench_end.len or bench_end[self.index] != byte) self.remove(0), .summary => if (self.index >= memory.len or memory[self.index] != byte) self.remove(0), } } fn matches(self: Matcher, name: []const u8, index: usize) bool { std.debug.assert(index < @bitSizeOf(u32)); std.debug.assert(name.len > 0); return self.mask & (@as(u32, 1) << @intCast(index)) != 0 and self.index == name.len; } fn remove(self: *Matcher, index: usize) void { std.debug.assert(index < @bitSizeOf(u32)); self.mask &= ~(@as(u32, 1) << @intCast(index)); } fn single(family: Family) Matcher { std.debug.assert(family != .kind); return .{ .index = 0, .mask = 1, .family = family }; }};pub fn feedToken(matcher: *Matcher, token: std.json.Token) ?StringPart { switch (matcher.family) { .kind => std.debug.assert(matcher.family == .kind), .schema => std.debug.assert(matcher.family == .schema), .event => std.debug.assert(matcher.family == .event), .summary => std.debug.assert(matcher.family == .summary), } switch (token) { .partial_string => |bytes| matcher.feed(bytes), .partial_string_escaped_1 => |bytes| matcher.feed(&bytes), .partial_string_escaped_2 => |bytes| matcher.feed(&bytes), .partial_string_escaped_3 => |bytes| matcher.feed(&bytes), .partial_string_escaped_4 => |bytes| matcher.feed(&bytes), .string => |bytes| { matcher.feed(bytes); return .terminal; }, else => return null, } return .partial;}fn fullMask(count: usize) u32 { std.debug.assert(count > 0); std.debug.assert(count < @bitSizeOf(u32)); return (@as(u32, 1) << @intCast(count)) - 1;}const KindMatch = struct { name: []const u8, value: ingest.event.EventKind,};const kind_matches = [_]KindMatch{ .{ .name = "alloc", .value = .alloc }, .{ .name = "free", .value = .free }, .{ .name = "resize", .value = .resize }, .{ .name = "remap", .value = .remap }, .{ .name = "allocator", .value = .allocator }, .{ .name = "summary", .value = .summary },};const metric_schema = "tiny.profiling.metric/v1";const bench_end = "bench_end";const memory = "memory";Source: src/profiling/ingest/root.zig:4
zig
pub const matching = @import("matching.zig");Audit
| Definitions | 11 |
|---|---|
| Public names | 11 |
| Members | 5 |
| Version | 26.7.0 |
| Revision | daab053ee433 |