Opened 18 years ago
Closed 2 years ago
#151 closed enhancement (fixed)
Use function attributes to mark functions as "const" or "pure"
Reported by: | Olly Betts | Owned by: | Olly Betts |
---|---|---|---|
Priority: | normal | Milestone: | 1.4.x |
Component: | Other | Version: | SVN trunk |
Severity: | minor | Keywords: | |
Cc: | Blocked By: | ||
Blocking: | Operating System: | All |
Description (last modified by )
GCC allows functions to be annotated with __attribute__((const))
if they "do not
examine any values except their arguments, and have no effects except the return
value", which allows the compiler to use CSE to eliminate calls to them with
identical arguments. This would probably be very useful for
Xapian::Unicode::get_category() for example.
__attribute__((const))
is available for (at least) GCC 2.95 onwards, which is all the versions we support. MSVC doesn't seem to have an equivalent. I've not looked at other compilers.
There's also __attribute__((pure))
which is like const, but the function may also examine global memory, perhaps via pointer (or presumably reference) parameters. This is available from GCC 2.96 onwards, which covers all the GCC versions we support for 1.1.x and higher.
MSVC doesn't seem to have an equivalent. I've not looked at other compilers.
Both GCC and MSVC support nothrow - this is now ticket #454.
GCC also allows functions to be marked listing pointer parameters which can't be NULL, which may be worth considering, or perhaps such parameters should be changed to references.
URL: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Function-Attributes.html#Function-Attributes
Change History (14)
comment:1 by , 18 years ago
Blocking: | 120 added |
---|---|
Operating System: | → All |
Status: | new → assigned |
Summary: | Use function attributes to mark functions as "const", "pure", and "nothrow".pure and const functions → Use function attributes to mark functions as "const", "pure", and "nothrow" |
comment:3 by , 17 years ago
Description: | modified (diff) |
---|---|
Milestone: | → 1.1 |
comment:4 by , 17 years ago
Blocking: | 120 removed |
---|
(In #120) Remove the unfixed dependencies so we can close this bug - they're all marked for the 1.1.0 milestone.
comment:5 by , 16 years ago
Milestone: | 1.1.0 → 1.1.1 |
---|
Doing this to internal functions wouldn't affect the ABI and to external functions wouldn't affect the API so bumping to 1.1.1.
comment:7 by , 16 years ago
Milestone: | 1.1.4 → 1.1.3 |
---|
comment:8 by , 15 years ago
Milestone: | 1.1.3 → 1.2.0 |
---|
I think this should be ABI compatible, except perhaps the "nothrow" stuff. We can certainly start to use these inside the library in 1.2.x. So bumping.
comment:9 by , 15 years ago
Description: | modified (diff) |
---|---|
Summary: | Use function attributes to mark functions as "const", "pure", and "nothrow" → Use function attributes to mark functions as "const" or "pure" |
Hmm, I created #454 for nothrow without realising we already had a ticket.
Since we're unlikely to address all of these at once, it seems useful to have a separate ticket, so removing nothrow from this one.
comment:10 by , 14 years ago
Description: | modified (diff) |
---|
comment:11 by , 13 years ago
Milestone: | 1.2.x → 1.3.x |
---|
comment:12 by , 12 years ago
comment:14 by , 7 years ago
[3dc09282a10ad737d8a83b8cdd7c846653142bfa] added XAPIAN_NONNULL
and subsequent commits made use of it in some places in the API.
comment:15 by , 2 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
I think it makes most sense to close this at this point.
We have macros to support specifying these attributes in a portable way so they work where supported and where not, and these macros are used in the most obvious places.
There's likely always going to be scope to use them more, but in most places they'll make little or no difference. In some places they'll make a significant difference (e.g. by allowing the compiler to realise it can apply CSE and only call a function once and reuse the result). It's really these cases that matter.
Oops, finger trouble...
attribute((const)) is available for (at least) GCC 2.95 onwards, which is all the versions we support.
There's also attribute((pure)) which is like const, but the function may also examine global memory, perhaps via pointer (or presumably reference) parameters. This is available from GCC 2.96 onwards.
MSVC doesn't seem to have equivalents for either of these.
Both GCC and MSVC support nothrow (as attribute and declspec respectively) to indicate a function doesn't throw exceptions. ISO C++ has "throw()" - not sure if this is the same or subtely different though.
GCC also allows functions to be marked listing pointer parameters which can't be NULL, which may be worth considering.
Marking this for consideration for 1.0.X.