From ee9991adbd13a5e82b217cecbd6f2de8e1637c66 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Fri, 20 Jul 2012 21:57:34 +0000 Subject: [PATCH] Lunatic: more bitop profiling; make undeclared-var reads forbidden in modules git-svn-id: https://svn.eduke32.com/eduke32@2839 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/source/lunatic/bittest.lua | 50 +++++++++++++++++++++- polymer/eduke32/source/lunatic/defs.ilua | 16 +++++-- 2 files changed, 62 insertions(+), 4 deletions(-) mode change 100644 => 100755 polymer/eduke32/source/lunatic/bittest.lua diff --git a/polymer/eduke32/source/lunatic/bittest.lua b/polymer/eduke32/source/lunatic/bittest.lua old mode 100644 new mode 100755 index 7efe1e7f6..64de2317d --- a/polymer/eduke32/source/lunatic/bittest.lua +++ b/polymer/eduke32/source/lunatic/bittest.lua @@ -1,3 +1,4 @@ +#!/usr/bin/env luajit local string = require "string" @@ -24,16 +25,62 @@ end local band, bxor = bit.band, bit.bxor local rshift, rol = bit.rshift, bit.rol -local m = 1000000 +local m = string.dump and arg[1] or 1e7 function sieve() local count = 0 local p = {} + if (string.dump) then + -- stand-alone using unchecked int32_t array instead of table: + -- on x86_64 approx. 100 vs. 160 ms for m = 1e7 + -- (enabling bound checking makes it be around 170 ms) + local ffi = require "ffi" + local pp = ffi.new("int32_t [?]", (m+31)/32 + 1) + + p = pp +--[[ + local mt = { + __index = function(tab,idx) + if (idx >= 0 and idx <= (m+31)/32) then + return pp[idx] + end + end, + + __newindex = function(tab,idx,val) + if (idx >= 0 and idx <= (m+31)/32) then + pp[idx] = val + end + end, + } + + p = setmetatable({}, mt) +--]] + end + for i=0,(m+31)/32 do p[i] = -1 end local t = getticks() + -- See http://luajit.org/ext_ffi_tutorial.html, + -- "To Cache or Not to Cache" + -- bit. - qualified version: with m=1e7 ond x86_64: + -- 165 ms embedded, 100 ms stand-alone +---[[ + for i=2,m do + if bit.band(bit.rshift(p[bit.rshift(i, 5)], i), 1) ~= 0 then + count = count + 1 + for j=i+i,m,i do + local jx = bit.rshift(j, 5) + p[jx] = bit.band(p[jx], bit.rol(-2, j)) + end + end + end +--]] + + -- local var version: with m=1e7 on x86_64: + -- 205 ms embedded, 90 ms stand-alone +--[[ for i=2,m do if band(rshift(p[rshift(i, 5)], i), 1) ~= 0 then count = count + 1 @@ -43,6 +90,7 @@ function sieve() end end end +--]] print(string.format("Found %d primes up to %d (%.02f ms)", count, m, getticks()-t)) diff --git a/polymer/eduke32/source/lunatic/defs.ilua b/polymer/eduke32/source/lunatic/defs.ilua index bf423c8c5..b1beb6a08 100644 --- a/polymer/eduke32/source/lunatic/defs.ilua +++ b/polymer/eduke32/source/lunatic/defs.ilua @@ -626,11 +626,17 @@ local G_ = {} -- our soon-to-be global environment -- this table, since user code could later do pairs=nil. local oG = _G +local module_mt = { + __index = function (_, n) + error("attempt to read undeclared variable '"..n.."'", 2) + end, +} + -- replacement for "module" local function our_module(modname) check_valid_modname(modname) - local M = {} + local M = oG.setmetatable({}, module_mt) package_loaded[modname] = M -- change the environment of the function which called us: oG.setfenv(2, M) @@ -911,11 +917,15 @@ local gamevarNames = {} -- based on 14.2 of PiL function gamevar(name, initval) -- aka 'declare' if (type(name) ~= "string") then - error("First argument to 'gamevar' must be a string", 2); + error("First argument to 'gamevar' must be a string", 2) end if (not string.match(name, "^[%a_][%w_]*$")) then - error("First argument to 'gamevar' must be a valid identifier", 2); + error("First argument to 'gamevar' must be a valid identifier", 2) + end + + if (string.match(name, "^_")) then + error("Identifier names starting with an underscore are reserved", 2) end if (gamevarNames[name]) then