[Gambas-user] Fixed arrays still dynamic
Tobias Boege
taboege at ...626...
Tue Oct 6 00:32:23 CEST 2015
On Mon, 05 Oct 2015, Moviga Technologies wrote:
>
> Ah, yes - it slipped my mind that inline arrays instantiates a new
> object.
> My example should rather have been:
>
> Dim aCarBrands As New String[5]
>
> With aCardBrands
> .Add("Volvo")
> .Add("Saab")
> .Add("Fiat")
> .Add("Aston Martin")
> .Add("Skoda")
> .Add("VW")
> .Add("Audi")
> End With
>
> So, the (only) purpose of giving it a predefined size is to reserve the
> memory in advance? sCarBrands[] is 0 byte while sCarBrands[4] is 16
> bytes? Will the latter be faster?
Uhh, probably... The Gambas array implementation could overallocate memory
in powers of the golden ratio (I don't know if it does) or some other obs-
cure stuff which might make the entirely dynamic allocation equally fast
for certain (small) sizes. But in general, if you know the size in advance,
you should use that information.
And if we're talking about internals here, your numbers are certainly
incorrect. Gambas needs at least some bookkeeping information for each
variable.
> So am I right then to say that there are no fixed arrays in Gambas, but
> all are dynamic. It is just that some have preallocated memory? Or how
> about Dim sCarBrands As New String[2, 2]
> is this fixed? I cannot seem to find a way to add more to this array
> dynamically.
>
Multidimensional arrays are another thing. I have discussed them countless
times already. See the references I give in my mail here[0] for example.
>
> You mean you prefer this Dim aCarBrands As New String[](5) notation for
> semantic reasons? No technical advantages?
>
Yes, they do absolutely the same. Consider the code:
Public Sub Main()
Dim a As New String[5]
End
Public Sub Main2()
Dim b As New String[](5)
End
then you compile it like this:
$ gbc3 -av
...
Compiling Main()...
Switching class String[] to Used
LOCAL a AS Object
0000 : B001 PUSH CLASS String[]
0001 : F005 PUSH QUICK 5
0002 : 1B42 NEW ARRAY (2)
0003 : 0900 POP LOCAL 0
0004 : 1002 RETURN (2)
1 local(s) 0 control(s) 3 stack
Compiling Main2()...
LOCAL b AS Object
0000 : B001 PUSH CLASS String[]
0001 : F005 PUSH QUICK 5
0002 : 1B02 NEW (2)
0003 : 0900 POP LOCAL 0
0004 : 1002 RETURN (2)
...
This is the generated byte code. They only differ in the "ARRAY" flag which
is apparently set when you use the New String[5] syntax. A quick grep showed
that this flag is currently unused (unless I overlooked something).
Regards,
Tobi
[0] http://gambas.8142.n7.nabble.com/Array-Add-question-td52019.html
--
"There's an old saying: Don't change anything... ever!" -- Mr. Monk
More information about the User
mailing list