Skip to content

Potentially eliminating Array.create<T> #853

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

Closed
dcodeIO opened this issue Sep 19, 2019 · 4 comments
Closed

Potentially eliminating Array.create<T> #853

dcodeIO opened this issue Sep 19, 2019 · 4 comments

Comments

@dcodeIO
Copy link
Member

dcodeIO commented Sep 19, 2019

So I've been thinking about the new Array<T>(10) problematic a bit, and I figured that it might be worth to not throw on creating a holey array, but to instead throw when accessing a holey value. For instance, currently

class Foo {}
var arr = new Array<Foo>(10); // throws

but this could be modified to

class Foo {}
var arr = new Array<Foo>(10);
arr[0]; // throws

making

class Foo {}
var arr = new Array<Foo>(10);
for (let i = 0; i < 10; ++i) arr[i] = new Foo();
arr[0]; // work

at the expense of an additional runtime check whether a value is null on element access, if the array's value type is non-nullable. Assuming that branch prediction will do fine there since actually reading a null is unlikely and only happens in error cases. Thoughts?

@bowenwang1996
Copy link
Contributor

Maybe we can do some benchmark to see how much performance loss this new approach would bring, but I think from the usability perspective this is certainly worth considering.

@MaxGraey
Copy link
Member

Great idea. But I guess ut should be the same "null-ownership" analysis as in #456

@dcodeIO
Copy link
Member Author

dcodeIO commented Sep 20, 2019

Turns out that there were two length-checks in Array#__get in the reference case already, which remains the same after the change. This is because the previous code tried to allow accessing out of .length while still inside .dataLength, which I think was wrong anyway in that one could theoretically unburry a reference that was already released or even collected. As such it seems that, comparing to the previous faulty code, we are not paying a tax.

@dcodeIO
Copy link
Member Author

dcodeIO commented Sep 23, 2019

This is merged now, with updated documentation here: https://docs.assemblyscript.org/standard-library/array#api

@dcodeIO dcodeIO closed this as completed Sep 23, 2019
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

3 participants