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

js: times.now generates 1193 LOC (4.5K in debug mode) #503

Open
timotheecour opened this issue Dec 31, 2020 · 3 comments
Open

js: times.now generates 1193 LOC (4.5K in debug mode) #503

timotheecour opened this issue Dec 31, 2020 · 3 comments
Assignees
Labels

Comments

@timotheecour
Copy link
Owner

timotheecour commented Dec 31, 2020

js: times.now generates 1193 LOC in -d:danger (4.5K LOC in debug mdoe)

Example

import times
echo now()

Current Output

nim r -b:js $options main
wc -l main.js

  • debug mode: 4521
  • -d:release: 1480
  • -d:danger: 1193

Expected Output

small codegen

Additional Information

1.5.1 devel b42e7c0

related issues (js code bloat)

when true:
  import tables
  let x = initTable[string, int]()
  echo x

-d:danger: 549 LOC

@timotheecour timotheecour changed the title js: times.now generates 4.5K LOC js: times.now generates 1193 LOC (4.5K in debug mode) Dec 31, 2020
@ringabout ringabout self-assigned this Dec 31, 2020
@ringabout
Copy link
Collaborator

ringabout commented Aug 16, 2021

After changing to if else, times.now generates more lines :)

1272 => 1293

After minifying, it reduces 1kb or less

24kb => 23kb

Edited:
Not finished, jsgen should traverse the case statement first finding the number of the range, then decide whether to generate case or if else.

@timotheecour
Copy link
Owner Author

After changing to if else, times.now generates more lines :)

what do you mean by that? some details would clarify

also, make sure you measure for -d:danger

contiguous case branches like this:

    case 26:
    case 27:
    case 28:
    case 29:
    case 30:
    case 31:

should be replaced by range checks, eg:
if x >= 26 and x <= 31: ...

@ringabout
Copy link
Collaborator

ringabout commented Aug 16, 2021

I doubt times.now can generate much smaller code. It is compact enough. Please use minify(half of the original size) or closure compiler to get smaller code.

I just change the case statement to if else

