| 1 | ---------------------------------------------------------------------------- |
| 2 | -- LuaJIT profiler zones. |
| 3 | -- |
| 4 | -- Copyright (C) 2005-2025 Mike Pall. All rights reserved. |
| 5 | -- Released under the MIT license. See Copyright Notice in luajit.h |
| 6 | ---------------------------------------------------------------------------- |
| 7 | -- |
| 8 | -- This module implements a simple hierarchical zone model. |
| 9 | -- |
| 10 | -- Example usage: |
| 11 | -- |
| 12 | -- local zone = require("jit.zone") |
| 13 | -- zone("AI") |
| 14 | -- ... |
| 15 | -- zone("A*") |
| 16 | -- ... |
| 17 | -- print(zone:get()) --> "A*" |
| 18 | -- ... |
| 19 | -- zone() |
| 20 | -- ... |
| 21 | -- print(zone:get()) --> "AI" |
| 22 | -- ... |
| 23 | -- zone() |
| 24 | -- |
| 25 | ---------------------------------------------------------------------------- |
| 26 | |
| 27 | local remove = table.remove |
| 28 | |
| 29 | return setmetatable({ |
| 30 | flush = function(t) |
| 31 | for i=#t,1,-1 do t[i] = nil end |
| 32 | end, |
| 33 | get = function(t) |
| 34 | return t[#t] |
| 35 | end |
| 36 | }, { |
| 37 | __call = function(t, zone) |
| 38 | if zone then |
| 39 | t[#t+1] = zone |
| 40 | else |
| 41 | return (assert(remove(t), "empty zone stack")) |
| 42 | end |
| 43 | end |
| 44 | }) |
| 45 |