treeShaking

This commit is contained in:
Andrew Morris
2023-03-29 16:06:08 +11:00
parent 93ce39e8b1
commit 2e590a50c3
3 changed files with 192 additions and 0 deletions

View File

@@ -10,6 +10,7 @@ export const orderedFiles = [
'/tutorial/strings.ts',
'/tutorial/binaryTree.ts',
'/tutorial/specialFunctions.ts',
'/tutorial/treeShaking.ts',
];
export const defaultFiles: Record<string, string | nil> = {

View File

@@ -0,0 +1,159 @@
export function factorize(n: number): number[] {
let factors: number[] = [];
let p = 2;
while (true) {
while (n % p === 0) {
factors.push(p);
n /= p;
}
if (n === 1) {
return factors;
}
p = nextOddPrime(p);
if (p * p > n) {
factors.push(n);
return factors;
}
}
}
export function factorizeAsPowers(n: number): [number, number][] {
const factors = factorize(n);
if (factors.length === 0) {
return [];
}
const result: [number, number][] = [];
let currentFactor = factors[0];
let currentPower = 1;
for (let i = 1; i < factors.length; i++) {
const factor = factors[i];
if (factor === currentFactor) {
currentPower += 1;
} else {
result.push([currentFactor, currentPower]);
currentFactor = factor;
currentPower = 1;
}
}
result.push([currentFactor, currentPower]);
return result;
}
export function nextOddPrime(n: number): number {
n += 1 + (n % 2); // Next odd number
while (!isOddPrime(n)) {
n += 2;
}
return n;
}
export function isOddPrime(n: number): boolean {
let i = 3;
while (i * i <= n) {
if (n % i === 0) {
return false;
}
i += 2;
}
return true;
}
export class PrimeGenerator {
pcg: PrimeCandidatesGenerator;
constructor() {
this.pcg = new PrimeCandidatesGenerator();
}
next() {
while (true) {
const candidate = this.pcg.next();
if (isPrime(candidate)) {
return candidate;
}
}
}
}
export function isPrime(n: number) {
let pcg = new PrimeCandidatesGenerator();
while (true) {
const pc = pcg.next();
if (pc * pc > n) {
return true;
}
if (n % pc === 0) {
return false;
}
}
}
export class PrimeCandidatesGenerator {
gen: Gen235 | GenMod30;
constructor() {
this.gen = new Gen235();
}
next() {
let c = this.gen.next();
if (c !== undefined) {
return c;
}
this.gen = new GenMod30();
return this.gen.next();
}
}
class Gen235 {
nums: number[];
i: number;
constructor() {
this.nums = [2, 3, 5];
this.i = 0;
}
next() {
return this.nums[this.i++];
}
}
class GenMod30 {
nums: number[];
i: number;
constructor() {
this.nums = [1, 7, 11, 13, 17, 19, 23, 29];
this.i = 1;
}
next() {
const rem = this.i % 8;
const rounds = (this.i - rem) / 8;
this.i++;
return 30 * rounds + this.nums[rem];
}
}

View File

@@ -0,0 +1,32 @@
// ValueScript comes with built-in bundling and tree-shaking.
//
// In this example, we import `factorize` from the primes module. In the
// assembly, you can see `factorize` has been included, as well as the
// `nextOddPrime` and `isOddPrime` functions it depends on.
//
// However, the primes module also defines:
// - `factorizeAsPowers`
// - `PrimeGenerator`
// - `PrimeCandidatesGenerator`
// - `Gen235`
// - `GenMod30`
//
// These definitions are not included, because the definitions exported by this
// module do not need them. Omitting those unused definitions reduces the
// bytecode for this module from 1,091 to 295 bytes.
import { factorize } from "../lib/primes";
// It's not just the default export that matters. If you uncomment this line,
// the assembly will also include `PrimeCandidatesGenerator`, even though it's
// not used anywhere else.
// export { PrimeCandidatesGenerator } from "../lib/primes";
export default function main() {
return factorize(18); // [2, 3, 3], because 2 * 3 * 3 = 18
}
// These function are also not in the assembly, because none of our exports use
// them.
function foo() { return bar(); }
function bar() {}