mirror of https://github.com/koreader/koreader
Merge pull request #944 from chrox/lazy_loading
refactoring: lazy loading of unused modulespull/947/head
commit
7df2e0fb28
@ -1,6 +1,6 @@
|
||||
[submodule "koreader-base"]
|
||||
path = koreader-base
|
||||
url = git://github.com/koreader/koreader-base.git
|
||||
[submodule "android/luajit-launcher"]
|
||||
path = android/luajit-launcher
|
||||
[submodule "platform/android/luajit-launcher"]
|
||||
path = platform/android/luajit-launcher
|
||||
url = https://github.com/koreader/android-luajit-launcher.git
|
||||
|
@ -0,0 +1,47 @@
|
||||
-- trace package loading flow with require call
|
||||
-- usage: ./luajit -lutils/trace_require reader.lua ../../test
|
||||
|
||||
local math = require("math")
|
||||
local _require = require
|
||||
|
||||
local loaded_modules = {}
|
||||
local highlight = "*"
|
||||
-- timing threshold for highlight annotation
|
||||
local threshold = 0.001
|
||||
-- whether to trace loaded packages
|
||||
local trace_loaded = false
|
||||
local indent = string.rep(" ", 4)
|
||||
|
||||
local level = 0
|
||||
function require(module)
|
||||
level = level + 1
|
||||
local x = os.clock()
|
||||
local info = debug.getinfo(2)
|
||||
-- this is a protected call of require
|
||||
if info.short_src == "[C]" then
|
||||
info = debug.getinfo(3)
|
||||
end
|
||||
local loaded = loaded_modules[module]
|
||||
if not loaded then
|
||||
print(string.format("%s%s:%s => %s",
|
||||
indent:rep(level), info.short_src, info.currentline, module))
|
||||
elseif trace_loaded then
|
||||
print(string.format("%s%s:%s -> %s",
|
||||
indent:rep(level), info.short_src, info.currentline, module))
|
||||
end
|
||||
-- protect require call in case we cannot raise call level when errors happen
|
||||
local ok, loaded_module = pcall(_require, module)
|
||||
if not ok then
|
||||
level = level - 1
|
||||
error(loaded_module)
|
||||
end
|
||||
local elapse = os.clock() - x
|
||||
local annot = highlight:rep(math.ceil(math.log10(elapse/threshold))) or ""
|
||||
if not loaded then
|
||||
print(string.format("%s%s loading time: %.3f",
|
||||
annot .. indent:rep(level):sub(#annot + 1), module, elapse))
|
||||
end
|
||||
loaded_modules[module] = true
|
||||
level = level - 1
|
||||
return loaded_module
|
||||
end
|
@ -1,4 +1,5 @@
|
||||
#!./koreader-base
|
||||
-- widget test utility
|
||||
-- usage: ./luajit util/wtest.lua
|
||||
|
||||
require "defaults"
|
||||
print(package.path)
|
Loading…
Reference in New Issue