Bug 145587 - WEEKS spreadsheet add-in function needs optimizations
Summary: WEEKS spreadsheet add-in function needs optimizations
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Calc (show other bugs)
Version:
(earliest affected)
Inherited From OOo
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard: target:7.4.0 target:7.3.0.0.beta2
Keywords: difficultyBeginner, easyHack, skillCpp, topicDebug
Depends on:
Blocks: Calc-Function
  Show dependency treegraph
 
Reported: 2021-11-08 08:40 UTC by Mike Kaganski
Modified: 2022-01-06 12:08 UTC (History)
3 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mike Kaganski 2021-11-08 08:40:55 UTC
WEEKS [1] is implemented in ScaDateAddIn::getDiffWeeks [2]. It does a lot of unnecessary calculations:

0. In mode 0, it operates on the simple date difference. It is absolutely not required to increment the two passed date by null date before getting the difference in this mode, especially since the null date needs expensive calculations (accessing XPropertySet using UNO calls; calling DateToDays; using exception handling).
1. In mode 1, it calculates the ISO week difference; and for that, it considers not only the relevant fact that ISO weeks start on Monday (which is trivially calculated), but also unnecessarily the week number in start date's year, which is not that trivial: it calls DaysToDay and DateToDays (and also cheaper, but still unnecessary, run-time modulo-7 divisions of that resulting value).
2. It is also possible to optimize DateToDays, that is called not only in getDiffWeeks, to treat the default null date value specially, like is done in Date::GetAsNormalizedDays() [3]. Or maybe even make the module depend on tools, and use existing functions from tools right there, to dedulpicate/optimize things.

The fix should consider bug 145578. If this easy hack is resolved prior to bug 145578, it should fix that bug as part of it. If it is resolved after bug 145578, it must make sure to keep it working properly, i.e. for all dates, including before and after the date chosen as a reference point to calculate week starts (e.g., 0001-01-01), it must ensure that WEEKS(date1;date2;1)+WEEKS(date2;date1;1)=0.

[1] https://help.libreoffice.org/latest/en-US/text/scalc/01/04060111.html?DbPAR=CALC#bm_id3149048
[2] https://git.libreoffice.org/core/+/6680e51716e383c68bb1ec9cc0a05d698d3b6a3d/scaddins/source/datefunc/datefunc.cxx#493
[3] https://git.libreoffice.org/core/+/6680e51716e383c68bb1ec9cc0a05d698d3b6a3d/tools/source/datetime/tdate.cxx#136
Comment 1 Xisco Faulí 2021-11-08 09:10:30 UTC
Hi Mike,
Thanks for reporting this easyHack. Moving to NEW
Comment 2 Commit Notification 2021-11-29 08:09:05 UTC
Winfried Donkers committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/105196aa00bc0d3e426796f6729a8e7e51271e56

tdf#145578, tdf#145587 revise calculations for WEEKS Add-In function

It will be available in 7.4.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 3 Winfried Donkers 2021-11-29 15:05:50 UTC
It looks like item 0 and 1 of comment #0 have been taken care of, I will start on item 2 and try to remove redundant code plus optimize where possible.
Comment 4 Commit Notification 2021-11-30 09:09:46 UTC
Winfried Donkers committed a patch related to this issue.
It has been pushed to "libreoffice-7-3":

https://git.libreoffice.org/core/commit/bd8e584db0c19a30164575d7fc8c583436db5b85

tdf#145578, tdf#145587 revise calculations for WEEKS Add-In function

It will be available in 7.3.0.0.beta2.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 5 Winfried Donkers 2021-12-04 13:23:41 UTC
Item 2 of comment #0 is not a quick and simple change. Making module scaddins depend on tools (for class Date) asks more time than I have right now.
Therefore I step back in case someone else sees possibilities to fix time 2 sooner than I do.
Comment 6 Winfried Donkers 2022-01-06 12:08:56 UTC
Further optimisations to scaddins code will be in seperate bug reports.