Opened 17 years ago

Closed 18 months 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 Olly Betts)

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 Olly Betts, 17 years ago

Blocking: 120 added
Operating System: All
Status: newassigned
Summary: Use function attributes to mark functions as "const", "pure", and "nothrow".pure and const functionsUse function attributes to mark functions as "const", "pure", and "nothrow"

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.

comment:3 by Richard Boulton, 16 years ago

Description: modified (diff)
Milestone: 1.1

comment:4 by Richard Boulton, 16 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 Olly Betts, 15 years ago

Milestone: 1.1.01.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:6 by Olly Betts, 15 years ago

Milestone: 1.1.11.1.4

Triaging milestone:1.1.1 bugs.

comment:7 by Olly Betts, 15 years ago

Milestone: 1.1.41.1.3

comment:8 by Olly Betts, 15 years ago

Milestone: 1.1.31.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 Olly Betts, 14 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 Olly Betts, 13 years ago

Description: modified (diff)

comment:11 by Olly Betts, 12 years ago

Milestone: 1.2.x1.3.x

comment:12 by Olly Betts, 12 years ago

Added XAPIAN_CONST_FUNCTION and XAPIAN_PURE_FUNCTION and used them to mark up some obvious cases in the public API in r16632, and updated the bindings to ignore them in r16633.

Still to do:

  • check for any more cases in the API
  • use these internally
  • consider other function attributes (like nonnull)

comment:13 by Olly Betts, 9 years ago

Milestone: 1.3.x1.4.x

Shouldn't block 1.4.0.

comment:14 by Olly Betts, 6 years ago

[3dc09282a10ad737d8a83b8cdd7c846653142bfa] added XAPIAN_NONNULL and subsequent commits made use of it in some places in the API.

comment:15 by Olly Betts, 18 months ago

Resolution: fixed
Status: assignedclosed

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.

Note: See TracTickets for help on using tickets.