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
This commit is contained in:
helixhorned 2012-07-20 21:57:34 +00:00
parent 1ffab1bf1a
commit ee9991adbd
2 changed files with 62 additions and 4 deletions

50
polymer/eduke32/source/lunatic/bittest.lua Normal file → Executable file
View file

@ -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))

View file

@ -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