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

array literals uses typed arrays; fix a jsgen bug #16850

Merged
merged 19 commits into from
Feb 15, 2021
Merged

Conversation

ringabout
Copy link
Member

Ref timotheecour#557

when true:
  proc main()=
    var b: array[10, byte]
    var b2 = [byte(1), 2]
    doAssert (b, b2) == ([byte(0), 0, 0, 0, 0, 0, 0, 0, 0, 0], [byte(1), 2])
  main()

Before this PR:

function main_369098753() {
  var b_369098754 = new Uint8Array(10);
  var b2_369098755 = [1, 2];
  if (!(HEX3DHEX3D_369098758({ Field0: nimCopy(null, b_369098754, NTI369098758), Field1: nimCopy(null, b2_369098755, NTI369098759) }, { Field0: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], Field1: [1, 2] }))) {
    failedAssertImpl_218103865(makeNimstrLit("play.nim(5, 14) `(b, b2) == ([byte(0), 0, 0, 0, 0, 0, 0, 0, 0, 0], [byte(1), 2])` "));
  }
}
main_369098753();

After

function main_369098753() {
  var b_369098754 = new Uint8Array(10);
  var b2_369098755 = new Uint8Array([1, 2]);
  if (!(HEX3DHEX3D_369098758({ Field0: nimCopy(null, b_369098754, NTI369098758), Field1: nimCopy(null, b2_369098755, NTI369098759) }, { Field0: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), Field1: new Uint8Array([1, 2]) }))) {
    failedAssertImpl_218103865(makeNimstrLit("play.nim(5, 14) `(b, b2) == ([byte(0), 0, 0, 0, 0, 0, 0, 0, 0, 0], [byte(1), 2])` "));
  }
}
main_369098753();

@ringabout ringabout closed this Jan 28, 2021
@ringabout ringabout reopened this Jan 28, 2021
@ringabout ringabout marked this pull request as draft January 28, 2021 09:21
@ringabout ringabout marked this pull request as ready for review January 28, 2021 10:59
compiler/jsgen.nim Outdated Show resolved Hide resolved
Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
@timotheecour
Copy link
Member

timotheecour commented Jan 29, 2021

this needs a test.

  • add this to lib/std/private/jsutils.nim:
proc jsTypeof*(a: auto): cstring =
  asm """`result` = typeof(`a`)"""

proc jsConstructorName*(a: auto): cstring =
  asm """`result` = `a`.constructor.name"""

(that file exists in devel but somehow not in your PR, which looks like it hasn't been rebased in a while?)

  • and then add this somewhere (find an appropriate existing test file): (feel free to add as needed)
import std/private/jsutils
proc main()=
  template fn(a): untyped = jsConstructorName(a)
  doAssert fn(array[2, int8].default) == "Int8Array"
  doAssert fn(array[2, uint8].default) == "Uint8Array"
  doAssert fn(array[2, byte].default) == "Uint8Array"
  # doAssert fn(array[2, char].default) == "Uint8Array" # xxx fails; bug?
  doAssert fn(array[2, uint64].default) == "Array"
    # pending https://github.com/nim-lang/RFCs/issues/187 maybe use `BigUint64Array`
  doAssert fn([1'u8]) == "Uint8Array"
  doAssert fn([1'u16]) == "Uint16Array"
  doAssert fn([byte(1)]) == "Uint8Array"

main()

@ringabout ringabout changed the title array litterals uses typed arrays array literals uses typed arrays Jan 29, 2021
changelog.md Outdated Show resolved Hide resolved
compiler/jsgen.nim Outdated Show resolved Hide resolved
@ringabout ringabout marked this pull request as draft January 29, 2021 11:58
@ringabout ringabout marked this pull request as ready for review January 30, 2021 04:37
@ringabout ringabout marked this pull request as draft January 30, 2021 04:38
@ringabout ringabout marked this pull request as ready for review January 30, 2021 04:50
@ringabout ringabout marked this pull request as draft January 30, 2021 05:28
@ringabout ringabout marked this pull request as ready for review January 30, 2021 05:31
@ringabout
Copy link
Member Author

ringabout commented Jan 30, 2021

I find a bug

proc hello(): array[5, int] = discard
var x = @(hello())
x.add(2)
echo x

x_369098759[0].push(2);;
^

TypeError: x_369098759[0].push is not a function

@ringabout ringabout changed the title array literals uses typed arrays array literals uses typed arrays; fix a jsgen bug Jan 30, 2021
@timotheecour
Copy link
Member

@xflywind needs rebase

@timotheecour
Copy link
Member

timotheecour commented Feb 3, 2021

I'm still pondering the significance of timotheecour#567, IMO that benchmark i wrote is contrived/not representative but would still be nice to have at least 1 non-contribed benchmark which shows benefit of typed array (but in future work is fine)

bugfix: more type safe even in presence of casts

this PR fixes the following bug; we should add a test either in this PR or in followup PRs:

when true:
  import jsconsole
  template fn(a) =
    echo a
    console.log a
    var x = 257.2
    var b = cast[uint8](x)
    echo b
    a[0] = b
    echo a
    console.log a

  block:
    var a: array[3, uint8]
    a = [2'u8,4,5]
    fn(a)
  block:
    var a: array[3, uint8] = [2'u8,4,5]
    fn(a)

before PR: bug
[2, 4, 5]
Uint8Array(3) [ 2, 4, 5 ]
257
[1, 4, 5]
Uint8Array(3) [ 1, 4, 5 ]
[2, 4, 5]
[ 2, 4, 5 ]
257
[257, 4, 5]
[ 257.2, 4, 5 ]

after PR:
[2, 4, 5]
Uint8Array(3) [ 2, 4, 5 ]
257
[1, 4, 5]
Uint8Array(3) [ 1, 4, 5 ]
[2, 4, 5]
Uint8Array(3) [ 2, 4, 5 ]
257
[1, 4, 5]
Uint8Array(3) [ 1, 4, 5 ]

@Araq
Copy link
Member

Araq commented Feb 3, 2021

Is this still a draft?

@ringabout
Copy link
Member Author

As https://stackoverflow.com/questions/13328658/are-the-advantages-of-typed-arrays-in-javascript-is-that-they-work-the-same-or-s said

This Chrome developer comment provides some guidance that worked as of June 2012:
Normal arrays can be as fast as typed arrays if you do a lot of sequential access. Random access outside the bounds of the array causes the array to grow.
Typed arrays are fast for access, but slow to be allocated. If you create temporary arrays frequently, avoid typed arrays. (Fixing this is possible, but it's low priority.)
Micro-benchmarks such as JSPerf are not reliable for real-world performance.

Typed arrays are fast for access, but slow to be allocated, and normal arrays can be as fast as typed arrays if you do a lot of sequential access. Random access outside the bounds of the array causes the array to grow.

This PR is ready to review.

@ringabout ringabout marked this pull request as ready for review February 3, 2021 08:34
@Araq Araq merged commit 240879b into nim-lang:devel Feb 15, 2021
ardek66 pushed a commit to ardek66/Nim that referenced this pull request Mar 26, 2021
* array litterals uses typed arrays
* Update compiler/jsgen.nim

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
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.

3 participants