mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
Merge pull request #944 from chrox/lazy_loading
refactoring: lazy loading of unused modules
This commit is contained in:
4
.gitmodules
vendored
4
.gitmodules
vendored
@@ -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
|
||||
|
||||
29
Makefile
29
Makefile
@@ -14,8 +14,13 @@ export PATH:=$(CURDIR)/$(KOR_BASE)/toolchain/android-toolchain/bin:$(PATH)
|
||||
MACHINE?=$(shell PATH=$(PATH) $(CC) -dumpmachine 2>/dev/null)
|
||||
INSTALL_DIR=koreader-$(MACHINE)
|
||||
|
||||
ANDROID_DIR=android
|
||||
# platform directories
|
||||
PLATFORM_DIR=platform
|
||||
KINDLE_DIR=$(PLATFORM_DIR)/kindle
|
||||
KOBO_DIR=$(PLATFORM_DIR)/kobo
|
||||
ANDROID_DIR=$(PLATFORM_DIR)/android
|
||||
ANDROID_LAUNCHER_DIR:=$(ANDROID_DIR)/luajit-launcher
|
||||
WIN32_DIR=$(PLATFORM_DIR)/win32
|
||||
|
||||
# files to link from main directory
|
||||
INSTALL_FILES=reader.lua frontend resources defaults.lua l10n \
|
||||
@@ -54,7 +59,7 @@ ifdef ANDROID
|
||||
endif
|
||||
ifdef WIN32
|
||||
# install runtime libraries for win32
|
||||
cd $(INSTALL_DIR)/koreader && cp ../../windows/*.dll .
|
||||
cd $(INSTALL_DIR)/koreader && cp ../../$(WIN32_DIR)/*.dll .
|
||||
endif
|
||||
# install plugins
|
||||
cp -r plugins/* $(INSTALL_DIR)/koreader/plugins/
|
||||
@@ -102,11 +107,11 @@ kindleupdate: all
|
||||
# remove old package if any
|
||||
rm -f koreader-kindle-$(MACHINE)-$(VERSION).zip
|
||||
# Kindle launching scripts
|
||||
ln -sf ../kindle/extensions $(INSTALL_DIR)/
|
||||
ln -sf ../kindle/launchpad $(INSTALL_DIR)/
|
||||
ln -sf ../../kindle/koreader.sh $(INSTALL_DIR)/koreader
|
||||
ln -sf ../../kindle/libkohelper.sh $(INSTALL_DIR)/koreader
|
||||
ln -sf ../../kindle/kotar_cpoint $(INSTALL_DIR)/koreader
|
||||
ln -sf ../$(KINDLE_DIR)/extensions $(INSTALL_DIR)/
|
||||
ln -sf ../$(KINDLE_DIR)/launchpad $(INSTALL_DIR)/
|
||||
ln -sf ../../$(KINDLE_DIR)/koreader.sh $(INSTALL_DIR)/koreader
|
||||
ln -sf ../../$(KINDLE_DIR)/libkohelper.sh $(INSTALL_DIR)/koreader
|
||||
ln -sf ../../$(KINDLE_DIR)/kotar_cpoint $(INSTALL_DIR)/koreader
|
||||
# create new package
|
||||
# Don't bundle launchpad on touch devices..
|
||||
ifeq ($(TARGET), kindle-legacy)
|
||||
@@ -137,13 +142,13 @@ koboupdate: all
|
||||
rm -f koreader-kobo-$(MACHINE)-$(VERSION).zip
|
||||
# Kobo launching scripts
|
||||
mkdir -p $(INSTALL_DIR)/kobo/mnt/onboard/.kobo
|
||||
ln -sf ../../../../../kobo/fmon $(INSTALL_DIR)/kobo/mnt/onboard/.kobo/
|
||||
ln -sf ../../../../../$(KOBO_DIR)/fmon $(INSTALL_DIR)/kobo/mnt/onboard/.kobo/
|
||||
cd $(INSTALL_DIR)/kobo && tar -czhf ../KoboRoot.tgz mnt
|
||||
cp resources/koreader.png $(INSTALL_DIR)/koreader.png
|
||||
cp kobo/fmon/README.txt $(INSTALL_DIR)/README_kobo.txt
|
||||
cp kobo/koreader.sh $(INSTALL_DIR)/koreader
|
||||
cp kobo/suspend.sh $(INSTALL_DIR)/koreader
|
||||
cp kobo/nickel.sh $(INSTALL_DIR)/koreader
|
||||
cp $(KOBO_DIR)/fmon/README.txt $(INSTALL_DIR)/README_kobo.txt
|
||||
cp $(KOBO_DIR)/koreader.sh $(INSTALL_DIR)/koreader
|
||||
cp $(KOBO_DIR)/suspend.sh $(INSTALL_DIR)/koreader
|
||||
cp $(KOBO_DIR)/nickel.sh $(INSTALL_DIR)/koreader
|
||||
# create new package
|
||||
cd $(INSTALL_DIR) && \
|
||||
zip -9 -r \
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
local InputContainer = require("ui/widget/container/inputcontainer")
|
||||
local CenterContainer = require("ui/widget/container/centercontainer")
|
||||
local ButtonDialog = require("ui/widget/buttondialog")
|
||||
local ReaderUI = require("apps/reader/readerui")
|
||||
local lfs = require("libs/libkoreader-lfs")
|
||||
local UIManager = require("ui/uimanager")
|
||||
local DocSettings = require("docsettings")
|
||||
@@ -79,6 +78,7 @@ function FileManagerHistory:addToMainMenu(tab_item_table)
|
||||
end
|
||||
|
||||
function FileManagerHistory:updateItemTable()
|
||||
local ReaderUI = require("apps/reader/readerui")
|
||||
self.hist = {}
|
||||
|
||||
for f in lfs.dir(history_dir) do
|
||||
|
||||
@@ -3,7 +3,6 @@ local InputContainer = require("ui/widget/container/inputcontainer")
|
||||
local DocumentRegistry = require("document/documentregistry")
|
||||
local InputDialog = require("ui/widget/inputdialog")
|
||||
local InfoMessage = require("ui/widget/infomessage")
|
||||
local ReaderUI = require("apps/reader/readerui")
|
||||
local lfs = require("libs/libkoreader-lfs")
|
||||
local UIManager = require("ui/uimanager")
|
||||
local Menu = require("ui/widget/menu")
|
||||
@@ -498,6 +497,7 @@ function Search:onMenuHold(item)
|
||||
end
|
||||
|
||||
function Search:showresults()
|
||||
local ReaderUI = require("apps/reader/readerui")
|
||||
local menu_container = CenterContainer:new{
|
||||
dimen = Screen:getSize(),
|
||||
}
|
||||
@@ -544,6 +544,7 @@ function Search:showresults()
|
||||
end
|
||||
|
||||
function Search:browse(option,run,chosen)
|
||||
local ReaderUI = require("apps/reader/readerui")
|
||||
local restart_me = false
|
||||
local menu_container = CenterContainer:new{
|
||||
dimen = Screen:getSize(),
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
local AndroidPowerd = require("ui/device/androidpowerd")
|
||||
local KindlePowerD = require("ui/device/kindlepowerd")
|
||||
local isAndroid, android = pcall(require, "android")
|
||||
local KoboPowerD = require("ui/device/kobopowerd")
|
||||
local BasePowerD = require("ui/device/basepowerd")
|
||||
local lfs = require("libs/libkoreader-lfs")
|
||||
local Screen = require("ui/device/screen")
|
||||
local util = require("ffi/util")
|
||||
@@ -288,12 +284,16 @@ function Device:getPowerDevice()
|
||||
else
|
||||
local model = self:getModel()
|
||||
if model == "KindleTouch" or model == "KindlePaperWhite" or model == "KindlePaperWhite2" then
|
||||
local KindlePowerD = require("ui/device/kindlepowerd")
|
||||
self.powerd = KindlePowerD:new{model = model}
|
||||
elseif self:isKobo() then
|
||||
local KoboPowerD = require("ui/device/kobopowerd")
|
||||
self.powerd = KoboPowerD:new()
|
||||
elseif self.isAndroid then
|
||||
local AndroidPowerd = require("ui/device/androidpowerd")
|
||||
self.powerd = AndroidPowerd:new()
|
||||
else -- emulated FrontLight
|
||||
local BasePowerD = require("ui/device/basepowerd")
|
||||
self.powerd = BasePowerD:new()
|
||||
end
|
||||
end
|
||||
@@ -322,4 +322,13 @@ function Device:isSpecialOffers()
|
||||
return self.is_special_offers
|
||||
end
|
||||
|
||||
-- FIXME: this is a dirty hack, normally we don't need to get power device this early,
|
||||
-- but Kobo devices somehow may need to init the frontlight module at startup?
|
||||
-- because `kobolight = require("ffi/kobolight")` used to be in the `koreader-base` script
|
||||
-- and run as the first line of koreader script no matter which device you are running on,
|
||||
-- which is utterly ugly. So I refactored it into the `init` method of `kobopowerd` and
|
||||
-- `kobolight` will be init here. It's pretty safe to comment this line for non-kobo devices
|
||||
-- so if kobo users find this line is useless, please don't hesitate to get rid of it.
|
||||
local dummy_powerd = Device:getPowerDevice()
|
||||
|
||||
return Device
|
||||
|
||||
@@ -5,7 +5,7 @@ local KoboPowerD = BasePowerD:new{
|
||||
flIntensity = 20,
|
||||
restore_settings = true,
|
||||
fl = nil,
|
||||
|
||||
|
||||
batt_capacity_file = "/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/capacity",
|
||||
is_charging_file = "/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/charge_now",
|
||||
battCapacity = nil,
|
||||
@@ -13,6 +13,7 @@ local KoboPowerD = BasePowerD:new{
|
||||
}
|
||||
|
||||
function KoboPowerD:init()
|
||||
local kobolight = require("ffi/kobolight")
|
||||
local ok, light = pcall(kobolight.open)
|
||||
if ok then self.fl = light end
|
||||
end
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#!./koreader-base
|
||||
#!./luajit
|
||||
|
||||
require "defaults"
|
||||
pcall(dofile, "defaults.persistent.lua")
|
||||
@@ -100,6 +100,7 @@ local function showusage()
|
||||
return
|
||||
end
|
||||
|
||||
local ARGV = arg
|
||||
local argidx = 1
|
||||
while argidx <= #ARGV do
|
||||
local arg = ARGV[argidx]
|
||||
|
||||
47
utils/trace_require.lua
Normal file
47
utils/trace_require.lua
Normal file
@@ -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)
|
||||
Reference in New Issue
Block a user