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

int16_t is not enough and better string implementation needed #62

Open
Naheel-Azawy opened this issue Jan 3, 2020 · 1 comment
Open

Comments

@Naheel-Azawy
Copy link

This sounds like multiple issues but they are all related. For the following js code:

var s = "";
for (var i = 0; i < 10000; ++i) {
    s += i;
}
console.log(s.length);
  1. The output of the compiled c code is -26646.
  2. If 10000 gets changed to 100000... Assertion 'gc_main->data != NULL' failed....

Both these issues are because int16_t is used (aka short) which is obviously not enough.

  1. However, if something like unsigned long is used, too many allocations will be done and from my experience my system (Arch Linux with 8gb of ram) froze...

I think a solution to this will be first using a something like size_t instead of int16_t. Using size_t is better I guess so that the compiler deals with it and 16-bit microcontrollers stay happy (issue #41).
Then, a better mini string implementation should be done. Many libraries already exist out there. Also C++ implementation does pretty well which I assume can be cloned to C. The following works pretty well:

int main() {
  string s = "";
  for (int i = 0; i < 1000000; ++i) {
    s += to_string(i);
  }
  cout << s << endl;
}
@andrei-markeev
Copy link
Owner

andrei-markeev commented Jan 4, 2020

Yes, good points!

There will be a switch at some point so that it is easy to change int16_t to int32_t or int64_t. @pitust has tried changing everything to int64_t and was working fine for his scenario, so I hope this can be solved without much problems: #26 (comment)

Regarding the string implementation: yes, right now strings are const char *. Even though your example is not very realistic, I agree that current implementation is certainly not the best option if there is a massive amount of string operations. So maybe we can detect these cases and use char * instead, preallocated to bigger capacity similarly to how arrays are implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants