Issue Details (XML | Word | Printable)

Key: STDCXX-723
Type: Sub-task Sub-task
Status: Open Open
Priority: Minor Minor
Assignee: Unassigned
Reporter: Martin Sebor
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
C++ Standard Library
STDCXX-544

[gcc] use __builtin_prefetch to optimize string

Created: 12/Feb/08 02:08 AM   Updated: 23/Apr/08 12:32 AM
Return to search
Component/s: 21. Strings
Affects Version/s: 4.1.2, 4.1.3, 4.1.4, 4.2.0
Fix Version/s: 4.2.2

Time Tracking:
Original Estimate: 4h
Original Estimate - 4h
Remaining Estimate: 4h
Remaining Estimate - 4h
Time Spent: Not Specified
Remaining Estimate - 4h


 Description  « Hide
We might be able to use the gcc __builtin_prefetch function in basic_string to give the hardware a hint when a string object's data is about to be accessed (e.g., the reference count which is stored at a negative offset from the basic_string::_C_data member pointer. This could improve performance on modern processors that implement prefetching (e.g., IA-64, x86_64, or PowerPC).

Quoting from section 5.46 Other built-in functions provided by GCC of the gcc manual:

Built-in Function: void __builtin_prefetch (const void *addr, ...)

This function is used to minimize cache-miss latency by moving data into a cache before it is accessed. You can insert calls to __builtin_prefetch into code for which you know addresses of data in memory that is likely to be accessed soon. If the target supports them, data prefetch instructions will be generated. If the prefetch is done early enough before the access then the data will be in the cache by the time it is accessed.

The value of addr is the address of the memory to prefetch. There are two optional arguments, rw and locality. The value of rw is a compile-time constant one or zero; one means that the prefetch is preparing for a write to the memory address and zero, the default, means that the prefetch is preparing for a read. The value locality must be a compile-time constant integer between zero and three. A value of zero means that the data has no temporal locality, so it need not be left in the cache after the access. A value of three means that the data has a high degree of temporal locality and should be left in all levels of cache possible. Values of one and two mean, respectively, a low or moderate degree of temporal locality. The default is three.

for (i = 0; i < n; i++)
{
    a[i] = a[i] + b[i];
    __builtin_prefetch (&a[i+j], 1, 1);
    __builtin_prefetch (&b[i+j], 0, 1);
    /* ... */
}

Data prefetch does not generate faults if addr is invalid, but the address expression itself must be valid. For example, a prefetch of p->next will not fault if p->next is not a valid address, but evaluation will fault if p is not a valid address.

If the target does not support data prefetch, the address expression is evaluated if it includes side effects but no other code is generated and GCC does not issue a warning.



 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Martin Sebor added a comment - 12/Feb/08 02:33 AM
The gcc i386-prefetch.exp test suite file contains some info on when __builtin_prefetch is useful.

Martin Sebor added a comment - 23/Apr/08 12:32 AM
Deferred until 4.2.2.

Martin Sebor made changes - 23/Apr/08 12:32 AM
Field Original Value New Value
Fix Version/s 4.2.1 [ 12312690 ]
Fix Version/s 4.2.2 [ 12313096 ]