diff options
author | Steven Barth <steven@midlink.org> | 2008-06-01 12:12:18 +0000 |
---|---|---|
committer | Steven Barth <steven@midlink.org> | 2008-06-01 12:12:18 +0000 |
commit | b454395a8da4013aff2ecd64cd7eafc01fc6a5a2 (patch) | |
tree | 0071604ca65f1e520ee2f56b6cf6343bc5297df9 /libs | |
parent | 1da5feb9f720fd48a886aad09df91bd8cc9df4c8 (diff) |
* Performance optimizations
* libs/core: Added bytecode stripping function to luci.util
* libs/core: Added smart indexcache that automatically updates cached index-files on change
* libs/web: Enabled template caching support
* Core Translation part 4
Diffstat (limited to 'libs')
-rw-r--r-- | libs/core/luasrc/fs.lua | 1 | ||||
-rw-r--r-- | libs/core/luasrc/util.lua | 72 | ||||
-rw-r--r-- | libs/web/luasrc/config.lua | 11 | ||||
-rw-r--r-- | libs/web/luasrc/dispatcher.lua | 40 | ||||
-rw-r--r-- | libs/web/luasrc/i18n.lua | 6 | ||||
-rw-r--r-- | libs/web/luasrc/template.lua | 40 | ||||
-rw-r--r-- | libs/web/root/etc/config/luci | 4 |
7 files changed, 126 insertions, 48 deletions
diff --git a/libs/core/luasrc/fs.lua b/libs/core/luasrc/fs.lua index 5c1f2a051..35b8289af 100644 --- a/libs/core/luasrc/fs.lua +++ b/libs/core/luasrc/fs.lua @@ -27,6 +27,7 @@ limitations under the License. module("luci.fs", package.seeall) require("posix") +posix.umask("rwx------") -- Glob glob = posix.glob diff --git a/libs/core/luasrc/util.lua b/libs/core/luasrc/util.lua index 0559fff6f..0a30b163a 100644 --- a/libs/core/luasrc/util.lua +++ b/libs/core/luasrc/util.lua @@ -96,6 +96,13 @@ function contains(table, value) end +-- Dumps and strips a Lua-Function +function dump(f) + local d = string.dump(f) + return d and strip_bytecode(d) +end + + -- Dumps a table to stdout (useful for testing and debugging) function dumptable(t, i) i = i or 0 @@ -181,12 +188,77 @@ function split(str, pat, max, regex) return t end + +-- Bytecode stripping function by Peter Cawley from http://lua-users.org/lists/lua-l/2008-02/msg01158.html +function strip_bytecode(dump) + local version, format, endian, int, size, ins, num = dump:byte(5, 11) + local subint + if endian == 1 then + subint = function(dump, i, l) + local val = 0 + for n = l, 1, -1 do + val = val * 256 + dump:byte(i + n - 1) + end + return val, i + l + end + else + subint = function(dump, i, l) + local val = 0 + for n = 1, l, 1 do + val = val * 256 + dump:byte(i + n - 1) + end + return val, i + l + end + end + + local strip_function + strip_function = function(dump) + local count, offset = subint(dump, 1, size) + local stripped, dirty = string.rep("\0", size), offset + count + offset = offset + count + int * 2 + 4 + offset = offset + int + subint(dump, offset, int) * ins + count, offset = subint(dump, offset, int) + for n = 1, count do + local t + t, offset = subint(dump, offset, 1) + if t == 1 then + offset = offset + 1 + elseif t == 4 then + offset = offset + size + subint(dump, offset, size) + elseif t == 3 then + offset = offset + num + end + end + count, offset = subint(dump, offset, int) + stripped = stripped .. dump:sub(dirty, offset - 1) + for n = 1, count do + local proto, off = strip_function(dump:sub(offset, -1)) + stripped, offset = stripped .. proto, offset + off - 1 + end + offset = offset + subint(dump, offset, int) * int + int + count, offset = subint(dump, offset, int) + for n = 1, count do + offset = offset + subint(dump, offset, size) + size + int * 2 + end + count, offset = subint(dump, offset, int) + for n = 1, count do + offset = offset + subint(dump, offset, size) + size + end + stripped = stripped .. string.rep("\0", int * 3) + return stripped, offset + end + + return dump:sub(1,12) .. strip_function(dump:sub(13,-1)) +end + + -- Removes whitespace from beginning and end of a string function trim(str) local s = str:gsub("^%s*(.-)%s*$", "%1") return s end + -- Updates given table with new values function update(t, updates) for k, v in pairs(updates) do diff --git a/libs/web/luasrc/config.lua b/libs/web/luasrc/config.lua index 854b12814..4eb8e46e4 100644 --- a/libs/web/luasrc/config.lua +++ b/libs/web/luasrc/config.lua @@ -25,10 +25,9 @@ limitations under the License. ]]-- -module("luci.config", package.seeall) -require("luci.model.uci") -require("luci.util") -require("luci.sys") +local uci = require("luci.model.uci") +local util = require("luci.util") +module("luci.config") -- Warning! This is only for fallback and compatibility purporses! -- main = {} @@ -42,7 +41,7 @@ main.lang = "de" -- Now overwrite with UCI values -local ucidata = luci.model.uci.sections("luci") +local ucidata = uci.sections("luci") if ucidata then - luci.util.update(luci.config, ucidata) + util.update(_M, ucidata) end
\ No newline at end of file diff --git a/libs/web/luasrc/dispatcher.lua b/libs/web/luasrc/dispatcher.lua index 8d1e493fb..c7e1ed6fe 100644 --- a/libs/web/luasrc/dispatcher.lua +++ b/libs/web/luasrc/dispatcher.lua @@ -177,7 +177,7 @@ end -- Calls the index function of all available controllers function createindex_plain(path, suffix) - local cachetime = nil + local cache = nil local controllers = luci.util.combine( luci.fs.glob(path .. "*" .. suffix) or {}, @@ -185,32 +185,38 @@ function createindex_plain(path, suffix) ) if indexcache then - cachetime = luci.fs.mtime(indexcache) + cache = luci.fs.mtime(indexcache) - if not cachetime then + if not cache then luci.fs.mkdir(indexcache) luci.fs.chmod(indexcache, "a=,u=rwx") + cache = luci.fs.mtime(indexcache) end end - if not cachetime then - for i,c in ipairs(controllers) do - c = "luci.controller." .. c:sub(#path+1, #c-#suffix):gsub("/", ".") - stat, mod = pcall(require, c) + for i,c in ipairs(controllers) do + local module = "luci.controller." .. c:sub(#path+1, #c-#suffix):gsub("/", ".") + local cachefile = indexcache .. "/" .. module + local stime + local ctime + + if cache then + stime = luci.fs.mtime(c) or 0 + ctime = luci.fs.mtime(cachefile) or 0 + end + + if not cache or stime > ctime then + stat, mod = pcall(require, module) if stat and mod and type(mod.index) == "function" then - index[c] = mod.index + index[module] = mod.index - if indexcache then - luci.fs.writefile(indexcache .. "/" .. c, string.dump(mod.index)) + if cache then + luci.fs.writefile(cachefile, luci.util.dump(mod.index)) end end - end - else - for i,c in ipairs(luci.fs.dir(indexcache)) do - if c:sub(1) ~= "." then - index[c] = loadfile(indexcache .. "/" .. c) - end + else + index[module] = loadfile(cachefile) end end end @@ -226,7 +232,7 @@ function createtree() -- Load default translation luci.i18n.loadc("default") - local scope = _G + local scope = luci.util.clone(_G) for k,v in pairs(_M) do if type(v) == "function" then scope[k] = v diff --git a/libs/web/luasrc/i18n.lua b/libs/web/luasrc/i18n.lua index 7ace708e9..546429933 100644 --- a/libs/web/luasrc/i18n.lua +++ b/libs/web/luasrc/i18n.lua @@ -39,7 +39,7 @@ end -- Loads a translation and copies its data into the global translation table function load(file, force) if force or not loaded[file] then - local f = loadfile(i18ndir .. file) + local f = loadfile(i18ndir..file..".lua") or loadfile(i18ndir..file) if f then setfenv(f, table) f() @@ -54,8 +54,8 @@ function load(file, force) end -- Same as load but autocompletes the filename with .LANG from config.lang -function loadc(file) - return load(file .. "." .. require("luci.config").main.lang) +function loadc(file, force) + return load(file .. "." .. require("luci.config").main.lang, force) end -- Returns the i18n-value defined by "key" or if there is no such: "default" diff --git a/libs/web/luasrc/template.lua b/libs/web/luasrc/template.lua index 369aa0a30..c672f16bf 100644 --- a/libs/web/luasrc/template.lua +++ b/libs/web/luasrc/template.lua @@ -30,22 +30,17 @@ require("luci.util") require("luci.fs") require("luci.http") -viewdir = luci.sys.libpath() .. "/view/" +luci.config.template = luci.config.template or {} + +viewdir = luci.config.template.viewdir or luci.sys.libpath() .. "/view" +compiledir = luci.config.template.compiledir or luci.sys.libpath() .. "/view" -- Compile modes: -- none: Never compile, only use precompiled data from files -- memory: Always compile, do not save compiled files, ignore precompiled -- file: Compile on demand, save compiled files, update precompiled -compiler_mode = "memory" - - --- This applies to compiler modes "always" and "smart" --- --- Produce compiled lua code rather than lua sourcecode --- WARNING: Increases template size heavily!!! --- This produces the same bytecode as luac but does not have a strip option -compiler_enable_bytecode = false +compiler_mode = luci.config.template.compiler_mode or "memory" -- Define the namespace for template modules @@ -107,12 +102,7 @@ function compile(template) template = template:gsub("<%%"..tostring(k).."%%>", re) end - if compiler_enable_bytecode then - tf = loadstring(template) - template = string.dump(tf) - end - - return template + return loadstring(template) end -- Oldstyle render shortcut @@ -156,8 +146,8 @@ function Template.__init__(self, name) end -- Compile and build - local sourcefile = viewdir .. name .. ".htm" - local compiledfile = viewdir .. name .. ".lua" + local sourcefile = viewdir .. "/" .. name .. ".htm" + local compiledfile = compiledir .. "/" .. name .. ".lua" local err if compiler_mode == "file" then @@ -171,9 +161,15 @@ function Template.__init__(self, name) source, err = luci.fs.readfile(sourcefile) if source then - local compiled = compile(source) - luci.fs.writefile(compiledfile, compiled) - self.template, err = loadstring(compiled) + local compiled, err = compile(source) + + local compiledfile_dir = luci.fs.dirname(compiledfile) + if not luci.fs.mtime(compiledfile_dir) then + luci.fs.mkdir(compiledfile_dir) + end + + luci.fs.writefile(compiledfile, luci.util.dump(compiled)) + self.template = compiled end else self.template, err = loadfile(compiledfile) @@ -186,7 +182,7 @@ function Template.__init__(self, name) local source source, err = luci.fs.readfile(sourcefile) if source then - self.template, err = loadstring(compile(source)) + self.template, err = compile(source) end end diff --git a/libs/web/root/etc/config/luci b/libs/web/root/etc/config/luci index 5fefe5b27..560cff450 100644 --- a/libs/web/root/etc/config/luci +++ b/libs/web/root/etc/config/luci @@ -34,6 +34,10 @@ config event uci_oncommit config internal languages option de "Deutsch" option en "English" + +config internal template + option compiler_mode file + option compiledir "/tmp/.lucitplcache" config internal themes option OpenWRT "/luci-static/openwrt.org" |