CanadaHonk / porffor

A from-scratch experimental AOT JS engine, written in JS
https://porffor.dev
MIT License
2.24k stars 55 forks source link

Add "chunk" fast paths for string read/write #144

Open CanadaHonk opened 1 month ago

CanadaHonk commented 1 month ago

From a memory perspective, individual characters do not matter. We can read/write u32 chunks while length > 4 instead of individual u8s/u16s.

Places where this could be done:

BobVarioa commented 1 month ago

Also, while maybe it's obvious, it's probably better to do this with two loops in order to avoid extra branching, i.e.

const str: u32[] = a;
const str2: u32[] = b;
const end = str.length - str.length % 4;
for (let i = 0; i != end; i += 4) {
  if (str[i] !== str2[i]) break
}
// etc. read the last characters in a traditional way

Also, since our strings can read off the end anyways, it might be better to "pad" the ends of strings so they are aligned to u32s, skipping the modulo and second loop in the above example