# RFC: Turn gc_collect_cycles into function pointer

- Status: Implemented (PHP 7.0)
- Authors: Benjamin Eberlei (kontakt@beberlei.de), Adam Harvey (aharvey@php.net)
- Date: 2015-01-07
- Version: 1.2
- Original: https://wiki.php.net/rfc/gc_fn_pointer
- Page: https://externals.io/rfc/gc_fn_pointer
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Turn gc_collect_cycles into function pointer? (closed)

Yes: 18 · No: 0

Voters: ab (Yes), aharvey (Yes), ajf (Yes), bwoebi (Yes), derick (Yes), dragoonis (Yes), guilhermeblanco (Yes), irker (Yes), jedibc (Yes), kalle (Yes), krakjoe (Yes), leigh (Yes), lstrojny (Yes), rdlowrey (Yes), rdohms (Yes), reeze (Yes), salathe (Yes), treffynnon (Yes)

## Discussions on the mailing list

- [\[RFC\] Turn gc_collect_cycles into a function pointer](https://externals.io/message/79413.md) — started on 2014-12-04, 11 messages
- [\[RFC\]\[VOTE\] Turn gc_collect_cycles into function pointer](https://externals.io/message/80255.md) — started on 2015-01-07, 4 messages

---

## Introduction

This RFC proposes to change `gc_collect_cycles` into a hookable function pointer. The goal is to allow extensions to hook into garbage collection for profiling.

## Proposal

PHP Profilers usually hook into zend\_execute and zend\_execute\_internal to measure execution time before and after the call. Because Garbage Collection is triggered during `ALLOC_ZVAL` and `FREE_ZVAL` (5.\*) and during variable/object release this can happen “undeterministically” in a random function call from the perspective of the developer. This skews reporting actual execution times.

Turning garbage collection into a function pointer would allow to measure the time spent in garbage collection and substract that from the actual function calls. This would allow Profilers to correct time spent in execution for accidentally triggered GC. It would also allow to give hints about inefficient garbage collection runs that should prompt a developer to consider calling `gc_disable()` in userland.

A default implementation `zend_gc_collect_cycles` will implement the current behavior.

Adam tested the performance of this change and there was no measurable overhead:

<https://github.com/LawnGnome/php-gc-benchmark>

### Relation to possible 7.0 extension API refactoring

On the mailinglist a discussion began weather this change should be part of a grand refactoring of hooks and extension API.

Making this change part of a grand refactoring will never lead to the inclusion since as of now, we know of nobody planning on working on the extension API for 7.0.

The RFC proposes to include the change AS-IS and then be part of a refactoring in the future, should one happen.

## Backward Incompatible Changes

No changes to userland.

This changes the signature of `gc_collect_cycles` in `Zend/zend_gc.h` file and should therefore target only 7.0

## Proposed PHP Version(s)

\- PHP 7.0

## RFC Impact

### To SAPIs

None

### To Existing Extensions

None

### To Opcache

None

## Unaffected PHP Functionality

This does not actually add profiling to garbage collection, it only allows extensions to build this on top.

## Patches and Tests

Adam Harvey has a patch for this feature here: <https://github.com/LawnGnome/php-src/compare/gc-fn-pointer>

## References

Initial discussion on hooking into Garbage Collection: <https://github.com/php/php-src/pull/930>

## Vote

**Vote: Turn gc\_collect\_cycles into function pointer?** — Yes: 18 · No: 0 (closed)

Voting started: 2015-01-07 19:31 UTC Voting ended: 2015-01-21 19:31 UTC (14 days)
