microsoft / devicescript

TypeScript for Tiny IoT Devices (ESP32, RP2040, ...)
https://microsoft.github.io/devicescript/
MIT License
3.28k stars 119 forks source link

code compression ideas #336

Open mmoskal opened 1 year ago

mmoskal commented 1 year ago

These should make the generated bytecode a little smaller.

Approximate size reductions are give here for "allcompile.ts" - 68k binary with 38k of function code (and 6k of fonts).

mmoskal commented 1 year ago

Utility script so it doesn't get lost:

devs build -F allFunctions -F allPrototypes devs/run-tests/allcompile.ts
devs disasm -d > all.dasm
node analyze-bytecode.ts all.dasm
import { readFileSync } from "node:fs"

const OP_PRINT_FMTS = [
    // from bytecode.ts
]

const seqs = {
    1: {},
    2: {},
    3: {},
}

let lines = 0
let size = 0

for (const line of readFileSync(process.argv[2], "utf8").split(/\n/)) {
    const m = /^\s*\d+:\s*.*\/\/ ([0-9a-f]+)/.exec(line)
    if (m) {
        lines++
        const buf = Buffer.from(m[1], "hex")
        size += buf.length
        for (let i = 0; i < buf.length; i++) {
            for (const lens of Object.keys(seqs)) {
                const len = +lens
                const seq = buf.slice(i, i + len)
                if (seq.length == len) {
                    const s = seq.toString("hex")
                    if (!seqs[lens][s]) seqs[lens][s] = 0
                    seqs[lens][s]++
                }
            }
        }
    }
}

function show(map) {
    for (const [k, v] of Object.entries(map)
        .sort((a, b) => a[1] - b[1])
        .slice(-100)) {
        const buf = Buffer.from(k, "hex")
        const op = OP_PRINT_FMTS[buf[0]]
        console.log(k, v, op)
    }
}

show(seqs[2])
console.log({ lines, size })