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