ludic/examples/lang/string_temps.ludic
Orkuncakilkaya 0141f99dd1 Os.platform/arch uname once; render3d primes a new buffer's Metal buffer
lp_os_platform and lp_os_arch malloc'd 8 KB per call (the uname buffer) and kept none of it:
string_temps now asks both every round, 327 MB over 20,000 before, 0 after. Reseeded. render3d:
MoltenVK made a mapped buffer's MTLBuffer at its first bind (fn_gvk_draw +4 blocks in the boat
window); gvk_buf_reserve queues it and the next frame's command buffer copies 4 bytes out of it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 13:34:45 +03:00

51 lines
2.4 KiB
Text

# string_temps.ludic - a string made inside an expression and held by nothing (`a + "/"` in
# `a + "/" + b`, a template's pieces, a number's text, a slice, a side only compared) is freed as soon as it
# has been used, and one that IS held - in a variable, handed to a function, a function's result -
# is not. Prints 1 2 3 4 5 6 7 and `grew 0` (the heap is read on the Mac; 0 elsewhere).
program StringTemps {
function keep(s: string) -> string { return s }
function tag(i: int) -> string { return `t{i}` }
# every shape of throwaway text, compared against itself built another way: nothing survives
function round(i: int, a: string, b: string) -> int {
var bad = 0
if a + "/" + b + "/" + string(i) != `{a}/{b}/{i}` { bad += 1 }
let p = "lake/camp.png" # a literal: the slices are what is made here
if p[len(p) - 4 .. len(p)] != ".png" or p[0 .. len(p) - 4] + ".dds" != `lake/{b[0 .. 0]}camp.dds` { bad += 1 }
if Os.platform() == "" or Os.arch() == "" { bad += 1 } # asked every round: nothing each time
if `{i}-{i * 2}-{long(i) * 1000000000}` != string(i) + "-" + string(i * 2) + "-" + string(long(i) * 1000000000) { bad += 1 }
return bad
}
entry {
let a = "dir"
let b = "file"
# held intermediates stay good after the `+` that read them
let mid = a + "/"
let full = mid + b
let head = full[0 .. 3]
let head2 = head + "!" + head
if mid == "dir/" and full == "dir/file" and head == "dir" and head2 == "dir!dir" { print(1) }
let n = string(-42)
let t = n + "!" + n
if n == "-42" and t == "-42!-42" { print(2) }
let k = keep(a + "+" + b)
let k2 = k + "=" + k
if k == "dir+file" and k2 == "dir+file=dir+file" { print(3) }
var acc = ""
for i in 0 .. 4 { acc += string(i) + "," }
if acc == "0,1,2,3," { print(4) }
if string(0) == "0" and string(2147483647) == "2147483647" and `{long(9000000000)}` == "9000000000" { print(5) }
let nested = `a{`b{1 + 1}c`}d`
# a function's result is its caller's to keep (keep() hands back its own argument), so only
# the text built round it goes
if nested == "ab2cd" and "[" + tag(3) + "]" == "[t3]" { print(6) }
var bad = 0
for i in 0 .. 200 { bad += round(i, a, b) }
if bad == 0 { print(7) }
let before = Os.heap_bytes()
for i in 0 .. 20000 { bad += round(i, a, b) }
let grew = Os.heap_bytes() - before
if bad == 0 { print(`grew {grew}`) } else { print(`bad {bad}`) }
}
}