Skip to content

Random optimizations#9657

Merged
6 commits merged into
masterfrom
unknown repository
Oct 28, 2022
Merged

Random optimizations#9657
6 commits merged into
masterfrom
unknown repository

Conversation

@ghost

@ghost ghost commented Oct 20, 2022

Copy link
Copy Markdown

This PR implements two different things:

  1. commit: Optimize UIManager by
  • hiding some logger.dbg behind dbg.is_on. This saves evaluation of the logger.dbg arguments, (Power)
  • minor refactoring of some logic, (Readability) and
  • despaghettify some code (Readability).
  1. commit: Replace some wrapper functions with aliases (but only if the wrapped method can not be overloaded). This is for minimi powersavings.

The PR should

  • simplify and improve the readability of the code
  • improve power saving (when debugging is off).

Dunno if this PR is desired. If not, pleas close.


This change is Reviewable

@Frenzie Frenzie added this to the 2022.11 milestone Oct 20, 2022
@NiLuJe

NiLuJe commented Oct 20, 2022

Copy link
Copy Markdown
Member

Nay for the dbg.is_on branches (it hampers readability too much for the very meager gains it offers (if any), the only meaningful logging-related branch we have that passes that benchmark is the input event parsing one), yay for the rest ;).

Comment thread frontend/device/wakeupmgr.lua Outdated
function WakeupMgr:getWakeupAlarmSys()
return RTC:getWakeupAlarmSys()
end
WakeupMgr.getWakeupAlarmSys = RTC.getWakeupAlarmSys

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That won't work because RTC is not a module-local, IIRC (it can be overloaded by the constructor).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see a problem: Calling WakeupMgr:getWakeupAlarmSys() will call RTC.getWakeupAlarmSys(self), but I also see that self is not used in RTC.getWakeupAlarmSys (in ffi/rtc.lua).

So it is OK but not ultra clean to use WakeupMgr.getWakeupAlarmSys = RTC.getWakeupAlarmSys. But if we want to be ultra clean we should also rewrite RTC:getWakeupAlarmSys() to RTC.getWakupAlarmSys()

Is that the problem you are adressing here or are you maybe confusing somthing like

self.device.wakeup_mgr = WakeupMgr:new{rtc = require("device/kindle/mockrtc")}
with this. As I understand (although I am not familiar with the code) WakeupMgr has a member called rtc (lowercase). This rtc can be set by a constructor. But the line here is using RTC (uppercase), which is a local RTC = require("ffi/rtc")) (and can't be overloaded by anything). ???
(It has been my original intention: Use grep -RPzo "\nfunction.*\(\)\n .*\(\)\nend\n" frontend/ to find wrappers and replace them. But it was obvious that self.something can not be optimized, so only a few methods remained.)

@NiLuJe NiLuJe Oct 21, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of the rtc = RTC in the constructor, but you raise another interesting concern, too, so, yeah, I'd forget about this one ;).

(LuaJIT is very good at tail-call optimizations anyway).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I will leave this change in, and I am looking forward to the solution of the "interesting concern".

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd revert it ;).

Comment thread frontend/ui/uimanager.lua Outdated
Comment thread frontend/ui/uimanager.lua
Comment thread frontend/ui/uimanager.lua
-- use our own main loop
if not self.looper then
while self._running do
repeat

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I abhor repeat constructs.

And a while makes much more sense here, because we do want to check the condition first.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I accept you personal decision about repeat-until constructs (as here there is no real performance drawback :) ). (But we set self._running to true a few lines above, so repeat-until would be OK.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather figure out a way to actually remove the whole _running & _run_forever stuff altogether, because it feels a little gadgety ;).

@ghost ghost marked this pull request as ready for review October 22, 2022 06:34
Comment thread frontend/ui/uimanager.lua Outdated
@ghost

ghost commented Oct 27, 2022

Copy link
Copy Markdown
Author

Ready to merge?

@NiLuJe NiLuJe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another random thought about using aliases for Event handlers: beware of the return value (because it controls propagation).

(Not an issue here given the type of events in question ;)).

Reviewed 2 of 4 files at r1, 2 of 2 files at r3, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @poire-z and @zwim)


frontend/ui/uimanager.lua line 1105 at r3 (raw file):

        if self._window_stack[i].widget.covers_fullscreen then
            start_idx = i
            if dbg.is_on and i > 1 then

Forgot this one ;).

@Frenzie Frenzie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @NiLuJe and @poire-z)


frontend/ui/uimanager.lua line 1105 at r3 (raw file):

Previously, NiLuJe (NiLuJe) wrote…

Forgot this one ;).

Logic suggests it should be a touch faster that way than having logger.dbg decide the same thing, assuming that's backed up by profiling.

@NiLuJe NiLuJe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @Frenzie and @poire-z)


frontend/ui/uimanager.lua line 1105 at r3 (raw file):

Previously, Frenzie (Frans de Jonge) wrote…

Logic suggests it should be a touch faster that way than having logger.dbg decide the same thing, assuming that's backed up by profiling.

Possibly, but if nothing else than for consistency's sake, those concerns are left to logger unless the gains are obvious (c.f., my earlier comment about the verbose input event decodes) ;).

@ghost ghost requested a review from NiLuJe October 27, 2022 18:55

@ghost ghost left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @NiLuJe and @poire-z)


frontend/ui/uimanager.lua line 1105 at r3 (raw file):

Previously, NiLuJe (NiLuJe) wrote…

Possibly, but if nothing else than for consistency's sake, those concerns are left to logger unless the gains are obvious (c.f., my earlier comment about the verbose input event decodes) ;).

Yeah, it would be faster with ad dbg.is_on check, but not really noticeable.
I would say we just should keep in mind that for all logger.xxx calls the arguments are always evaluated, even if the function itself is a noop.

For this special case I have no strong opinion to keep if dbg.is_on or drop it. We have a similar construct in Line 202 (but there the logger call before the if costs more so hmm, I would drop it.

@Frenzie Frenzie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @NiLuJe and @poire-z)


frontend/ui/uimanager.lua line 1105 at r3 (raw file):

Previously, zwim wrote…

Yeah, it would be faster with ad dbg.is_on check, but not really noticeable.
I would say we just should keep in mind that for all logger.xxx calls the arguments are always evaluated, even if the function itself is a noop.

For this special case I have no strong opinion to keep if dbg.is_on or drop it. We have a similar construct in Line 202 (but there the logger call before the if costs more so hmm, I would drop it.

I'm not entirely sure what you mean by special case. If it makes no appreciable performance difference it's not a special case to me. ;-)

@ghost ghost left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @NiLuJe and @poire-z)


frontend/ui/uimanager.lua line 1105 at r3 (raw file):

Previously, Frenzie (Frans de Jonge) wrote…

I'm not entirely sure what you mean by special case. If it makes no appreciable performance difference it's not a special case to me. ;-)

"This special case" was just a saying and meant nothing special (huh). Exactly spoken I meant in this "this context".

@Frenzie Frenzie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 3 of 4 files reviewed, 2 unresolved discussions (waiting on @NiLuJe and @poire-z)


frontend/ui/uimanager.lua line 1105 at r3 (raw file):

Previously, zwim wrote…

"This special case" was just a saying and meant nothing special (huh). Exactly spoken I meant in this "this context".

Specific? ^_^

@ghost ghost left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 3 of 4 files reviewed, 2 unresolved discussions (waiting on @NiLuJe and @poire-z)


frontend/ui/uimanager.lua line 1105 at r3 (raw file):

Previously, Frenzie (Frans de Jonge) wrote…

Specific? ^_^

I don't want to fool you. But I think we have a communication problem ;-)
I wanted to say:
1.) I would drop it and
2.) don't forget that arguments of functions are always evaluated.

@NiLuJe NiLuJe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 1 files at r4, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @Frenzie and @poire-z)


frontend/ui/uimanager.lua line 1105 at r3 (raw file):

Previously, zwim wrote…

I don't want to fool you. But I think we have a communication problem ;-)
I wanted to say:
1.) I would drop it and
2.) don't forget that arguments of functions are always evaluated.

Yeah, that's kind of my reasoning for dropping it ;).

Namely: the argument.. argument ;p doesn't hold here as they're relatively trivial; the main issue is the fact that it's the sole call behind a dedicated branch...
And that we indeed have two similar constructs, so handling only one doesn't make sense ;).

(Not that I'm advocating for handling both, although I do understand the motivation. I'm mostly against it so as not to set a precedent when it's for such trivial branching).

@NiLuJe NiLuJe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3 of 3 files at r5, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @Frenzie and @poire-z)

@ghost ghost merged commit d7c63ed into koreader:master Oct 28, 2022
@ghost ghost deleted the random_optimizations branch October 28, 2022 05:51
@poire-z

poire-z commented Nov 7, 2022

Copy link
Copy Markdown
Contributor

Some issues caused by this PR: #9569 (comment) , #9741.
@zwim: in case you have done these kind of shortcuts in some other PRs recently, can you please check they are valid?

@ghost

ghost commented Nov 7, 2022

Copy link
Copy Markdown
Author

Thanks, not that I can recall. I will fix that.

Frenzie pushed a commit that referenced this pull request Nov 7, 2022
0xstillb pushed a commit to 0xstillb/koreader-thai that referenced this pull request May 9, 2026
0xstillb pushed a commit to 0xstillb/koreader-thai that referenced this pull request May 9, 2026
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants