[Gambas-user] New feature in the Array classes

Benoît Minisini g4mba5 at gmail.com
Tue Feb 9 21:12:18 CET 2021


Le 09/02/2021 à 20:54, Tobias Boege a écrit :
> On Tue, 09 Feb 2021, Benoît Minisini wrote:
>> Hi,
>>
>> Following the recent optimizations on the array classes (that apparently
>> made array access about 15% faster), I added a new property named "Sorted"
>> to the array classes.
>>
>> If you set that property to TRUE, it will tell the interpreter that the
>> array is sorted.
>>
>> Then, the Find() and Exist() method will use a faster binary search
>> algorithm instead of a sequential one.
>>
>> But it's up to you to maintain the array sorted. If Array.Sorted is TRUE,
>> and the array is not actually sorted, the result of Find() and Exist() will
>> be false.
>>
> 
> The performance benefit of this will probably be very nice wherever
> it can be reliably exploited.
> 
> But I am a little concerned about Sorted being a property of the object
> instead of an optional argument to the two methods that make use of it.
> For these two related reasons:
> 
>    - If I write a library that uses arrays passed in by the user,
>      I now have to take care to reset the Sorted property (which may
>      have been set by the user earlier) whenever I do an operation
>      which cannot guarantee that the order is maintained. Otherwise
>      my code is potentially buggy. (All code people wrote to this day
>      which modifies arrays and then searches in them is now potentially
>      buggy, because they don't reset Sorted.)
> 
>    - Moreover, there is an API breakage even when you use arrays only
>      in a read-only fashion. When someone passes into my trusty Gambas
>      library written for the stable v3.12, which is still working fine
>      otherwise, an array with Sorted = True, of which my old code is
>      unaware, and I call Find() on it and compare it to the previously
>      documented value -1 indicating "not found", my code is now buggy,
>      as in Sorted mode I have to compare with < 0 to see if the value
>      was not found.
> 
> These wouldn't be problems if the *caller* of the method could opt into
> binary search, instead of everyone who had a reference to the object at
> some point in the program's lifetime making that choice for code that
> is unaware of that feature.
> 
> Best,
> Tobias
> 

You are right, Array.Sorted is a bad idea. I was concerned of not adding 
too many new methods, but apparently it is unavoidable!

-- 
Benoît Minisini


More information about the User mailing list