Introduction
Among other things, C99 added the restrict keyword as a way for a programmer to specify that a pointer is the only pointer to a given object in a scope and, consequently, give the compiler a “hint” that it may perform additional optimizations when accessing the object via that pointer.
The Problem
To illustrate the problem that restrict was meant to solve, consider a function like:
void update_ptrs( int *p, int *q, int const *v ) { *p += *v; *q += *v; }
for which the compiler will generate x86-64 code like:
mov eax, [rdx] ; tmp = *v // 1 add [rdi], eax ; *p += tmp mov eax, [rdx] ; tmp = *v // 3 add [rsi], eax ; *q += tmp
You might wonder why it generates line 3 since it seems redundant with line 1. The problem is the compiler can’t know you didn’t do something like this:
int x = 1, v = 2; update_ptrs( &v, &x, &v ); // x = 5, v = 4
In update_ptrs(), p and v would alias the same int, so the compiler has to play it safe and assume that the value of *v can change between reads, hence the additional mov instruction.
In general, pointers in C confound optimization since the compiler can’t know whether two pointers alias each other. In performance critical code, eliding memory reads could be a huge win if the compiler could do it safely.
The Solution
To solve the aforementioned problem, restrict was added to C to allow you specify that a given pointer is the only pointer to an object in the pointer’s scope, i.e., no other pointer in the same scope aliases it.
To use restrict, you insert it between the * and the pointer’s name in a declaration. An update_ptrs() rewritten to use restrict would be:
void update_ptrs_v2( int *restrict p, int *restrict q, int const *restrict v ) { *p += *v; *q += *v; }
(Read from right-to-left, e.g., v is a restricted pointer to a constant int; or use cdecl.)
By adding restrict, the the compiler can now generate code like:
mov eax, [rdx] ; tmp = *v add [rdi], eax ; *p += tmp add [rsi], eax ; *q += tmp
Now, the compiler was able to elide the previous line 3 of the additional mov instruction.
Perhaps the best-known example where restrict is used is the standard library function memcpy(). It’s the fastest way to copy a chunk of memory if the source and destination addresses do not overlap. The slightly slower memmove() function exists for use when the addresses do overlap.
Pitfalls
Misuse of restrict results in undefined behavior, for example, by passing pointers that do alias each other to update_ptrs_v2() or memcpy(). In some cases, the compiler can warn you, but not in all cases, so don’t rely on the compiler to catch misuses.
Note that restrict is for a given scope. Assigning one restricted pointer to another in the same scope results in undefined behavior:
void f( int *restrict d, int *restrict s ) { int *restrict p = s; // undefined behavior
However, you can assign a restricted pointer to an unrestricted pointer just fine:
void f( int *restrict d, int *restrict s ) { int *p = s; // OK
Even though p is unrestricted, the compiler can still perform the same optimizations.
It’s also OK to assign a restricted pointer in an inner scope to another in an outer scope (but not the other way around):
void f( int *restrict d, int *restrict s ) { { // inner scope int *restrict p = s; // OK // ... s = p; // undefined behavior } }
When (and When Not) to Use restrict
First, you should definitely profile your code (and perhaps even look at the generated assembly code) to see if using restrict actually makes a significant performance improvement to justify risking the potential pitfalls. Diagnosing bugs caused by misuse of restrict is very hard to do.
Second, if use of restrict is confined to the implementation of a function where the memory accessed via restricted pointers was allocated by you, then it’s safer. For example, given:
void safer( unsigned n ) { n += n % 2 != 0; // make even by rounding up int *const array = malloc( n * sizeof(unsigned) ); unsigned *restrict half_1st = array; unsigned *restrict half_2nd = array + n/2; // ... free( array ); }
the code could operate on the first and second halves of array safely because they don’t overlap (assuming you never access half_1st[n/2] or beyond).
Third, if restrict is used in a function’s parameters, then it’s potentially less safe. For example, contrast safer() with update_ptrs_v2() where the caller controls the pointers. For all you know, the caller got it wrong and passed pointers that alias.
Miscellaneous
Only pointers to objects (or void) can be qualified with restrict:
restrict int x; // error: can't restrict object int restrict *p; // error: pointer to restrict object int (*restrict f)(); // error: pointer-to-function
You can use restrict for struct members, for example:
struct node { void *restrict data; struct node *restrict left; struct node *restrict right; };
says that data will the the only pointer to that data and that left and right will never point to the same node. However, using restrict for struct members is very uncommon.
Lastly, C++ does not have restrict. Why not? There’s a long answer, but the TL;DR version is that:
- It can be a source of hard-to-find bugs that the C++ committee didn’t want to import from C.
- C++’s increased use of pointers, e.g., this, make using restrict safely even harder.
However, many compilers have __restrict__ as an extension.
Conclusion
In limited cases, using restrict can lead to performance improvements, but there are several significant pitfalls. If you’re considering using restrict, profile your code first.
Use wisely.
The above is the detailed content of The Obscure 'restrict” Keyword in C. For more information, please follow other related articles on the PHP Chinese website!

The main differences between C# and C are memory management, polymorphism implementation and performance optimization. 1) C# uses a garbage collector to automatically manage memory, while C needs to be managed manually. 2) C# realizes polymorphism through interfaces and virtual methods, and C uses virtual functions and pure virtual functions. 3) The performance optimization of C# depends on structure and parallel programming, while C is implemented through inline functions and multithreading.

The DOM and SAX methods can be used to parse XML data in C. 1) DOM parsing loads XML into memory, suitable for small files, but may take up a lot of memory. 2) SAX parsing is event-driven and is suitable for large files, but cannot be accessed randomly. Choosing the right method and optimizing the code can improve efficiency.

C is widely used in the fields of game development, embedded systems, financial transactions and scientific computing, due to its high performance and flexibility. 1) In game development, C is used for efficient graphics rendering and real-time computing. 2) In embedded systems, C's memory management and hardware control capabilities make it the first choice. 3) In the field of financial transactions, C's high performance meets the needs of real-time computing. 4) In scientific computing, C's efficient algorithm implementation and data processing capabilities are fully reflected.

C is not dead, but has flourished in many key areas: 1) game development, 2) system programming, 3) high-performance computing, 4) browsers and network applications, C is still the mainstream choice, showing its strong vitality and application scenarios.

The main differences between C# and C are syntax, memory management and performance: 1) C# syntax is modern, supports lambda and LINQ, and C retains C features and supports templates. 2) C# automatically manages memory, C needs to be managed manually. 3) C performance is better than C#, but C# performance is also being optimized.

You can use the TinyXML, Pugixml, or libxml2 libraries to process XML data in C. 1) Parse XML files: Use DOM or SAX methods, DOM is suitable for small files, and SAX is suitable for large files. 2) Generate XML file: convert the data structure into XML format and write to the file. Through these steps, XML data can be effectively managed and manipulated.

Working with XML data structures in C can use the TinyXML or pugixml library. 1) Use the pugixml library to parse and generate XML files. 2) Handle complex nested XML elements, such as book information. 3) Optimize XML processing code, and it is recommended to use efficient libraries and streaming parsing. Through these steps, XML data can be processed efficiently.

C still dominates performance optimization because its low-level memory management and efficient execution capabilities make it indispensable in game development, financial transaction systems and embedded systems. Specifically, it is manifested as: 1) In game development, C's low-level memory management and efficient execution capabilities make it the preferred language for game engine development; 2) In financial transaction systems, C's performance advantages ensure extremely low latency and high throughput; 3) In embedded systems, C's low-level memory management and efficient execution capabilities make it very popular in resource-constrained environments.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver CS6
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
