--
You received this message because you are subscribed to the Google Groups "Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@arduino.cc.
On 21 April 2016 at 17:56:00, William Westfield (wes...@mac.com) wrote:
. Enhancing a feature widely believed to not work very well seems
I do think it adds too many methods and it might make the code become a bit too complicated to understand.
--
(How many realloc() operations does that do on a 50-character “line”? Might be 50. Might be a more reasonable 4 or 5. It just bugs me not to KNOW.)
Am Freitag, 22. April 2016 18:40:16 UTC+2 schrieb Bill Westfield:
(How many realloc() operations does that do on a 50-character “line”? Might be 50. Might be a more reasonable 4 or 5. It just bugs me not to KNOW.)
You could get around all that reallocations if you could preset the size for the internal buffer.
We'd simply have to overload the constructor of the class to solve at least the one problem you mentioned.
I am gonna have a look at that later when I'm home from work.
yes, like this, but this function just re-allocates an existing buffer with new size. It should be part of the constructor, so that the first allocation already sets the buffer to a desired size.
Also, I think that the buffer should not be increased by the amount of needed extra space. It should be doubled always. This behaviour is also used in .net (C#) StringBuilder's internal buffer. It is always doubled, because the developers assumed that if a buffer already has a large size (due to large strings being added), this way of operation will continue and more large strings will be added, therefore a doubled buffer makes sense.
It may not make sense do double on MCU, so I suggest adding 25% or 50% of the current size if the buffer is exhausted.
If this is not desired, then a parameter could be added in the string class which explicitly enables this behaviour.
Here I think that we could add a public parameter to the String class that defines the percentage of reserved buffer size on each created string instance and for each resize.
By default this value is 0, unit is percent. If someone frequently concats Strings, he should increase this value to whatever he likes in order to reduce the number of buffer reallocations.
This is independent from the first idea and will help for sure, so I would definitely implement this.
I use my own malloc, which lacks 'issues' -- i actually include it to override the provided one on avr — mainly because it is ISR safe. It may also not have any of the bugs seen/triggered.
--
> String has 13 different constructors. Overloading all of them just for buffer preallocation is not a sensible option.
Hmm. Why not? Isn’t it essentially part of the C++ “style” to hide “ugliness” in standard classes instead of directly exposing the user? Unused code gets optimized away…
> Adding a public static member that holds the default buffer size will lead to very undesired behaviour and is not an option either.
Can’t you just add a default “size = 10” (or whatever) to each of the existing constructors?