IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

Vous êtes nouveau sur Developpez.com ? Créez votre compte ou connectez-vous afin de pouvoir participer !

Vous devez avoir un compte Developpez.com et être connecté pour pouvoir participer aux discussions.

Vous n'avez pas encore de compte Developpez.com ? Créez-en un en quelques instants, c'est entièrement gratuit !

Si vous disposez déjà d'un compte et qu'il est bien activé, connectez-vous à l'aide du formulaire ci-dessous.

Identifiez-vous
Identifiant
Mot de passe
Mot de passe oublié ?
Créer un compte

L'inscription est gratuite et ne vous prendra que quelques instants !

Je m'inscris !

PureBasic 6.40 alpha 1 est disponible
Avec une surprise !

Le , par comtois

0PARTAGES

4  1 
Hi everyone !

That's it, we finally decided to rework the internal stringmanager from the ground up ! The stringmanager hasn't changed since the PureBasic
creation almost 30 years ago, so it wasn't an easy decision to make because major modification for the compilers were needed and all the functions returning a string were impacted. But it's here with the 6.40 alpha 1, only available for the C back-end on Windows x64. The idea is to validate the new stringmanager to see if there is any showstopper before porting the modification to the x86 and x64 asm compilers which will takes time.

I will share some internal information about it:

- It should be 99% backward compatible. We will discuss edge case below.

- All strings are now prefixed by their length (except the strings in DataSection). The last bit of this prefix is used for a temporary flag.
That means than all string operation will have instant length information resulting in faster operation as iterating byte by byte on the string to find the null terminated char is no more needed.
The strings are still null terminated, so no change are needed in client code.

- Still no garbage collector

- The string manager doesn't have a per thread shared buffer anymore for temporary strings. That means there is no global variable to save the current position and the thread mode will have no performance hit. All is now done as new temporary string which will be used as final string if assigned. That removes useless string copy and is much more streamlined.

Example:

Code : Sélectionner tout
a$ = b$+c$
Old:
Code : Sélectionner tout
1
2
3
4
SYS_PushStringBasePosition(); // current position in the shared buffer is saved (costly in threaded mode)
SYS_CopyString(v_bS); // string is copied on the per thread shared buffer 
SYS_CopyString(v_cS);
SYS_AllocateString4(&v_aS,SYS_PopStringBasePosition()); // Position is restored and string is allocated

New:
Code : Sélectionner tout
1
2
s1=SYS_ConcatString(v_cS,v_bS); // A temporary string is created here
SYS_NewString(&v_aS,s1); // The temporary string is directly used (temporary bit removed)

It's a very basic case, but it shows the improvement by avoiding useless string copy and global variable preservation

- Optimized procedure parameters passing. If a string parameter isn't modified in the procedure, it will not be duplicated in the procedure. For example

Code : Sélectionner tout
1
2
3
Procedure.s a(a$, b$)
  ProcedureReturn UCase(a$) + LCase(b$)
EndProcedure
Here the a$ and b$ are just referenced and won't be duplicated. In the older system, all the string parameters were duplicated.

Even better, the procedure 'a()' will return a temporary string which will be directly assigned to Result$ (no extra allocation/copy):

Code : Sélectionner tout
Result$ = a("Hello", "World")
Here are the case where the parameters will be duplicated

Code : Sélectionner tout
1
2
3
4
5
6
7
8
9
Procedure.s a(a$, b$)
  a$ = "-"+a$ ; Here, the a$ will be duplicated at procedure start, as it's modified.
  ProcedureReturn UCase(a$) + LCase(b$)
EndProcedure

Procedure.s a(a$, b$)
  *Cursor = @a$ ; Will also be duplicated as getting the pointer allow modification
  ProcedureReturn UCase(a$) + LCase(b$)
EndProcedure
- The string functions don't have an hidden parameter anymore, allowing better code generation

c$ = Str(150)

Old:

Code : Sélectionner tout
1
2
3
4
SYS_PushStringBasePosition();
SYS_PushStringBasePosition();
PB_Str(150LL,SYS_PopStringBasePosition());
SYS_AllocateString4(&v_cS,SYS_PopStringBasePosition());
New:

Code : Sélectionner tout
1
2
void *r5=PB_Str(150LL);
SYS_NewString(&v_cS,r5);
- For PureBasic function returning a string, it's possible to tag a parameter as 'reusable' to avoid extra allocation when a temporary string is passed. For now, only LCase() and UCase() uses this, but it greatly improves their performances:

Ex:

Code : Sélectionner tout
a$ = UCase(b$ + c$)
The allocation is done when concatenating 'b$' + 'c$', then the UCase() function directly change the buffer with upper case character and returns the same buffer which is assigned to a$. So only one allocation occurred and no extra copy were performed.

- The 'mimalloc' memory manager has been integrated in PureBasic for all dynamic allocation to have a very fast and optimized memory allocator for all internal functions. More info here: https://github.com/microsoft/mimalloc

- Overall the performances should be much better, but there will be probably some case were it won't ! Feel free to test it and bench it with your own code (my speed test suite show major improvement (up to 10x) on most tests). For example this one is 9x faster:

Code : Sélectionner tout
1
2
3
4
5
6
7
8
Start = ElapsedMilliseconds()
a$ = "World WorldWorld WorldWorld WorldWorld WorldWorld WorldWorld WorldWorld WorldWorld WorldWorld WorldWorld WorldWorld WorldWorld WorldWorld WorldWorld WorldWorld WorldWorld WorldWorld WorldWorld WorldWorld WorldWorld WorldWorld WorldWorld WorldWorld WorldWorld World"
For l = 0 To 1000
  b$ = b$ + a$ 
Next

PrintN("Final length = " + Str(Len(b$)))
PrintN("Large concat: "+Str(ElapsedMilliseconds() - Start) + " ms")
- Edge cases:

We tried to do the migration as painless as possible, but there is some case where code change will be required. Basically if you patch a string by putting a zero in it, the Len() function will be wrong, and the concat functions will fail. You will need to use PeekS() for this.
You can take a look to the needed modification for the IDE to work with the new stringmanager, it's very light: https://github.com/fantaisie-software/p ... /344/files

Using Win32 API with Space() for example will require an extra PeekS(). If you find other issues, don't hesitate to share it, so I can take a look if it can be addressed or not.

We hope this major rework will increase the PureBasic app efficiency. Don't hesitate to test it but don't use this build in prod, it's indeed not production ready yet !

Have fun,

The Fantaisie Software Team

Source de l'information
Vous avez lu gratuitement 2 291 articles depuis plus d'un an.
Soutenez le club developpez.com en souscrivant un abonnement pour que nous puissions continuer à vous proposer des publications.

Une erreur dans cette actualité ? Signalez-nous-la !