L'ingegnere di Intel, Noah Goldstein, ha ottimizzato la funzione memset() nella libreria glibc. Questa ottimizzazione offre un guadagno in termini di prestazioni di circa il 7,5% sulle versioni desktop dei processori delle architetture Skylake-X e Ice Lake. Per le versioni server, il guadagno delle prestazioni è leggermente inferiore, principalmente a causa della performance complessiva più bassa di un singolo core.
Nella precedente implementazione della funzione memset() veniva utilizzata l'istruzione assembler rep stosb. Fino a poco tempo fa, questa istruzione funzionava piuttosto velocemente grazie all'ottimizzazione intra-processore zero-over-zero writeback. Tuttavia, è stata trovata una vulnerabilità potenziale in questa ottimizzazione che potrebbe portare a un attacco attraverso canali laterali. Di conseguenza, l'ottimizzazione zero-over-zero writeback è stata annullata, portando a un degrado delle prestazioni di rep stosb. Nella nuova versione di memset(), l'istruzione rep stosb viene ancora utilizzata, ma in condizioni più rigorose.
Cosa sia cambiato può essere compreso osservando la modifica del seguente commento nel codice, che descrive i dettagli dell'implementazione di memset()
Versione precedente della descrizione:
/* memset is implemented as: 1. Use overlapping store to avoid branch. 2. If size is less than VEC, use integer register stores. 3. If size is from VEC_SIZE to 2 * VEC_SIZE, use 2 VEC stores. 4. If size is from 2 * VEC_SIZE to 4 * VEC_SIZE, use 4 VEC stores. 5. On machines ERMS feature, if size is greater or equal than __x86_rep_stosb_threshold then REP STOSB will be used. 6. If size is more to 4 * VEC_SIZE, align to 4 * VEC_SIZE with 4 VEC stores and store 4 * VEC at a time until done. */
Nuova versione della descrizione:
/* memset is implemented as: 1. Use overlapping store to avoid branch. 2. If size is less than VEC, use integer register stores. 3. If size is from VEC_SIZE to 2 * VEC_SIZE, use 2 VEC stores. 4. If size is from 2 * VEC_SIZE to 4 * VEC_SIZE, use 4 VEC stores. 5. If size is more to 4 * VEC_SIZE, align to 1 * VEC_SIZE with 4 VEC stores and store 4 * VEC at a time until done. 6. On machines ERMS feature, if size is range [__x86_rep_stosb_threshold, __x86_memset_non_temporal_threshold) then REP STOSB will be used. 7. If size >= __x86_memset_non_temporal_threshold, use a non-temporal stores. */
Fonte: linux.org.ru
