Bug 157665 - Implement a three-way comparison operator to replace ==, <, >, <=, >=
Summary: Implement a three-way comparison operator to replace ==, <, >, <=, >=
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: LibreOffice (show other bugs)
Version:
(earliest affected)
24.2.0.0 alpha0+
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard: target:24.8.0
Keywords: difficultyBeginner, easyHack, skillCpp, topicCleanup
Depends on:
Blocks: Dev-related
  Show dependency treegraph
 
Reported: 2023-10-09 08:56 UTC by Mike Kaganski
Modified: 2024-03-25 12:51 UTC (History)
5 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 2023-10-09 08:56:25 UTC
Since C++20, a three-way comparison operator (a "spaceship" operator <=>) is available [1].

The codebase uses C++20 since commit 1eef07805021b7ca26a1a8894809b6d995747ba1.

In cases where we have a full set of comparison operators, it could be beneficial to replace them all with such a three-way operator, to benefit from code deduplication. Indeed, this needs to take performance into account.

In many cases, the implementation of the operator may also be simplified from something like

>    if (a < other.a)
>        ...
>    else if (a == other.a && b < other.b)
>        ...

into more idiomatic

>   return std::tie(a, b) <=> std::tie(other.a, other.b);

Sometimes there is no full set of comparison operators in a class, and only e.g. operator < is defined. In such cases, the three-way comparison would be unnecessary; but the simplification of the implementation comparing std::tie could still be used.

When simplifying the implementations in such a way, performance needs to also be taken into account: e.g., some calculations should only be performed after previous comparisons were done, which would not happen, if everything would be passed to std::tie at once.

This is an easyhack that can be worked on in parallel. Please don't do a large-scale change over the whole codebase; only change one or two files at a time. Do not assign this issue to you, because of the parallel nature of the task.

[1] https://en.cppreference.com/w/cpp/language/operator_comparison#Three-way_comparison
Comment 1 t.aswath 2023-11-11 11:15:19 UTC
could you recommend specific files or areas where changes might be particularly beneficial.
Comment 2 Buovjaga 2023-11-11 11:19:27 UTC
(In reply to t.aswath from comment #1)
> could you recommend specific files or areas where changes might be
> particularly beneficial.

One of the tasks in easy hacks is to apply and grow your discovery skills, so can you please tell us how you tried to search for candidates so far?
Comment 3 Commit Notification 2024-03-25 12:51:30 UTC
RMZeroFour committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/6227fccce44e7c44a7a6da707f361b1d4886c1d0

tdf#157665 Use <=> operator for errcode and strong_int

It will be available in 24.8.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.