Closed GavinRay97 closed 1 year ago
The way you are supposed to do it is to have an empty array and resize it:
p = arrayof(u32).resize(4096)
And from what I can tell there's nothing terribly inefficient about it (https://github.com/google/rune/discussions/35#discussion-4671934)
define internal void @array() !dbg !240 {
%.tmp1 = alloca %struct.runtime_array
store %struct.runtime_array zeroinitializer, %struct.runtime_array* %.tmp1
; statement 0x3dd assignment p = arrayof(u32).resize(4096u64)
call void @runtime_freeArray(%struct.runtime_array* %.tmp1)
call void @runtime_resizeArray(%struct.runtime_array* %.tmp1, i64 4096, i64 4, i1 zeroext 0), !dbg !241
call void @runtime_moveArray(%struct.runtime_array* @array_p, %struct.runtime_array* %.tmp1), !dbg !241
; statement 0x3e5 return
ret void, !dbg !241
}
Which in the end quickly comes down to calling runtime_allocArray
Ah okay, so it is more like Python/Javascript than a systems language in this regard if I understand it?
There is no notion of "fixed-size type", there is just an Array
type which has a length
property
The language is not complete yet, so some optimisations may be missed. It is supposed to be systems programming language.
From what I can see there's no fixed-size array, but why do you need it? It's not more efficient than one allocation of an array of the needed size
For two reasons:
Now instead of it being a type-level constraint, you must move this sort of information and validation into userland. This means you have a have few choices as a user:
[u32]
to make sure it's the right length as you pass it across your functions that expect something like [u32; 256]
[u32]
, guaranteeing that it has been validated to be the right length for the method. See: https://www.foonathan.net/2022/11/proof-typesI thought about it and I think that if you need to specify a certain number of elements of the same type there's two cases:
This deals with the unnecessary checks problems
Now we can see more clearly that through "lossy encoding" compiler misses out on some optimisations for large number of elements, although the gains are probably rather not significant
I couldn't come up with an example where you really need to have a certain number of elements, maybe you have one?
I see there is a method
allocArrayBuffer
in the runtime, but looking through every test in/tests
and/newtests
, I couldn't find some usage of code like:I tried the below on a guess, but this was not valid rune: