Skip to content
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

New External Magic String API to save heap memory #136

Merged
merged 3 commits into from
Jun 8, 2015
Merged

New External Magic String API to save heap memory #136

merged 3 commits into from
Jun 8, 2015

Conversation

seanshpark
Copy link
Contributor

Related issue: #109

JerryScript-DCO-1.0-Signed-off-by: SaeHie Park [email protected]

*/
static uint32_t ecma_magic_string_ex_count = 0;
static ecma_length_t* ecma_magic_string_ex_lengths = NULL;
static jerry_get_magic_string_t ecma_magic_string_ex_handler = NULL;
Copy link
Contributor

Choose a reason for hiding this comment

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

@seanshpark, could we implement the external magic strings with registration of a constant structures array, using a structure like the following?

typedef struct
{
  const jerry_char_t* value_p;
  jerry_api_length_t length;
} jerry_ext_magic_string_t;

This way, we can improve performance by removing call to an external handler while getting value of an external magic string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

with an jerry_ext_magic_string_t array I think ecma_magic_string_ex_handler is not needed any more. it can be accessed something like

ecma_magic_string_ex_table[id].value_p;

@ruben-ayrapetyan
Copy link
Contributor

@seanshpark, do I understand correctly that the external magic strings mechanism is supposed to be used for registration of externally defined identifiers like console, log, etc.?

@seanshpark
Copy link
Contributor Author

@ruben-ayrapetyan , yes, you are right. reason is to save heap for byte codes.

@ruben-ayrapetyan
Copy link
Contributor

@seanshpark, thank you for explanation. That's great!

@seanshpark
Copy link
Contributor Author

@ruben-ayrapetyan , as memory consumption point of view, this implementation requires NUM_OF_STRINGS * sizeof(ecma_length_t = uint16_t) bytes for ecma_magic_string_ex_lengths array. when changed to structure it needs additional NUM_OF_STRINGS * sizeof(const jerry_char_t*) and as usually structure is aligned in 4 bytes, it'll be 4 times the size. for example, if there is about 256 items, it'll need 512 bytes to store lengths, and for structure 2048 (= 256 * (4+4)) bytes. when measure with last week iotjs JS script it saved about 1.8K for about 180 string items in total.(measured peak with mem-stats on)
so I'm not sure about changing to structure, though I prefer the structure type.
please let me know if I missed something.

@seanshpark
Copy link
Contributor Author

@ruben-ayrapetyan , updated PR so that string array is used without the handler but not by structure you mentioned.

@egavrin egavrin added this to the Core ECMA features milestone Jun 2, 2015
@egavrin egavrin added the enhancement An improvement label Jun 2, 2015
@ruben-ayrapetyan
Copy link
Contributor

@seanshpark, I agree with you, that alignment of structure increases memory consumption and, so, using structure for defining external strings seems not to be the right way.

Could you, please, change the jerry_register_external_magic_strings interface to take array of lengths, that already contains strings' lengths, and add debug mode check that it is filled correctly?

Maybe, the lengths' array could be defined at compile-time similar to strings' array definition using sizeof operator.

In the case, we could make the strings' and lengths' arrays constant, would remove filling of externally defined array inside of engine, and, maybe, would remove runtime lengths' calculation.

@seanshpark
Copy link
Contributor Author

@ruben-ayrapetyan , thank you, PR is updated with your new idea. now no heap is used (I think). great. maybe we can change existing magic string like this one.

* Initialize data for external string helpers
*/
void
ecma_strings_ex_init (const ecma_char_ptr_t* ex_str_items, uint32_t count, const ecma_length_t* ex_str_lengths)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you, please, add comments to the arguments?

/**< character arrays, representing
  *    external magic strings' contents */
/**< number of the strings */
/**< lengths of the strings */

@seanshpark
Copy link
Contributor Author

Done as @ruben-ayrapetyan' comment.
afd532d fails cause __program needs to be NULL. 2b2f2df is to solve this.

#if CONFIG_ECMA_CHAR_ENCODING == CONFIG_ECMA_CHAR_ASCII
typedef uint8_t jerry_api_char_t;
#elif CONFIG_ECMA_CHAR_ENCODING == CONFIG_ECMA_CHAR_UTF16
typedef uint16_t jerry_api_char_t;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, change the definitions using uint8_t and uint16_t with single definition using ecma_char_t.

@ruben-ayrapetyan
Copy link
Contributor

@seanshpark, thank you for updating the changes.

Could you please, reorder commits in the pull request, to fix build problem?

After fixing build and API type definitions, the changes would look good for me.

@egavrin
Copy link
Contributor

egavrin commented Jun 5, 2015

As for me, all tree commits should go with separate pull requests.

@seanshpark after passing the review, please, apply these commits to master separately.

@seanshpark
Copy link
Contributor Author

@ruben-ayrapetyan ,

  1. last three comments
Please, change the definitions using uint8_t and uint16_t with single definition using ecma_char_t.
Please, change uint16_t to ecma_length_t.
Please, change jerry_api_char_t* to ecma_char_ptr_t.

cannot apply. compile error. ecma_globals.h is not included and I'm not sure it can be.

  1. re-ordered commits to fix build error

@egavrin ,
ok, three separate commits as in this pull request.

@ruben-ayrapetyan
Copy link
Contributor

@seanshpark, thank you for update. Changes look good to me.

Sorry for incorrect comment about type definitions.
I think, later the type definitions would be moved to another header that would be common to the whole engine and API, so the definitions would not be duplicated.

@egavrin
Copy link
Contributor

egavrin commented Jun 8, 2015

Great patch! make push

JerryScript-DCO-1.0-Signed-off-by: SaeHie Park [email protected]
 * dump string literals with test_api to see jerry_register_external_magic_strings is working ok

JerryScript-DCO-1.0-Signed-off-by: SaeHie Park [email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants