Make python-version 2x faster

This commit is contained in:
Koichi Nakamura 2021-01-05 21:06:03 +09:00
parent 4004fd7689
commit 3b272e982f

View file

@ -5,84 +5,91 @@
import sys import sys
import operator import operator
CELL = 4
MEMORY_SIZE = 0x08000000 MEMORY_SIZE = 0x08000000
STACK_SIZE = 0x400 STACK_SIZE = 0x400
RSTACK_SIZE = 0x400 RSTACK_SIZE = 0x400
HERE_CELL = 0 HERE_CELL = 0
LATEST_CELL = CELL LATEST_CELL = 4
memory = bytearray(MEMORY_SIZE) memory = [0]*(MEMORY_SIZE>>2)
sp = MEMORY_SIZE
rp = MEMORY_SIZE - STACK_SIZE sp = MEMORY_SIZE>>2
rp = (MEMORY_SIZE - STACK_SIZE)>>2
ip = 0 ip = 0
np = 0 np = 0
def aligned(n): def aligned(n):
return (n + CELL - 1) & ~(CELL - 1) return (n + 4 - 1) & ~(4 - 1)
def align(): def align():
write(HERE_CELL, aligned(read(HERE_CELL))) write(HERE_CELL, aligned(read(HERE_CELL)))
def read(addr): def read(addr):
return int.from_bytes( return memory[addr>>2]
memory[addr:addr+CELL], 'little', signed=True)
def write(addr, v): def write(addr, v):
memory[addr:addr+CELL] = v.to_bytes(4, 'little', signed=True) memory[addr>>2] = v
def comma(v): def comma(v):
here = read(HERE_CELL) here = read(HERE_CELL)
write(here, v) write(here, v)
write(HERE_CELL, here + CELL) write(HERE_CELL, here + 4)
def read_byte(addr): def read_byte(addr):
return memory[addr] #return memory[addr]
i = addr>>2
m = (addr&0x3)*8
v = memory[i]
return (v >> m) & 0xff
def write_byte(addr, v): def write_byte(addr, c):
memory[addr] = v #memory[addr] = v
i = addr>>2
m = (addr&0x3)*8
v = memory[i]
memory[i] = (v & ~(0xff << m)) | (c&0xff) << m
def comma_byte(v): def comma_byte(v):
here = read(HERE_CELL) here = read(HERE_CELL)
memory[here] = v write_byte(here, v)
write(HERE_CELL, here + 1) write(HERE_CELL, here + 1)
def comma_string(s): def comma_string(s):
n = len(s) for c in s:
here = read(HERE_CELL) comma_byte(ord(c))
memory[here:here+n+1] = bytes(s+'\0', 'ascii') comma_byte(0)
write(HERE_CELL, here+n+1)
def find(c): def find(c):
it = read(LATEST_CELL) it = read(LATEST_CELL)
while it != 0: while it != 0:
n = read_byte(it + CELL) n = read_byte(it + 4)
C = chr(read_byte(it + CELL + 1)) C = chr(read_byte(it + 4 + 1))
if (c == C and n == 1): if (c == C and n == 1):
return it + 2*CELL return it + 2*4
it = read(it) it = read(it)
raise Exception('Unknown word: {}'.format(c)) raise Exception('Unknown word: {}'.format(c))
def push(v): def push(v):
global sp global sp
sp -= CELL sp -= 4
write(sp, v) write(sp, v)
def pop(): def pop():
global sp global sp
v = read(sp) v = read(sp)
sp += CELL sp += 4
return v return v
def rpush(v): def rpush(v):
global rp global rp
rp -= CELL rp -= 4
write(rp, v) write(rp, v)
def rpop(): def rpop():
global rp global rp
v = read(rp) v = read(rp)
rp += CELL rp += 4
return v return v
operators = [] operators = []
@ -102,7 +109,7 @@ def add_operator(name, func):
return funcid return funcid
def next(np): def next(np):
return read(np), np + CELL return read(np), np + 4
def add_simple_operator(name, func): def add_simple_operator(name, func):
def func_(ip, np): def func_(ip, np):
@ -116,7 +123,7 @@ def add_binary_operator(name, op):
push(op(pop(), b)) push(op(pop(), b))
return add_simple_operator(name, func) return add_simple_operator(name, func)
write(HERE_CELL, 2*CELL) write(HERE_CELL, 2*4)
write(LATEST_CELL, 0) write(LATEST_CELL, 0)
# Store command line arguments # Store command line arguments
@ -131,10 +138,10 @@ for addr in argv_addrs:
def docol(ip, np): def docol(ip, np):
rpush(np) rpush(np)
return next(ip + CELL) return next(ip + 4)
DOCOL_ID = add_operator('', docol) DOCOL_ID = add_operator('', docol)
add_simple_operator('Q', lambda: exit(0)) add_simple_operator('Q', lambda: exit(0))
add_simple_operator('C', lambda: push(CELL)) add_simple_operator('C', lambda: push(4))
add_simple_operator('h', lambda: push(HERE_CELL)) add_simple_operator('h', lambda: push(HERE_CELL))
add_simple_operator('l', lambda: push(LATEST_CELL)) add_simple_operator('l', lambda: push(LATEST_CELL))
def key(): def key():
@ -146,7 +153,7 @@ def key():
add_simple_operator('k', key) add_simple_operator('k', key)
add_simple_operator('t', lambda: sys.stdout.write(chr(pop()))) add_simple_operator('t', lambda: sys.stdout.write(chr(pop())))
add_operator('j', lambda ip,np: next(np + read(np))) add_operator('j', lambda ip,np: next(np + read(np)))
add_operator('J', lambda ip,np: next(np + (CELL if pop() else read(np)))) add_operator('J', lambda ip,np: next(np + (4 if pop() else read(np))))
add_simple_operator('f', lambda: push(find(chr(pop())))) add_simple_operator('f', lambda: push(find(chr(pop()))))
add_operator('x', lambda ip,np: (pop(), np)) add_operator('x', lambda ip,np: (pop(), np))
add_simple_operator('@', lambda: push(read(pop()))) add_simple_operator('@', lambda: push(read(pop())))
@ -170,11 +177,11 @@ add_simple_operator('i', lambda: push(DOCOL_ID))
add_operator('e', lambda ip,np: next(rpop())) add_operator('e', lambda ip,np: next(rpop()))
def lit(ip, np): def lit(ip, np):
push(read(np)) push(read(np))
return next(np + CELL) return next(np + 4)
add_operator('L', lit) add_operator('L', lit)
def litstring(ip, np): def litstring(ip, np):
push(np + CELL) push(np + 4)
return next(aligned(np + CELL + read(np))) return next(aligned(np + 4 + read(np)))
add_operator('S', litstring) add_operator('S', litstring)
add_binary_operator('+', operator.add) add_binary_operator('+', operator.add)
add_binary_operator('-', operator.sub) add_binary_operator('-', operator.sub)
@ -196,7 +203,7 @@ comma(find('k'))
comma(find('f')) comma(find('f'))
comma(find('x')) comma(find('x'))
comma(find('j')) comma(find('j'))
comma(-4*CELL) comma(-4*4)
ip, np = next(start) ip, np = next(start)
while True: while True: