Compare commits

...

3 Commits

Author SHA1 Message Date
17e3e0397f add environment variable to enable profile stats collection 2025-06-12 20:53:22 +01:00
1486e5a9e0 avoid reffing g:line_to in loop
it makes a difference on the flame graph but not a noticeable one in
top
2025-06-11 23:11:20 +01:00
deedd02efd add LuaProfiler to dev shell 2025-06-11 22:06:54 +01:00
2 changed files with 71 additions and 32 deletions

View File

@ -8,6 +8,10 @@
(import-macros { : define-tests : expect : expect= } :assert)
(local profile
(and (os.getenv "IN_NIX_SHELL")
(require :libluaperf)))
(macro with-timing [label & body]
`(let [before# (ptime.clock_gettime ptime.CLOCK_PROCESS_CPUTIME_ID)
ret# (table.pack (do ,body))]
@ -58,13 +62,17 @@ label.readout {
)
(style_provider:load_from_data CSS)))
(fn main-quit []
(when profile (profile:stop))
(Gtk.main_quit))
(local window (Gtk.Window {
:title "Map"
:name "toplevel"
:default_width viewport-width
:default_height viewport-height
:on_destroy Gtk.main_quit
:on_destroy main-quit
}))
(local state-widgets { })
@ -150,38 +158,38 @@ label.readout {
(local cq (cqueues.new))
(fn road-width-for [line offset]
(+ (or offset 0)
(case (?. line :tags :highway)
:motorway 18
:trunk 17
:primary 16
:secondary 14
:cycleway 4
:footway 4
other 12)))
(fn cairo-road-path [g [[sx sy] & points] bounds width]
(g:save)
(g:set_line_width width)
(g:move_to (* tile-size (- sx bounds.min.x))
(* tile-size (- sy bounds.min.y)))
(each [_ [x y] (ipairs points)]
(let [x1 (* tile-size (- x bounds.min.x))
y1 (* tile-size (- y bounds.min.y))]
(g:line_to x1 y1)))
(g:stroke)
(g:restore))
(fn road-width-for [line]
(case (?. line :tags :highway)
:motorway 18
:trunk 17
:primary 16
:secondary 14
:cycleway 4
:footway 4
other 12))
(fn cairo-road-path [g [[sx sy] & points] bounds]
(let [min bounds.min
{ : line_to } g]
(g:move_to (* tile-size (- sx min.x))
(* tile-size (- sy min.y)))
(each [_ [x y] (ipairs points)]
(let [x1 (* tile-size (- x min.x))
y1 (* tile-size (- y min.y))]
(line_to g x1 y1)))))
(fn cairo-roads [g lines bounds]
(let [road-width 14]
(g:set_source_rgb 0 0 0)
(each [_ line (pairs lines)]
(cairo-road-path g line.points bounds (road-width-for line)))
(g:set_source_rgb 1 1 1)
(each [_ line (pairs lines)]
(cairo-road-path g line.points bounds (road-width-for line -2)))))
(g:set_source_rgb 0 0 0)
(each [_ line (pairs lines)]
(g:set_line_width (road-width-for line))
(cairo-road-path g line.points bounds )
(g:stroke))
(g:set_source_rgb 1 1 1)
(each [_ line (pairs lines)]
(g:set_line_width (- (road-width-for line) 2))
(cairo-road-path g line.points bounds)
(g:stroke)))
(var map-surface nil)
@ -453,6 +461,15 @@ label.readout {
true)
nil nil)
(fn collect-profile []
(GLib.timeout_add
GLib.PRIORITY_DEFAULT
(* 60 1000) main-quit
nil nil)
(print "profiling for 60 seconds")
(profile.start 0))
(window:add
(doto (Gtk.Overlay {})
(: :add (osm-widget))
@ -463,4 +480,5 @@ label.readout {
(window:show_all)
(styles)
(when (os.getenv "MAP_PROFILE") (collect-profile))
(Gtk:main)

View File

@ -16,13 +16,34 @@ let
};
};
fennel-ls = pkgs.fennel-ls.override { inherit (package) lua luaPackages; };
luaProfiler =
let
inherit (pkgs) stdenv cmake;
inherit (package) lua;
in stdenv.mkDerivation {
name = "LuaProfiler";
src = fetchFromGitHub {
owner = "Patrick08T";
repo = "LuaProfiler";
rev = "abb989337f6f46b820c9d2eb1e1d339e8f6f3760";
hash = "sha256-43kwZCZZmWD5ens1qCzD7LTg/jKMfcb9Vw/DBiN2sSo=";
};
buildInputs = [ lua ];
installPhase = ''
mkdir -p "$out/lib/lua/${lua.luaversion}"
echo cp /build/source/bin/release/libluaperf.so "$out/lib/lua/${lua.luaversion}"
cp /build/source/bin/release/libluaperf.so "$out/lib/lua/${lua.luaversion}"
'';
nativeBuildInputs = [ cmake ];
};
in
package.overrideAttrs(o: {
nativeBuildInputs = [ fennel-ls ] ++ o.nativeBuildInputs;
nativeBuildInputs = [ fennel-ls luaProfiler flamegraph ] ++ o.nativeBuildInputs;
shellHook = ''
mkdir -p bin
( cd bin && ln -sf `type -p fennel-ls` `type -p fennel` . )
export LUA_CPATH=$(lua -e "print(package.cpath)")
export LUA_CPATH=$(lua -e "print(package.cpath)")\;${luaProfiler}/lib/lua/${package.lua.luaversion}/\?.so
export LUA_PATH=$(lua -e "print(package.path)")\;$RXI_JSON/share/lua/5.3/?.lua
'';
})