Code Monkey home page Code Monkey logo

php-ext-thisrc's Introduction

php-ext-thisrc

Actions GitHub GitHub

PHP extension to get refcount of $this.

This extension only adds one new function thisrc() : int. It returns the refcount of $this when called from a class.

Why require $this instead of arbitrary argument?

When the parameter is passed to another function, the refcount increases. This might cause confusing behaviour. Calling the function without passing any references is more reliable.

When is refcount information useful?

Consider an immutable value class with methods that return a modified version:

class Foo {
	private $inner;

	public function __construct(int $inner) {
		$this->inner = $inner;
	}

	public function plus(int $value) : Foo {
		return new Foo($this->inner + $value);
	}

	public function times(int $value) : Foo {
		return new Foo($this->inner - $value);
	}
}

$three = new Foo(3);
$sixteen = $three->plus(5)->times(2);

This creases an intermediate object Foo(8) in the call chain. If used in hot code, this allocation could be optimized away:

public function plus(int $value) : Foo {
	if(thisrc() === 1) {
		$this->inner += $value;
		return $this;
	} else {
		return new Foo($this->inner + $value);
	}
}

Then only one new Foo object is created in the call chain that creates $sixteen, avoiding the allocation for the intermediate Foo(8).

php-ext-thisrc's People

Contributors

sof3 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.