[vim/vim] Unnecessary multiplications in backspace code (PR #14308)

22 views
Skip to first unread message

zeertzjq

unread,
Mar 26, 2024, 5:58:06 PM3/26/24
to vim/vim, Subscribed

Problem: Unnecessary multiplications in backspace code, as
"col / ts * ts" is the same as "col - col % ts".
Solution: Change "col / ts * ts" to "col - col % ts". Adjust the loop
and the comments ins_bs() to be easier to understand. Update
tests to reset 'smarttab' properly.


You can view, comment on, or merge this pull request online at:

  https://github.com/vim/vim/pull/14308

Commit Summary

  • 50ec541 Unnecessary multiplications in backspace code

File Changes

(3 files)

Patch Links:


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/14308@github.com>

Marvin Renich

unread,
Mar 26, 2024, 8:45:49 PM3/26/24
to vim...@googlegroups.com
* zeertzjq <vim-dev...@256bit.org> [240326 17:58]:
> Problem: Unnecessary multiplications in backspace code, as
> &quot;col / ts * ts&quot; is the same as &quot;col - col % ts&quot;.
> Solution: Change &quot;col / ts * ts&quot; to &quot;col - col % ts&quot;. Adjust the loop
> and the comments ins_bs() to be easier to understand. Update
> tests to reset &#39;smarttab&#39; properly.
>
> You can view, comment on, or merge this pull request online at:
>
> https://github.com/vim/vim/pull/14308

Did you examine the assembler produced by the compiler for these? I
expected that any reasonable optimizing compiler would produce identical
code, and in fact, gcc -O2 on amd64 (linux) does for a very simple
source program comparing them:

-------- multmod.c --------
/* multmod compares int division and modulus
*
* To compile to assembler with some optimization: cc -S -O2 multmod.c
*/
#include <stdio.h>

int mult(int a, int b)
{
return a / b * b;
}

int mod(int a, int b)
{
return a - a % b;
}

int main(int argc, char* argv[])
{
int a = 76;
int b = 7;
int c;

c = mult(a, b);
printf("c = %d\n", c);

c = mod(a, b);
printf("c = %d\n", c);
}
--------

Examining the produced assembler, mult and mod are identical (only one
idivl, with the expected subl).

It is certainly possible that for the specific source you are changing,
the compiler does not recognize the opportunity for optimization, but
until you inspect the assembler for the original code, you will not
know. I did not actually compile to assembler and examine the code you
are changing, so I cannot say definitively.

Unless there is a reason (such a particular architecture that is giving
problems with not-good-enough optimizations), I would avoid source churn
for zero benefit.

...Marvin

Christian Brabandt

unread,
Mar 27, 2024, 5:15:16 AM3/27/24
to vim/vim, Subscribed

That seems like unneccessary optimizations at the risk of breaking something. Does this really gain us something?


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/14308/c2022275762@github.com>

zeertzjq

unread,
Mar 27, 2024, 5:46:32 AM3/27/24
to vim/vim, Subscribed

Yes, this also deduplicates code between +vartabs and -vartabs.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/14308/c2022332310@github.com>

zeertzjq

unread,
Mar 27, 2024, 7:41:50 AM3/27/24
to vim/vim, Push

@zeertzjq pushed 1 commit.

  • 384445d Unnecessary multiplications in backspace code


View it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/14308/push/17727317268@github.com>

Christian Brabandt

unread,
Mar 28, 2024, 5:29:05 AM3/28/24
to vim/vim, Subscribed

okay thanks!


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/14308/c2024755156@github.com>

Christian Brabandt

unread,
Mar 28, 2024, 5:32:07 AM3/28/24
to vim/vim, Subscribed

Closed #14308 via 8ede7a0.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/14308/issue_event/12278544629@github.com>

Reply all
Reply to author
Forward
0 new messages