zone.lua 1002 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. local remove = table.remove
  27. return setmetatable({
  28. flush = function(t)
  29. for i=#t,1,-1 do t[i] = nil end
  30. end,
  31. get = function(t)
  32. return t[#t]
  33. end
  34. }, {
  35. __call = function(t, zone)
  36. if zone then
  37. t[#t+1] = zone
  38. else
  39. return (assert(remove(t), "empty zone stack"))
  40. end
  41. end
  42. })