On Thu, 26 Feb 2004 09:57:00 +0000, Anton Altaparmakov <ai...@ca...> =
wrote:
>I committed this fix instead:
>- /* Need two bytes for null terminator. */
>- maxlen -=3D 2;
>+ /* Convert maxlen from bytes to unicode characters. */
>+ maxlen /=3D sizeof(uchar_t);
just nitpicking, but maybe getting rid of the magic number 2
and using sizeof(uchar_t), or even more sizeof(*dest) at both=20
places is a bit better
>why not uni_size =3D size / sizeof(uchar_t) - 1; ?
>then you don't need the -1 in the test
for some reason i thought it'll make easier for the casual=20
reader to detect that the - 1 is meant to reserve the
space for the terminator, but most likely you are right,
and it doesn't help; and if there is a comment, it doesn't
matter anyway
>I don't like assert() very much because it is too easy to create
>Heisenbugs by using things in inside the assert() that have side
>effects...
bugs are easy to create with almost every language construct :O)
ok, no asserts will be used
do you feel that an if() is needed?
or the comment added in my version of the fn description is enough?
>Please stick with DocBook style for parameters
can you give me a pointer to some DocBook intro ?
>Another comment about your style is can we please have C style comments:
> /* comment */
>Instead of C++ style:
> //
>
>I only use C++ style for TODO or FIXME comments with the intention of
>them going away when the TODO or FIXME has been done...
i've only seen that // was used extensively, but you are right,
only for this specific purpose; if it's important i'll try to
get back into the habit of using /* */ instead
>> // while there's both input and output left
>> while ( *src && --uni_size )
>> *dst++ =3D cpu_to_le16( *src++ ) ; // ascii -> unicode
>
>Again, I don't trust gcc to do the right thing. (-: I prefer to cache
>*src in a temporary variable for reuse. I guess gcc would do the right
>thing as you made src const...
>
>You might notice I (perhaps unfairly) don't trust gcc to do anything
>well so I tend to write what I consider optimized code instead of hoping
>that gcc would optimize it... I know the performance of stoucs() in
>mkntfs isn't important at all but hey. (-8
<also some previous comments on efficiency were deleted>
this is the only point where i feel i have to make a stand
-- but if you'll insist, i'll do my best to optimize :O)
- i'd prefer maintainability over efficiency; if can be
understood, then it can be changed (fixed!), so it can=20
be optimized; if it's uncomprehensible, it cannnot be=20
fixed, updated or optimized, in the rare cases where=20
that's needed, and imho this (or any other i/o-centric code)=20
isn't that case
- today it's extremely hard to tell which code is faster,
even with asm code (consider pipelines, cache etc); with C=20
basically the only way to tell is trial and error (or a lot=20
of experience); for example your original (fixed) stoucs()=20
and the last version generate the same code:
gcc -O2 -Wall -fms-extensions -c -S o.c
; original
.L7:
* movzbl (%ebx,%edi), %eax ; al: c =3D src[i]
* testb %al, %al ; !c ?
* je .L3 ; yes -> exit
* movsbl %al,%eax ; call cpu_to_le16( c )
* movl %eax, (%esp)
* call cpu_to_le16
movl 8(%ebp), %edx ; edx: dst
movw %ax, (%edx,%ebx,2) ; dst[i] =3D result
* incl %ebx ; ++i
cmpl %esi, %ebx ; i < maxlen ?
jl .L7 ; smaller -> repeat
; c-ish fix
.L14:
* movsbl %al,%eax ; eax =3D *src
* incl %ebx ; ++src
* movl %eax, (%esp) ; call cpu_to_le16( eax )
* call cpu_to_le16
movw %ax, (%edi) ; *dst =3D result
addl $2, %edi ; ++dst
* movzbl (%ebx), %eax ; al =3D *src
* testb %al, %al ; 0 =3D=3D *src ?
* je .L11 ; yes -> exit
decl %esi ; --uni_size
jne .L14 ; nonzero -> repeat
the only difference is the ordering of the instructions
(* marks instructions that match in the two blocks), and=20
three-three 1-cycle statements
actually, on a 8086 the c-ish version would have been faster
(all variables are stored in registers, and simpler addressing
methods) than your optimized code, but on a pentium it's the same
should i still try to optimize?
br,
andras
"We should forget about small efficiencies, say about 97% of the time:=20
premature optimization is the root of all evil."
Knuth (although some say it was originally Hoare)
|