-
-
Notifications
You must be signed in to change notification settings - Fork 671
Implement StaticArray<T> as a way to have static data #1122
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
Conversation
|
From looking at the code, while const readonlyArray = [1,2,3] as ReadonlyArray<T>;
const fixedArray = [1,2,3] as FixedArray<T>; respectively const readonlyArray: ReadonlyArray<T> = [1,2,3];
const fixedArray: FixedArray<T> = [1,2,3]; which, to my surprise, seems to type-check just fine since these classes are structure compatible to |
Yeah, |
btw |
For some reason |
You're right it should be |
So it seems that
From a purist perspective, both of these do not map well to AssemblyScript, which makes me even more favor something explicit like |
Yes, "readonly" and |
In TS: const arr1 = [1, 2, 3] as ReadonlyArray<number>; // infers as "readonly number[]"
const arr2 = [1, 2, 3] as const; // infers as "readonly [1, 2, 3]" tuple |
Yeah, that |
Alright, most likely unpopular conclusion: I have a feeling that if we force Hence I removed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall LGTM.
I'm wondering when we will have own lang server could we use
[1,2,3] as fixed;
instead
[1,2,3] as fixed<i32>;
wdyt?
So the final decision is with |
We can use |
The more I look at |
There is more than just size this tries to solve. To me it's even more important that it gets rid of the additional layer of indirection that comes from normal arrays being resizable, and by doing so also gets rid of unnecessary globals and locals dealing with Perhaps another name than
|
Yeah, FixedArray is similar to "slice" type in other languages (including Rust) and it totally makes sense but not sure as explicit data type but rather, as an implicit internally optimizing view for ordinal arrays, hmm... But this require sophisticated optimizations and decisions which that we do not yet have. So maybe we can stop at FixedArray as a compromise solution. |
My vote is in for Is it possible to make a class both Unmanaged and managed at the same time? Maybe we could compile the static data with
|
And... Maybe forgive me for suggesting crazy, but now that we have package management, maybe someone could just implement FastArray in their own package? 😊 |
Also possible names:
I also like |
|
const arr = [[1, 2], [3, 4]] as StaticArray<StaticArray<i32>>; vs const arr = [[1, 2], [3, 4]] as Seq<Seq<i32>>; |
Would prefer |
Okay, so let's call it StaticArray =) |
Btw Typescript has |
interface for ConcatArray |
Has similar problems like |
On TS function foo(...args: ConcatArray<i32>) {
} So it basically |
It's for example used by TS to describe the argument to |
TS don't mix Array & ConcatArray. |
But TS would be fine with a user providing our variant of |
I guess |
I'll go ahead now and rename this abomination of an array to |
sure. It was just suggestions and small brainstorm for alternative point of view |
Updated the opening post with what I hope is a good summary of the choices made :) |
This PR adds a more efficient variant of arrays, named
StaticArray<T>
to the standard library that, unlike normal arrays, does not have a level of indirection, hence.dataStart
ReadonlyArray<T>
in TSArray<T>
,ReadonlyArray<T>
orConcatArray<T>
in TS due to a different layout in memory, hence uses a new nameFrom a technical point of view, one can think of it as a raw
ArrayBuffer
with an attached element type. It's especially useful in low-level code like our standard math library where it eliminates most of the overhead introduced by having no other option than usingArray<T>
for precomputed lookup tables. There are some other use cases benefiting from it, like the n-body example where getting rid of the level of indirection yields overall better performance, that otherwise can only be achieved by resorting to low-level loads and stores and caching.dataStart
.Also somewhat related to #1121 in that we have more options now how to go forward with
.dataStart
, potentially replacing it with.buffer + .byteOffset
to match the spec more closely.