Sniffs: fields the sniffs read from codecs / containers (Aac.adts, IsoBmff.sniff, Zstd.MAGIC, ...) copied once onto shared proxies, so detection loads no codec chunk; chunked build: a Detect chunk (Sniffs + what it requires); Brotli: static dictionary inflated on first reference (a trial decode no longer loads Deflate)

This commit is contained in:
4DBug committed 2026-11-03 01:19:07 -06:00
1 parent d64adc978f
commit 3468bfb49a
14 files changed
+8966 -5389

No files matched your search

+10 -1
View File
@@ -249,7 +249,8 @@ def bundle_roblox():
# module with no entry yet requires its chunk (require(script:FindFirstChild(name)); under the luau CLI, with no
# `script`, require("@self/" .. name) over the same files in build/_modules/chunked/Rosetta). So requiring the root
# loads Core (what the root runs at load time) and every other chunk on first use, per Actor.
# Chunks: Core; one per hot codec (HOT); the rest packed per family (top folder, or codecs/<category>) in path order.
# Chunks: Core; Detect (DETECT: what F.detect runs, apart from Core); one per hot codec (HOT); the rest packed per family
# (top folder, or codecs/<category>) in path order.
# A --!native module's functions get @native (a chunk can mix native and non-native modules); a chunk with no native
# module has none. Outputs: build/Rosetta.rbxm (LZ4, written by model/rbxm under the luau CLI), build/Rosetta.rbxmx,
# build/Rosetta.rbxm.linemap.json ({chunk: [[first line, source file], ...]}).
@@ -257,6 +258,7 @@ CHUNK_SRC_MAX = 256 * 1024
CHUNK_IR_MAX = 1 << 18
CHUNK_ASM_MAX = 4 << 20
CHUNKED = os.path.join(MODULES, "chunked", "Rosetta")
DETECT = ["Sniffs"] # F.detect: Sniffs.luau and what it requires at load time (core/Sniff), one small chunk
HOT = [
("Mpeg2", ["codecs/video/Mpeg2", "codecs/video/Mpeg1", "codecs/video/Dsp"]),
("H263", ["codecs/video/H263"]),
@@ -331,6 +333,13 @@ def plan_chunks(mods):
core.add(k)
todo += sorted(eager(mods[k][0]))
pack("Core", sorted(core))
detect, todo = set(), [k for k in DETECT if k in left]
while todo:
k = todo.pop()
if k in left and k not in detect:
detect.add(k)
todo += sorted(eager(mods[k][0]))
pack("Detect", sorted(detect))
for name, prefixes in HOT:
keys = [k for k in left if any(under(k, p) for p in prefixes)]
if not keys:
+1 -1
View File
@@ -127,7 +127,7 @@ def sniffs(formats):
"\t})",
"end",
"",
]
] + sniff_extract.shared_lines()
for c in formats:
if c["sniff_block"] is None:
continue
+193 -5
View File
@@ -5,7 +5,14 @@
# loads the module to sniff it. Requires the copy reaches: core/* as they are; any other module through a proxy that
# requires it on first field access (sniff time).
# cost 0: the copy needs only core/*; 1: it reaches a proxied module; 2: not copied (the module sniffs).
import re
# A field the copy reads from a proxied module (Aac.adts, Zstd.MAGIC) is copied too when it reaches only core/*,
# literals and other such fields (of that module or of modules it requires; FIELD_MAX bytes at most per module): once,
# at the top of Sniffs.luau, onto the module's shared proxy, which then loads its module only for the fields left (a
# decoder). So detection runs Sniffs alone (build/Rosetta.rbxm: one small chunk), not the codecs behind the proxies.
import os, re
SRC = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "src", "Rosetta")
FIELD_MAX = 16384
KEYWORDS = set("and break continue do else elseif end false for function if in local nil not or repeat return then "
"true until while type export typeof".split())
@@ -173,17 +180,23 @@ def extract(text, path):
todo.append(k)
if bad:
return 2, None, self_name
left = {}
for j, (alias, mod) in lazy.items():
left[j] = share(mod, used_fields(alias, [sts[i][2] for i in include])) # the fields that load mod
USED.add(mod)
# a non-function statement that reads a proxied module runs at load time: the proxy would load it at once
for i in include:
if i != sniff_i and defines(sts[i][2])[1] not in ("func", "type"):
if free_names(sts[i][2]) & {a for a, _ in lazy.values()}:
return 2, None, self_name
for j, (alias, _) in lazy.items():
if alias in free_names(sts[i][2]) and (left[j] or not re.search(
r"(?<![.:\w])" + re.escape(alias) + r"\s*\.", sts[i][2])):
return 2, None, self_name
block = []
for i in sorted(set(include) | set(lazy)):
s, e, st = sts[i]
if i in lazy:
alias, mod = lazy[i]
block.append('local %s = lazy(function()\n\treturn require("@rosetta/%s")\nend)' % (alias, mod))
block.append("local %s = %s" % (alias, shared_name(mod)))
elif i == sniff_i:
src = "\n".join(lines[s:e + 1])
src = re.sub(r"^function\s+" + re.escape(self_name) + r"\.sniff\s*\(", 'S["%s"] = function(' % path, src)
@@ -191,7 +204,182 @@ def extract(text, path):
else:
src = "\n".join(lines[s:e + 1])
block.append(re.sub(r"^export\s+type\b", "type", src))
return (1 if lazy else 0), block, self_name
return (1 if any(reaches_left(m) for _, m in lazy.values()) else 0), block, self_name
# ------------------------------------------------------------------------------------------------ shared field copies
# SHARED: module -> [fields the sniffs read, fields left to the proxy]; one table per module at the top of Sniffs.luau
# (shared_lines), a lazy proxy when a field is left, with the copied fields set on it.
SHARED = {}
USED = set() # the modules the sniffs read (emit also adds what their copies read)
def shared_name(mod):
return "P_" + re.sub(r"\W", "_", mod)
def used_fields(alias, texts):
pat = re.compile(r"(?<![.:\w])" + re.escape(alias) + r"\s*\.\s*(" + NAME + ")")
return {f for t in texts for f in pat.findall(t)}
def module_file(mod):
for p in (os.path.join(SRC, mod + ".luau"), os.path.join(SRC, mod, "init.luau")):
if os.path.isfile(p):
return p
return None
def field_statements(mod, fields, depth):
"""(lines, self name, required modules) of the top-level statements module mod needs for fields (function M.f /
M.f = ..., with the locals, types, M fields, load-time statements and fields of required modules they reach), or
None"""
path = module_file(mod)
if path is None:
return None
with open(path) as f:
text = f.read()
lines = text.split("\n")
sts = statements(text)
self_name = None
for s, e, st in sts:
m = re.match(r"return\s+(" + NAME + r")\s*$", st)
if m:
self_name = m.group(1)
if self_name is None:
return None
field_at = {}
for i, (s, e, st) in enumerate(sts):
m = re.match(r"(?:function\s+)?" + re.escape(self_name) + r"\s*\.\s*(" + NAME + r")\s*(\(|=(?!=))", st)
if m and m.group(1) not in field_at:
field_at[m.group(1)] = i
defs = []
for i, (s, e, st) in enumerate(sts):
names, kind = defines(st)
for n in names:
defs.append((i, n, kind))
others = [i for i, (s, e, st) in enumerate(sts) if defines(st)[1] is None and i not in field_at.values()
and not re.match(r"return\b", st)]
def resolve(name, before):
best = None
for i, n, kind in defs:
if n == name and i < before:
best = (i, kind)
return best
include, reqs, todo, reached, got = set(), {}, [], set(), set()
for f in fields:
if f not in field_at:
return None
todo.append(field_at[f])
got.add(f)
while todo:
while todo:
i = todo.pop()
if i in include:
continue
include.add(i)
st = sts[i][2]
for f in used_fields(self_name, [st]):
if f in field_at:
got.add(f)
todo.append(field_at[f])
for n in free_names(st):
if n == self_name:
continue
r = resolve(n, i)
if r is None:
continue
j, kind = r
if kind == "forward":
return None
if kind == "local" and re.search(r"^[ \t]*" + re.escape(n) + r"\s*=(?!=)", text, re.M):
return None # module state (a cache): a copy would hold a second one
reached.add(n)
req = REQUIRE.match(lines[sts[j][0]]) if sts[j][0] == sts[j][1] else None
if req and not req.group(2).startswith("core/"):
reqs[j] = req.group(1), req.group(2)
continue
todo.append(j)
# load-time statements that fill a reached local or field (a table built by a loop)
for k in others:
if k not in include and (free_names(sts[k][2]) & reached or used_fields(self_name, [sts[k][2]]) & got):
todo.append(k)
out, deps = [], []
for i in sorted(include | set(reqs)):
s, e, st = sts[i]
if i in reqs:
alias, sub = reqs[i]
used = used_fields(alias, [sts[k][2] for k in include])
if depth >= 3 or not used or alias in free_names(" ".join(
re.sub(r"(?<![.:\w])" + re.escape(alias) + r"\s*\.\s*" + NAME, "", sts[k][2]) for k in include)):
return None # the module table itself is used, not only its fields
share(sub, used, depth + 1) # a field left there loads sub when the copy reaches it
deps.append(sub)
out.append("local %s = %s" % (alias, shared_name(sub)))
else:
out.append(re.sub(r"^export\s+type\b", "type", "\n".join(lines[s:e + 1])))
if sum(len(x) + 1 for x in out) > FIELD_MAX:
return None
return out, self_name, deps
def share(mod, fields, depth=0):
"""registers fields of mod for Sniffs.luau; -> the fields left to the proxy"""
e = SHARED.setdefault(mod, [set(), set()])
new = set(fields) - e[0] - e[1]
if new:
if field_statements(mod, sorted(new | e[0]), depth) is not None:
e[0] |= new
else:
for f in sorted(new): # each field alone: some may copy
if field_statements(mod, sorted(e[0] | {f}), depth) is not None:
e[0].add(f)
else:
e[1].add(f)
return set(fields) & e[1]
def reaches_left(mod, seen=None):
"""a sniff reading mod may load a module (a field left to a proxy, there or in what the copies read)"""
seen = set() if seen is None else seen
if mod in seen:
return False
seen.add(mod)
copied, left = SHARED[mod]
got = field_statements(mod, sorted(copied), 0) if copied else None
return bool(left) or (got is not None and any(reaches_left(d, seen) for d in got[2]))
def shared_lines():
"""the top of Sniffs.luau: the shared proxies / copies, each after the modules its copies read"""
out, done = [], set()
def emit(mod):
if mod in done:
return
done.add(mod)
copied, left = SHARED[mod]
got = field_statements(mod, sorted(copied), 0) if copied else ([], None, [])
body, self_name, deps = got
for d in deps:
emit(d)
name = shared_name(mod)
if left:
out.append('local %s = lazy(function()\n\treturn require("@rosetta/%s")\nend)' % (name, mod))
else:
out.append("local %s: any = {}" % name)
if copied:
out.append("do -- %s: %s" % (mod, ", ".join(sorted(copied))))
out.append("local %s = %s" % (self_name, name))
out.extend(body)
out.append("end")
out.append("")
for mod in sorted(USED):
emit(mod)
return out
def has_variant(text, self_name):