Skip to content

Instantly share code, notes, and snippets.

@jridgewell
Created May 6, 2019 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jridgewell/9fec0c929c0acb8cb67ee0e5dd8a645e to your computer and use it in GitHub Desktop.
Save jridgewell/9fec0c929c0acb8cb67ee0e5dd8a645e to your computer and use it in GitHub Desktop.
Demonstrating cross chunk code motion
import {test} from './base.js';
import {inter} from './intermediate.js';
inter();
test('a');
import {test} from './base.js';
import {inter} from './intermediate.js';
inter();
test('b');
const local = 'test';
// I have to include some code to prevent cross method code motion.
export function crc32(str) {
const crcTable = new Array(256);
for (let i = 0; i < 256; i++) {
let c = i;
for (let j = 0; j < 8; j++) {
if (c & 1) {
c = (c >>> 1) ^ 0xEDB88320;
} else {
c = c >>> 1;
}
}
crcTable[i] = c;
}
// Shrink to 32 bits.
let crc = -1 >>> 0;
for (let i = 0; i < str.length; i++) {
const lookupIndex = (crc ^ str[i]) & 0xFF;
crc = (crc >>> 8) ^ crcTable[lookupIndex];
}
return (crc ^ (-1)) >>> 0;
}
export function test(message) {
document.getElementById(local).innerText = crc32(message);
console.log(message);
}
OPTS=(
"--language_in=ES6_STRICT"
"--language_out=ES5"
"--compilation_level=ADVANCED_OPTIMIZATIONS"
"--formatting=pretty_print"
"--js base.js"
"--chunk base:auto"
"--js intermediate.js"
"--chunk intermediate:1:base"
"--js a.js"
"--chunk a:1:intermediate"
"--js b.js"
"--chunk b:1:intermediate"
)
set -ex
java -jar compiler.jar $(echo ${OPTS[*]})
export function inter() {
console.log('inter');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment