-
Notifications
You must be signed in to change notification settings - Fork 683
Refactoring jerry-libc to squeeze out some extra bytes from the code (+fixes) #845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The result and effect of the three patches on code size as applied on top of master and incrementally on each other. There is almost always some bytes or some houndred bytes gain, except for 3 hardly explicable code size increases.
|
@@ -225,12 +202,12 @@ strncmp (const char *s1, const char *s2, size_t n) | |||
char * __attr_used___ // FIXME |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this FIXME comment valid after your patch?
LGTM |
value = (uintmax_t)va_arg (*args_list_p, int); /* short int is promoted to int */ | ||
break; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the compiler do this automatically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Me too. Still, as the table above shows, doing the merge manually does have an impact on the code size.
Generally, I think it is good practice not to write overly verbose, or suboptimal, or clone-full code and rely on the compiler to optimize it away. Since sometimes it fails. (Up to a certain point of course. Extreme brevity and incomprehensible optimizations can have a critical maintenance cost in the long run, but that's another story. And this is not the case here, IMHO.)
@LaszloLango I have to be honest, I don't know why some of the functions are tagged with the used attribute while others are not. I did not dare to touch it... The description of the attribute is: "This attribute, attached to a function, means that code must be emitted for the function even if it appears that the function is not referenced." |
@@ -263,7 +240,8 @@ strlen (const char *s) | |||
int | |||
rand (void) | |||
{ | |||
uint32_t intermediate = libc_random_gen_state[0] ^ (libc_random_gen_state[0] << 11); | |||
uint32_t intermediate = libc_random_gen_state[0]; | |||
intermediate ^= intermediate << 11; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is harder to me to read. And I think the compiler can optimize this as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The body contains two more ^=
s... And as for the compiler optimization, same as above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is the only thing needs to be changed, and after that LGTM.
As for me I am not conviced it is worth to refactor the code this much. Perhaps in a few complicated cases. |
@zherczeg We are aiming at a super light libc, aren't we? |
This PR has been pending for a while. What shall happen to it? And should it be rejected, what should happen to the issues it fixes? |
@akiss77, I'm still thinking of this change is needed, but @zherczeg had doubts. It depends on him. |
@@ -284,5 +260,8 @@ rand (void) | |||
void | |||
srand (unsigned int seed) /**< new seed */ | |||
{ | |||
libc_random_gen_state[0] = | |||
libc_random_gen_state[1] = | |||
libc_random_gen_state[2] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this increase code size?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most probably it is increasing the code size with some bytes. However, I cannot think of a smaller solution for fixing srand
. Since that function must reinitialize the whole state of the pseudo-RNG to ensure that always the same sequence is generated from the same seed.
I think in general the patch is ok, but there are a few changes I don't particularly like. But I have no strong objection. |
@zherczeg, are the changes those you mentioned before, or something else? I could try and come up with "nicer" approaches. |
103d5aa
to
f216f90
Compare
Refactor memory and string handling routines * Potential code size improvements: * Most loops don't really need a new incrementing index variable running between 0..n-1 but can just loop `while (n--)`, and the bodies don't need array indexing but can just use the `*p++` idiom. * Compare routines are not required to return -1, 0, and +1, but any negative, zero, or positive result will do. * `strncmp` may follow the other routines and does not have to have defined behaviour if any of its args is NULL. * Fix: * `strncmp` did not work correctly if the strings were equal but `n` was greater than their length (did not stop at the terminating zero character). Refactor printf * Merging code duplications, removing dead initialization. Refactor rand and srand * Making sure that the type of the state variables is OK (`uint32_t` instead of `unsigned int`). * Getting type conversions OK. * Fixing `srand` to write all state variables and thus indeed generate the same random sequence. JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
f216f90
to
3608f8d
Compare
'cause every byte matters :)