if ((Temporary1 == 7)) {
    s_33557848[s_33557848_Idx].push.apply(s_33557848[s_33557848_Idx], makeNimstrLit("\\a"));;
    }
    else {
    if ((Temporary1 == 8)) {
    s_33557848[s_33557848_Idx].push.apply(s_33557848[s_33557848_Idx], makeNimstrLit("\\b"));;
    }
    else {
    if ((Temporary1 == 9)) {
    s_33557848[s_33557848_Idx].push.apply(s_33557848[s_33557848_Idx], makeNimstrLit("\\t"));;
    }
    else {
    if ((Temporary1 == 10)) {
    s_33557848[s_33557848_Idx].push.apply(s_33557848[s_33557848_Idx], makeNimstrLit("\\n"));;
    }
    else {
    if ((Temporary1 == 11)) {
    s_33557848[s_33557848_Idx].push.apply(s_33557848[s_33557848_Idx], makeNimstrLit("\\v"));;
    }
    else {
    if ((Temporary1 == 12)) {
    s_33557848[s_33557848_Idx].push.apply(s_33557848[s_33557848_Idx], makeNimstrLit("\\f"));;
    }
    else {
    if ((Temporary1 == 13)) {
    s_33557848[s_33557848_Idx].push.apply(s_33557848[s_33557848_Idx], makeNimstrLit("\\r"));;
    }
    else {
    if ((Temporary1 == 27)) {
    s_33557848[s_33557848_Idx].push.apply(s_33557848[s_33557848_Idx], makeNimstrLit("\\e"));;
    }
    else {
    if ((Temporary1 == 92)) {
    s_33557848[s_33557848_Idx].push.apply(s_33557848[s_33557848_Idx], makeNimstrLit("\\\\"));;
    }
    else {
    if ((Temporary1 == 39)) {
    s_33557848[s_33557848_Idx].push.apply(s_33557848[s_33557848_Idx], makeNimstrLit("\\\'"));;
    }
    else {
    if ((Temporary1 == 34)) {
    s_33557848[s_33557848_Idx].push.apply(s_33557848[s_33557848_Idx], makeNimstrLit("\\\""));;
    }
    else {
    if (((Temporary1 >= 32) & (Temporary1 <= 33)) | ((Temporary1 >= 35) & (Temporary1 <= 38)) | ((Temporary1 >= 40) & (Temporary1 <= 91)) | ((Temporary1 >= 93) & (Temporary1 <= 126))) {
    addChar(s_33557848[s_33557848_Idx], c_33557849);;
    }

Before

    switch (c_33557845) {
    case 7:
      s_33557844[s_33557844_Idx].push.apply(s_33557844[s_33557844_Idx], makeNimstrLit("\\a"));;
      break;
    case 8:
      s_33557844[s_33557844_Idx].push.apply(s_33557844[s_33557844_Idx], makeNimstrLit("\\b"));;
      break;
    case 9:
      s_33557844[s_33557844_Idx].push.apply(s_33557844[s_33557844_Idx], makeNimstrLit("\\t"));;
      break;
    case 10:
      s_33557844[s_33557844_Idx].push.apply(s_33557844[s_33557844_Idx], makeNimstrLit("\\n"));;
      break;
    case 11:
      s_33557844[s_33557844_Idx].push.apply(s_33557844[s_33557844_Idx], makeNimstrLit("\\v"));;
      break;
    case 12:
      s_33557844[s_33557844_Idx].push.apply(s_33557844[s_33557844_Idx], makeNimstrLit("\\f"));;
      break;
    case 13:
      s_33557844[s_33557844_Idx].push.apply(s_33557844[s_33557844_Idx], makeNimstrLit("\\r"));;
      break;
    case 27:
      s_33557844[s_33557844_Idx].push.apply(s_33557844[s_33557844_Idx], makeNimstrLit("\\e"));;
      break;
    case 92:
      s_33557844[s_33557844_Idx].push.apply(s_33557844[s_33557844_Idx], makeNimstrLit("\\\\"));;
      break;
    case 39:
      s_33557844[s_33557844_Idx].push.apply(s_33557844[s_33557844_Idx], makeNimstrLit("\\\'"));;
      break;
    case 34:
      s_33557844[s_33557844_Idx].push.apply(s_33557844[s_33557844_Idx], makeNimstrLit("\\\""));;
      break;
    case 32:
    case 33:
    case 35:
    case 36:
    case 37:
    case 38:
    case 40:
    case 41:
    case 42:
    case 43:
    case 44:
    case 45:
    case 46:
    case 47:
    case 48:
    case 49:
    case 50:
    case 51:
    case 52:
    case 53:
    case 54:
    case 55:
    case 56:
    case 57:
    case 58:
    case 59:
    case 60:
    case 61:
    case 62:
    case 63:
    case 64:
    case 65:
    case 66:
    case 67:
    case 68:
    case 69:
    case 70:
    case 71:
    case 72:
    case 73:
    case 74:
    case 75:
    case 76:
    case 77:
    case 78:
    case 79:
    case 80:
    case 81:
    case 82:
    case 83:
    case 84:
    case 85:
    case 86:
    case 87:
    case 88:
    case 89:
    case 90:
    case 91:
    case 93:
    case 94:
    case 95:
    case 96:
    case 97:
    case 98:
    case 99:
    case 100:
    case 101:
    case 102:
    case 103:
    case 104:
    case 105:
    case 106:
    case 107:
    case 108:
    case 109:
    case 110:
    case 111:
    case 112:
    case 113:
    case 114:
    case 115:
    case 116:
    case 117:
    case 118:
    case 119:
    case 120:
    case 121:
    case 122:
    case 123:
    case 124:
    case 125:
    case 126:
      addChar(s_33557844[s_33557844_Idx], c_33557845);;
      break;
    default: 
      s_33557844[s_33557844_Idx].push.apply(s_33557844[s_33557844_Idx], makeNimstrLit("\\x"));;
      var n_33557855 = c_33557845;
      addChar(s_33557844[s_33557844_Idx], makeNimstrLit("0123456789ABCDEF")[((n_33557855 & 240) >> 4)]);;
      addChar(s_33557844[s_33557844_Idx], makeNimstrLit("0123456789ABCDEF")[(n_33557855 & 15)]);;
      break;

I may optimize it later.

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

No branches or pull requests

2 participants