Skip to content

Commit 31160ff

Browse files
committed
compiler: use 1/4th page size by default
most things don't need a full page and can always specify via pref or bump later if it turns out to be an issue but I doubt it. this makes richards ~25% faster, surpassing native quickjs! test262: 56.84% | 🧪 50259 | 🤠 28567 (+2) | ❌ 7096 (+5) | 💀 13660 (-7) | 🏗️ 31 | 💥 177 | ⏰ 11 | 📝 717 benchmark: bench,porffor,node_jitless,quickjs richards,310,220,400
1 parent 54992cb commit 31160ff

10 files changed

+18
-19
lines changed

compiler/2c.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { read_ieee754_binary64, read_signedLEB128, read_unsignedLEB128 } from './encoding.js';
2-
import { Blocktype, Opcodes, Valtype } from './wasmSpec.js';
2+
import { Blocktype, Opcodes, Valtype, PageSize } from './wasmSpec.js';
33
import { operatorOpcode } from './expression.js';
44
import { log } from './log.js';
55
import './prefs.js';
@@ -218,7 +218,7 @@ export default ({ funcs, globals, tags, data, exceptions, pages }) => {
218218
if (pages.size > 0) {
219219
includes.set('stdlib.h', true);
220220
prepend.set('_memory', `char* _memory; u32 _memoryPages = ${pages.size};\n`);
221-
prependMain.set('_initMemory', `_memory = malloc(_memoryPages * ${pageSize});\n`);
221+
prependMain.set('_initMemory', `_memory = malloc(_memoryPages * ${PageSize});\n`);
222222
if (Prefs['2cMemcpy']) includes.set('string.h', true);
223223
}
224224

compiler/allocator.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { PageSize } from './wasmSpec.js';
21
import './prefs.js';
32

43
const pagePtr = ind => {
54
if (ind === 0) return 16;
6-
return ind * PageSize;
5+
return ind * pageSize;
76
};
87

98
export const nameToReason = (scope, name) => {
@@ -37,7 +36,7 @@ export const allocBytes = ({ scope, pages }, reason, bytes) => {
3736
return allocs.get(reason);
3837
}
3938

40-
let bin = bins.find(x => (PageSize - x.used) >= bytes);
39+
let bin = bins.find(x => (pageSize - x.used) >= bytes);
4140
if (!bin) {
4241
// new bin
4342
const page = pages.size;

compiler/builtins.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as PrecompiledBuiltins from './builtins_precompiled.js';
22
import ObjectBuiltins from './builtins_objects.js';
3-
import { Blocktype, Opcodes, Valtype, ValtypeSize } from './wasmSpec.js';
3+
import { PageSize, Blocktype, Opcodes, Valtype } from './wasmSpec.js';
44
import { TYPES, TYPE_NAMES } from './types.js';
55
import { number, unsignedLEB128 } from './encoding.js';
66
import './prefs.js';
@@ -778,7 +778,7 @@ export const BuiltinFuncs = function() {
778778
wasm: [
779779
number(1, Valtype.i32),
780780
[ Opcodes.memory_grow, 0 ],
781-
number(pageSize, Valtype.i32),
781+
number(PageSize, Valtype.i32),
782782
[ Opcodes.i32_mul ]
783783
]
784784
},
@@ -787,23 +787,23 @@ export const BuiltinFuncs = function() {
787787
locals: [],
788788
globals: [ Valtype.i32, Valtype.i32 ],
789789
globalNames: [ 'chunkPtr', 'chunkOffset' ],
790-
globalInits: [ 0, 100 * pageSize ],
790+
globalInits: [ 0, 100 * PageSize ],
791791
returns: [ Valtype.i32 ],
792792
returnType: TYPES.number,
793793
wasm: [
794794
// if chunkOffset >= chunks:
795795
[ Opcodes.global_get, 1 ],
796-
number(pageSize * (Prefs.allocatorChunks ?? 16), Valtype.i32),
796+
number(PageSize * (Prefs.allocatorChunks ?? 16), Valtype.i32),
797797
[ Opcodes.i32_ge_s ],
798798
[ Opcodes.if, Valtype.i32 ],
799799
// chunkOffset = 1 page
800-
number(1 * pageSize, Valtype.i32),
800+
number(pageSize, Valtype.i32),
801801
[ Opcodes.global_set, 1 ],
802802

803803
// return chunkPtr = allocated
804804
number(Prefs.allocatorChunks ?? 16, Valtype.i32),
805805
[ Opcodes.memory_grow, 0 ],
806-
number(pageSize, Valtype.i32),
806+
number(PageSize, Valtype.i32),
807807
[ Opcodes.i32_mul ],
808808
[ Opcodes.global_set, 0 ],
809809
[ Opcodes.global_get, 0 ],

compiler/builtins_objects.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Blocktype, Opcodes, PageSize, Valtype } from './wasmSpec.js';
1+
import { Blocktype, Opcodes, Valtype } from './wasmSpec.js';
22
import { TYPES } from './types.js';
33
import { number } from './encoding.js';
44

compiler/codegen.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3545,7 +3545,7 @@ const coctcOffset = prop => {
35453545

35463546
let offset = coctc.get(prop);
35473547
if (offset == null) {
3548-
offset = (coctc.lastOffset ?? 60000) - 9;
3548+
offset = (coctc.lastOffset ?? Prefs.coctcOffset ?? 16300) - 9;
35493549
if (offset < 0) return 0;
35503550

35513551
coctc.lastOffset = offset;

compiler/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ export default (code, module = undefined) => {
7373
globalThis.valtype = Prefs.valtype ?? 'f64';
7474
globalThis.valtypeBinary = Valtype[valtype];
7575

76-
globalThis.pageSize = (parseInt(Prefs.valtype) * 1024) || PageSize;
76+
// use smaller page sizes internally (65536 / 4 = 16384)
77+
globalThis.pageSize = Prefs.pageSize ?? (PageSize / 4);
7778

7879
// change some prefs by default for c/native
7980
if (target !== 'wasm') {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "porffor",
33
"description": "An ahead-of-time JavaScript compiler",
4-
"version": "0.55.24",
4+
"version": "0.55.25",
55
"author": "Oliver Medhurst <[email protected]>",
66
"license": "MIT",
77
"scripts": {},

runner/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22
import fs from 'node:fs';
3-
globalThis.version = '0.55.24';
3+
globalThis.version = '0.55.25';
44

55
// deno compat
66
if (typeof process === 'undefined' && typeof Deno !== 'undefined') {

runner/repl.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ console.log(`Welcome to \x1B[1m\x1B[35mPorffor\x1B[0m \x1B[2m(${globalThis.versi
3434
console.log();
3535

3636
let lastMemory, lastPages;
37-
const PageSize = 65536;
3837
const memoryToString = mem => {
3938
let out = '';
40-
const wasmPages = mem.buffer.byteLength / PageSize;
39+
const wasmPages = mem.buffer.byteLength / 65536;
4140

4241
out += `\x1B[1mallocated ${mem.buffer.byteLength / 1024}KiB\x1B[0m (using ${wasmPages} Wasm page${wasmPages === 1 ? '' : 's'})\n\n`;
4342

test262/history.json

+1-1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)