Bad performance in String::createStringFromData


#1

I wonder if it would be possible to change

        for (int j = 0; j < i; ++j)
            result += (juce_wchar) (unsigned char) data[j];

into

	result+=String(data);

?

If not, createStringFromData spends really long time if data is very large. This is a big problem when loading large image resources into the jucer on linux.


#2

Ah yes, that would be slow. (hmm - in fact, that’s probably why the linux juce demo is slower to load the SVG images than the windows one)

An even faster version would be something like this:

[code] tchar* const dst = const_cast <tchar*> ((const tchar*) result);

    for (int j = 0; j <= i; ++j)
        dst[j] = (juce_wchar) (unsigned char) data[j];

[/code]


#3

[quote=“jules”]Ah yes, that would be slow. (hmm - in fact, that’s probably why the linux juce demo is slower to load the SVG images than the windows one)

An even faster version would be something like this:

[code] tchar* const dst = const_cast <tchar*> ((const tchar*) result);

    for (int j = 0; j <= i; ++j)
        dst[j] = (juce_wchar) (unsigned char) data[j];

[/code][/quote]

Wow, this really sped up the start up time for jucedemo! :smiley:

I had to make this modification though:

tchar* const dst = const_cast <tchar*> ((const tchar*) result); 
int j;
for (j = 0; j <= i; ++j) 
  dst[j] = (juce_wchar) (unsigned char) data[j];
dst[j]=0;

result=String(dst);

#4

ah yes, I just typed it in without actually trying it. You don’t need the “result =” line at the end though.


#5

whau ! this really speeds up svg loading a lot ! nice one kjetil !


#6

It doesn’t seem to quite work. When saving files in the jucer, it puts 0-4 random-like extra charactors at the end of the file.


#7

You left a “<=” in there. Try this:

[code] String result;
result.preallocateStorage (i + 1);
tchar* const dst = const_cast <tchar*> ((const tchar*) result);

    for (int j = 0; j < i; ++j)
        dst[j] = (juce_wchar) (unsigned char) data[j];

    dst[i] = 0;

    return result;[/code]

#8

[quote=“jules”]You left a “<=” in there. Try this:

[code] String result;
result.preallocateStorage (i + 1);
tchar* const dst = const_cast <tchar*> ((const tchar*) result);

    for (int j = 0; j < i; ++j)
        dst[j] = (juce_wchar) (unsigned char) data[j];

    dst[i] = 0;

    return result;[/code][/quote]

Thanks.


#9