google / rune

Rune is a programming language developed to test ideas for improving security and efficiency.
Apache License 2.0
1.91k stars 46 forks source link

Fixed-size array allocation and fixed-size types #37

Closed GavinRay97 closed 1 year ago

GavinRay97 commented 1 year ago

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:

std::byte buffer[4096];

I tried the below on a guess, but this was not valid rune:

myArray = [u32; 4096]
0dminnimda commented 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

GavinRay97 commented 1 year ago

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

0dminnimda commented 1 year ago

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

GavinRay97 commented 1 year ago

For two reasons:

  1. You lose the type-soundness of being able to specify "this a function that a buffer/array of a specific size"

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:


  1. From the language/compiler perspective, there is less information in the type preserved than the user had initially provided it with. In other words, we suffer from a "lossy encoding" of information.
0dminnimda commented 1 year ago

I 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?