diff options
Diffstat (limited to 'src/trace/store.zig')
| -rw-r--r-- | src/trace/store.zig | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/trace/store.zig b/src/trace/store.zig index 0b7e8fe..e4546c2 100644 --- a/src/trace/store.zig +++ b/src/trace/store.zig @@ -566,6 +566,8 @@ pub const Trace = struct { key_moments: []u64, /// Every string the recording defines, by id. strings: std.AutoHashMapUnmanaged(u64, []const u8) = .empty, + /// Their ids, in order: for `stringBelow`. + ids: []u64 = &.{}, strings_arena: std.heap.ArenaAllocator, cache: *Cache, @@ -618,6 +620,10 @@ pub const Trace = struct { } } } + t.ids = try t.strings_arena.allocator().alloc(u64, t.strings.count()); + var ids = t.strings.keyIterator(); + for (t.ids) |*id| id.* = ids.next().?.*; + std.mem.sortUnstable(u64, t.ids, {}, std.sort.asc(u64)); const root = try Map.init(db.rootCursor().readOnly()); if (try root.getCursor(key(.moments))) |cursor| t.len = try (try List.init(cursor)).count(); var key_moments: std.ArrayList(u64) = .empty; @@ -634,6 +640,20 @@ pub const Trace = struct { return t; } + /// The string with the greatest id at or below `id`, and that id: where + /// strings are keyed by where a range of addresses begins + /// (`event.frameId`), the one whose range may hold `id`. + pub fn stringBelow(t: *const Trace, id: u64) ?struct { id: u64, text: []const u8 } { + const after = std.sort.upperBound(u64, t.ids, id, struct { + fn order(want: u64, have: u64) std.math.Order { + return std.math.order(want, have); + } + }.order); + if (after == 0) return null; + const found = t.ids[after - 1]; + return .{ .id = found, .text = t.strings.get(found).? }; + } + fn root0(db: *ReadDB) Map { // An empty file has no root map yet: a map that holds nothing. return Map.init(db.rootCursor().readOnly()) catch unreachable; |
