mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Richard B. Johnson" <root@chaos.analogic.com>
To: Jeff Garzik <jgarzik@pobox.com>
Cc: Linus Torvalds <torvalds@transmeta.com>,
	Rusty Trivial Russell <rusty@rustcorp.com.au>,
	linux-kernel@vger.kernel.org
Subject: Re: [TRIVIAL] kstrdup
Date: Fri, 18 Apr 2003 15:29:49 -0400 (EDT)	[thread overview]
Message-ID: <Pine.LNX.4.53.0304181512220.22901@chaos> (raw)
In-Reply-To: <3EA0469D.7090602@pobox.com>

On Fri, 18 Apr 2003, Jeff Garzik wrote:

> Richard B. Johnson wrote:
> > On Fri, 18 Apr 2003, Jeff Garzik wrote:
> >
> >
> >>Linus Torvalds wrote:
> >>
> >>>On Fri, 18 Apr 2003, Jeff Garzik wrote:
> >>>
> >>>
> >>>>You should save the strlen result to a temp var, and then s/strcpy/memcpy/
> >>>
> >>>
> >>>No, you should just not do this. I don't see the point.
> >>
> >>
> >>strcpy has a test for each byte of its contents, and memcpy doesn't.
> >>Why search 's' for NULL twice?
> >>
> >>	Jeff
> >
> >
> > Because it doesn't. strcpy() is usually implemented by getting
> > the string-length, using the same code sequence as strlen(), then
> > using the same code sequence as memcpy(), but copying the null-byte
> > as well. The check for the null-byte is done in the length routine.
> >
> > If you do a memcpy(a, b, strlen(b));, then you are making two
> > procedure calls and dirtying the cache twice..
>
> Wrong, because we have to call strlen _anyway_, to provide the size to
> kmalloc.
>
>
> > A typical Intel procedure, stripped of the push/pops to save
> > registers is here....
>
> That's kinda cute.  Why not submit a patch to the strcpy implementation
> in include/asm-i386/string.h?  :)  Ours is shorter, but does have a jump:
>          "1:\tlodsb\n\t"
>          "stosb\n\t"
>          "testb %%al,%%al\n\t"
>          "jne 1b"
>

Years ago I did submit patches of all kinds, mostly 'asm' stuff
because I have been doing assembly for over 20 years. However,
I got tired of being shot-down in flames by persons who haven't
a clue, so I stopped doing that.

The history of the stuff in asm-i386 is full of changes, with
many bugs introduced by persons who tried so save a nanosecond
here and there. In recent times (past two years) somebody changed
the stuff back to things which were not optimum, but quite obviously
correct.

I might 'risk' sending in some patches again. Maybe.

> Which is better?  I don't know; I'm still learning the performance
> eccentricities of x86 insns on various processors.
>

The test for every byte transferred is, quite obviously, correct.
It is also, quite obviously, non optimum.

>
> Related x86 question:  if the memory buffer is not dword-aligned, is
> 'rep movsl' the best idea?  On RISC it's usually smarter to unroll the
> head of the loop to avoid unaligned accesses; but from reading x86 asm
> code in the kernel, nobody seems to care about that.  Is the
> unaligned-access penalty so small that the increased code size of the
> head-unroll is never worth it?
>

Unaligned access takes a penalty. On early i586 machines, it was
horrible, doubled the access time. On i486 and, later on i686
machines, the access times are not changed as radically. Many
of the changes, that were later reverted back, to the string
and memory 'asm' routines occurred when the 'awful' i586
came out. Of course, the unaligned access on the i586 was
still faster than the i486 with aligned access (because of
clock speeds). However, it was worth the trouble to improve
the assembly routines at that time.

>
> > A lot of persons who are unfamiliar with tools other than 'C' think
> > that strcpy() is made like this:
> >
> > 	while(*dsp++ = *src++)
> >                    ;
>
> In fact, that's basically the kernel's non-arch-specific implementation :)
>
> 	Jeff

Yep. Naive code looks so 'simple', must be "optimum", no? ;^).


Cheers,
Dick Johnson
Penguin : Linux version 2.4.20 on an i686 machine (797.90 BogoMips).
Why is the government concerned about the lunatic fringe? Think about it.


  parent reply	other threads:[~2003-04-18 19:15 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-18  7:58 Rusty Trivial Russell
2003-04-18  8:10 ` Jeff Garzik
2003-04-18 16:20   ` Linus Torvalds
2003-04-18 16:56     ` Jeff Garzik
2003-04-18 18:00       ` Linus Torvalds
2003-04-18 18:44         ` Jeff Garzik
2003-04-19  4:52         ` Rusty Russell
2003-04-18 18:00       ` Richard B. Johnson
2003-04-18 18:40         ` Jeff Garzik
2003-04-18 19:24           ` H. Peter Anvin
2003-04-18 23:37             ` Alan Cox
2003-04-18 19:29           ` Richard B. Johnson [this message]
2003-04-19 11:45             ` Kai Henningsen
2003-04-19 20:16             ` Ruth Ivimey-Cook
2003-04-19  4:14   ` Rusty Russell
2003-04-19  4:48     ` Jeff Garzik
2003-04-19  8:28       ` Rusty Russell
2003-04-19 12:27       ` Alan Cox
2003-04-19 14:44         ` Bernd Eckenfels
2003-04-20  8:05         ` Rusty Russell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.53.0304181512220.22901@chaos \
    --to=root@chaos.analogic.com \
    --cc=jgarzik@pobox.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rusty@rustcorp.com.au \
    --cc=torvalds@transmeta.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®