koreader/koreader — pull request #944 by chrox (2014-10-03 08:25:35+00:00)
Title: refactoring: lazy loading of unused modules

This will save hundreds of milliseconds for startup to filemanager since `ReaderUI` is not loaded to show the filemanager.

There is also a utility to trace the module loading process and print the loading order and time cost for each module.
``` shell
koreader $ ./luajit -lutils.trace_require ./reader.lua ../../
./reader.lua:3 => defaults
defaults loading time: 0.001
./reader.lua:8 => ffi
ffi loading time: 0.000
./reader.lua:18 => docsettings
frontend/docsettings.lua:1 => libs/libkoreader-lfs
libs/libkoreader-lfs loading time: 0.000
docsettings loading time: 0.000
./reader.lua:19 => gettext
frontend/gettext.lua:1 => android
frontend/gettext.lua:2 => dbg
frontend/dbg.lua:2 => android
dbg loading time: 0.000
gettext loading time: 0.001
./reader.lua:20 => ffi/util
ffi/util.lua:6 => bit
bit loading time: 0.000
ffi/util.lua:51 => ffi/posix_h
ffi/posix_h loading time: 0.000
ffi/util loading time: 0.001
./reader.lua:32 => ui/uimanager
frontend/ui/uimanager.lua:1 => ui/device
frontend/ui/device.lua:1 => android
frontend/ui/device.lua:3 => ui/device/screen
frontend/ui/device/screen.lua:1 => ffi/blitbuffer
* ffi/blitbuffer loading time: 0.002
frontend/ui/device/screen.lua:2 => ffi/framebuffer
[C]:-1 => ffi/framebuffer_SDL2_0
ffi/framebuffer_SDL2_0.lua:4 => ffi/SDL2_0
ffi/SDL2_0.lua:13 => ffi/SDL2_0_h
ffi/SDL2_0_h loading time: 0.000
ffi/SDL2_0.lua:14 => ffi/linux_input_h
ffi/linux_input_h loading time: 0.000
ffi/SDL2_0 loading time: 0.001
* ffi/framebuffer_SDL2_0 loading time: 0.001
* ffi/framebuffer loading time: 0.005
frontend/ui/device/screen.lua:3 => ui/geometry
ui/geometry loading time: 0.000
** ui/device/screen loading time: 0.068
frontend/ui/device.lua:293 => ui/device/androidpowerd
frontend/ui/device/androidpowerd.lua:1 => ui/device/basepowerd
ui/device/basepowerd loading time: 0.000
ui/device/androidpowerd loading time: 0.000
** ui/device loading time: 0.069
frontend/ui/uimanager.lua:2 => ui/screen
ui/screen loading time: 0.000
frontend/ui/uimanager.lua:3 => ui/input
frontend/ui/input.lua:2 => ui/gesturedetector
frontend/ui/gesturedetector.lua:2 => ui/timeval
ui/timeval loading time: 0.000
ffi/util.lua:166 => android
* ui/gesturedetector loading time: 0.001
frontend/ui/input.lua:3 => ui/event
ui/event loading time: 0.000
frontend/ui/input.lua:6 => ffi/input
[C]:-1 => ffi/input_SDL2_0
ffi/input_SDL2_0 loading time: 0.000
ffi/input loading time: 0.000
frontend/ui/input.lua:8 => optmath
optmath loading time: 0.000
* ui/input loading time: 0.002
** ui/uimanager loading time: 0.074
./reader.lua:170 => apps/filemanager/filemanager
frontend/apps/filemanager/filemanager.lua:1 => apps/filemanager/filemanagerhistory
frontend/apps/filemanager/filemanagerhistory.lua:1 => ui/widget/container/inputcontainer
frontend/ui/widget/container/inputcontainer.lua:1 => ui/widget/container/widgetcontainer
frontend/ui/widget/container/widgetcontainer.lua:2 => ui/widget/widget
frontend/ui/widget/widget.lua:1 => ui/widget/eventlistener
ui/widget/eventlistener loading time: 0.000
ui/widget/widget loading time: 0.000
ui/widget/container/widgetcontainer loading time: 0.000
ui/widget/container/inputcontainer loading time: 0.001
frontend/apps/filemanager/filemanagerhistory.lua:2 => ui/widget/container/centercontainer
ui/widget/container/centercontainer loading time: 0.000
frontend/apps/filemanager/filemanagerhistory.lua:3 => ui/widget/buttondialog
frontend/ui/widget/buttondialog.lua:1 => ui/widget/buttontable
frontend/ui/widget/buttontable.lua:1 => ui/widget/verticalgroup
ui/widget/verticalgroup loading time: 0.000
frontend/ui/widget/buttontable.lua:2 => ui/widget/horizontalgroup
ui/widget/horizontalgroup loading time: 0.000
frontend/ui/widget/buttontable.lua:3 => ui/widget/verticalspan
ui/widget/verticalspan loading time: 0.000
frontend/ui/widget/buttontable.lua:4 => ui/widget/linewidget
ui/widget/linewidget loading time: 0.000
frontend/ui/widget/buttontable.lua:5 => ui/widget/button
frontend/ui/widget/button.lua:2 => ui/widget/textwidget
frontend/ui/widget/textwidget.lua:3 => ui/rendertext
frontend/ui/rendertext.lua:1 => ui/font
frontend/ui/font.lua:2 => ffi/freetype
ffi/freetype.lua:9 => ffi/freetype_h
ffi/freetype_h loading time: 0.000
* ffi/freetype loading time: 0.001
* ui/font loading time: 0.002
frontend/ui/rendertext.lua:3 => cache
frontend/cache.lua:4 => MD5
frontend/MD5.lua:11 => memutils
memutils loading time: 0.000
frontend/MD5.lua:12 => stringzutils
stringzutils loading time: 0.000
* MD5 loading time: 0.001
* cache loading time: 0.002
frontend/ui/rendertext.lua:4 => cacheitem
cacheitem loading time: 0.000
* ui/rendertext loading time: 0.004
* ui/widget/textwidget loading time: 0.004
frontend/ui/widget/button.lua:3 => ui/widget/imagewidget
frontend/ui/widget/imagewidget.lua:3 => ffi/mupdfimg
ffi/mupdfimg.lua:14 => ffi/mupdf_h
ffi/mupdf_h loading time: 0.000
* ffi/mupdfimg loading time: 0.002
* ui/widget/imagewidget loading time: 0.002
frontend/ui/widget/button.lua:6 => ui/gesturerange
ui/gesturerange loading time: 0.000
frontend/ui/widget/button.lua:7 => ui/widget/container/framecontainer
ui/widget/container/framecontainer loading time: 0.000
* ui/widget/button loading time: 0.007
* ui/widget/buttontable loading time: 0.008
* ui/widget/buttondialog loading time: 0.008
frontend/apps/filemanager/filemanagerhistory.lua:7 => ui/widget/menu
frontend/ui/widget/menu.lua:3 => ui/widget/container/leftcontainer
ui/widget/container/leftcontainer loading time: 0.000
frontend/ui/widget/menu.lua:4 => ui/widget/container/rightcontainer
ui/widget/container/rightcontainer loading time: 0.000
frontend/ui/widget/menu.lua:7 => ui/widget/container/bottomcontainer
ui/widget/container/bottomcontainer loading time: 0.000
frontend/ui/widget/menu.lua:8 => ui/widget/container/underlinecontainer
ui/widget/container/underlinecontainer loading time: 0.000
frontend/ui/widget/menu.lua:9 => ui/widget/horizontalspan
ui/widget/horizontalspan loading time: 0.000
frontend/ui/widget/menu.lua:10 => ui/widget/focusmanager
ui/widget/focusmanager loading time: 0.000
frontend/ui/widget/menu.lua:12 => ui/widget/overlapgroup
ui/widget/overlapgroup loading time: 0.000
frontend/ui/widget/menu.lua:26 => ui/widget/infomessage
frontend/ui/widget/infomessage.lua:9 => ui/widget/textboxwidget
ui/widget/textboxwidget loading time: 0.000
ui/widget/infomessage loading time: 0.001
* ui/widget/menu loading time: 0.003
** apps/filemanager/filemanagerhistory loading time: 0.013
frontend/apps/filemanager/filemanager.lua:4 => apps/filemanager/filemanagermenu
frontend/apps/filemanager/filemanagermenu.lua:3 => ui/widget/confirmbox
ui/widget/confirmbox loading time: 0.000
frontend/apps/filemanager/filemanagermenu.lua:4 => ui/widget/touchmenu
frontend/ui/widget/touchmenu.lua:10 => ui/widget/inputdialog
frontend/ui/widget/inputdialog.lua:8 => ui/widget/inputtext
frontend/ui/widget/inputtext.lua:2 => ui/widget/scrolltextwidget
frontend/ui/widget/scrolltextwidget.lua:3 => ui/widget/verticalscrollbar
ui/widget/verticalscrollbar loading time: 0.000
ui/widget/scrolltextwidget loading time: 0.000
frontend/ui/widget/inputtext.lua:5 => ui/widget/virtualkeyboard
* ui/widget/virtualkeyboard loading time: 0.001
* ui/widget/inputtext loading time: 0.002
* ui/widget/inputdialog loading time: 0.003
frontend/ui/widget/touchmenu.lua:13 => ui/widget/iconbutton
ui/widget/iconbutton loading time: 0.000
frontend/ui/widget/touchmenu.lua:23 => ui/networkmgr
ui/networkmgr loading time: 0.000
* ui/widget/touchmenu loading time: 0.004
frontend/apps/filemanager/filemanagermenu.lua:6 => ui/otamanager
ui/otamanager loading time: 0.000
frontend/apps/filemanager/filemanagermenu.lua:12 => ui/language
ui/language loading time: 0.000
frontend/apps/filemanager/filemanagermenu.lua:15 => apps/reader/modules/readerfrontlight
frontend/apps/reader/modules/readerfrontlight.lua:3 => ui/widget/notification
ui/widget/notification loading time: 0.000
apps/reader/modules/readerfrontlight loading time: 0.001
frontend/apps/filemanager/filemanagermenu.lua:16 => apps/filemanager/filemanagersearch
frontend/apps/filemanager/filemanagersearch.lua:3 => document/documentregistry
frontend/document/documentregistry.lua:63 => document/pdfdocument
frontend/document/pdfdocument.lua:3 => ui/data/koptoptions
frontend/ui/data/koptoptions.lua:2 => ui/data/strings
ui/data/strings loading time: 0.000
ui/data/koptoptions loading time: 0.001
frontend/document/pdfdocument.lua:4 => document/document
frontend/document/document.lua:1 => ffi/drawcontext
ffi/drawcontext loading time: 0.000
frontend/document/document.lua:5 => document/tilecacheitem
frontend/document/tilecacheitem.lua:3 => serialize
serialize loading time: 0.000
document/tilecacheitem loading time: 0.001
frontend/document/document.lua:7 => configurable
configurable loading time: 0.000
* document/document loading time: 0.001
* document/pdfdocument loading time: 0.002
frontend/document/documentregistry.lua:64 => document/djvudocument
document/djvudocument loading time: 0.000
frontend/document/documentregistry.lua:65 => document/credocument
frontend/document/credocument.lua:1 => ui/data/creoptions
ui/data/creoptions loading time: 0.000
* document/credocument loading time: 0.001
frontend/document/documentregistry.lua:66 => document/picdocument
document/picdocument loading time: 0.000
* document/documentregistry loading time: 0.005
* apps/filemanager/filemanagersearch loading time: 0.006
frontend/apps/filemanager/filemanagermenu.lua:17 => apps/filemanager/filemanagersetdefaults
frontend/apps/filemanager/filemanagersetdefaults.lua:5 => ui/widget/multiinputdialog
ui/widget/multiinputdialog loading time: 0.000
* apps/filemanager/filemanagersetdefaults loading time: 0.001
** apps/filemanager/filemanagermenu loading time: 0.013
frontend/apps/filemanager/filemanager.lua:7 => ui/widget/filechooser
ui/widget/filechooser loading time: 0.000
** apps/filemanager/filemanager loading time: 0.027
```