Skip to content

Conversation

@mmatyas
Copy link
Contributor

@mmatyas mmatyas commented Apr 12, 2018

Their code differs only in handling the array index.

JerryScript-DCO-1.0-Signed-off-by: Mátyás Mustoha mmatyas@inf.u-szeged.hu

@mmatyas
Copy link
Contributor Author

mmatyas commented Apr 12, 2018

This reduces the binary size by about 2000 bytes on my machine (libjerry-core.a: 3301510 ->3299494).

@mmatyas mmatyas force-pushed the builtin_codemin_3 branch from 218e2c1 to afbe87a Compare April 13, 2018 08:55
@mmatyas
Copy link
Contributor Author

mmatyas commented Apr 13, 2018

Rebased to master to fix the Travis build.

Copy link
Contributor

@LaszloLango LaszloLango left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM


/* 8.b */
while (!k_present && index < len && ecma_is_value_empty (ret_value))
bool has_items_left = start_from_left ? (index < len) : (index != UINT32_MAX);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has_items_left ? I also don't like the UINT32_MAX comparison, sometimes the compilers decides that certain overflowed values cannot happen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original code for reduceRight used an int64_t index to handle the case where the index is 0 and reduced by 1. I've used a uint32 for both cases, and compare with UINT32_MAX (ie. -1) instead. Would you prefer using int64_t?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to use an indexing from right? So go from 0 - len, and substract the current value from the len? I would not use int64 because it is not a native type on 32 bit systems.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the whole has_items_left could be replaced with a counter -- I'll push an update.


/* 8.b.iv */
index++;
index = start_from_left ? (index + 1) : (index - 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could have a direction or something similar.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean using a direction type, something like enum { LEFT, RIGHT } ?

@mmatyas mmatyas force-pushed the builtin_codemin_3 branch from afbe87a to 6aa16f6 Compare April 16, 2018 10:04

/* 6. */
uint32_t index = 0;
uint32_t index = start_from_left ? 0 : (len - 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok my idea looks like the following. This variable is always initialized with 0.
uint32_t index = 0;


/* 8.b */
while (!k_present && index < len && ecma_is_value_empty (ret_value))
while (!k_present && items_visited < len && ecma_is_value_empty (ret_value))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is always index < len, just like in the original.

ECMA_FINALIZE (current_value);

/* 8.b.iv */
index++;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is index++.

while (!k_present && items_visited < len && ecma_is_value_empty (ret_value))
{
/* 8.b.i */
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here comes the catch: depending on start_from_left we read index or len - 1 - index. We can create a local variable for len - 1.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way no need int64 magic, no need to handle negative numbers.

@mmatyas mmatyas force-pushed the builtin_codemin_3 branch from 6aa16f6 to ebf09e6 Compare April 17, 2018 12:36
@mmatyas
Copy link
Contributor Author

mmatyas commented Apr 17, 2018

@zherczeg updated, what do you think?

Copy link
Member

@zherczeg zherczeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting close.

{
/* 8.b.i */
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index);
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (start_from_left
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the correct style.

(a ? b
   : c)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks.

ecma_value_t current_index;

for (; index < len && ecma_is_value_empty (ret_value); index++)
while (index < len && ecma_is_value_empty (ret_value))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a personal preference to remove the for or something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was changed due to the index update in the previous versions. With that part reverted, the for might be a better choice indeed.

Their code differs only in handling the array index.

JerryScript-DCO-1.0-Signed-off-by: Mátyás Mustoha mmatyas@inf.u-szeged.hu
@mmatyas mmatyas force-pushed the builtin_codemin_3 branch from ebf09e6 to b2f1e39 Compare April 18, 2018 11:57
Copy link
Member

@zherczeg zherczeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@yichoi
Copy link
Contributor

yichoi commented Apr 19, 2018

LGTM

@yichoi yichoi merged commit c288cda into jerryscript-project:master Apr 19, 2018
jimmy-huang pushed a commit to jimmy-huang/jerryscript that referenced this pull request May 8, 2018
…ript-project#2280)

Their code differs only in handling the array index.

JerryScript-DCO-1.0-Signed-off-by: Mátyás Mustoha mmatyas@inf.u-szeged.hu
@mmatyas mmatyas deleted the builtin_codemin_3 branch September 26, 2018 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants