php / php/php-src

Ability to pick PHP value by reference in FFI.

オープン
#7,866 コメント 9 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Extension: ffi Feature Status: Needs Triage
主要言語
C
スター
40.4k
フォーク
8.2k
平均マージ
2日 13時間
マージ済み PR(30日)
96

説明

Description

HI!
Currently the only way convert php value to FFI value is memcpy (or other ways with memory copying involved).
It could be wasting if we for example received large chunk from socket and need to deal with it through FFI.
For example,

/* Initial */
echo memory_get_usage(); // 348504

/* Got chunk of data */
$data = str_repeat('1', 512 << 10);
echo memory_get_usage(); // 876992

/* Defining the FFI type */
$type = FFI::arrayType(FFI::type('char'), [512 << 10]);
echo memory_get_usage(); // 877184

/* Allocating memory for type */
$ffiValue = FFI::new($type);
echo memory_get_usage(); // 1401984

/* And memcpy */
FFI::memcpy($ffiValue, $data, 512 << 10);
echo memory_get_usage(); 1402048

It would be great to have ability to reflect value from PHP to FFI using it by reference like so:

$data = str_repeat('1', 512 << 10);

/* @var FFI\CData $ffiValue */
$ffiValueCharArray = FFI::represent('char[]', &$data, size: 512 << 10); // No additional memory allocations for $data content

$ffiValueCharArray [1] = 5;
var_dump($data[1]); // $data changed by reference

$ffiValueInt32 = FFI::represent('int32_t', &$data, size: 4, offset: 12);
var_dump($ffiValueInt32->cdata); // substr($data, 12, 4) === "1111", we will get here 825307441

$ffiValueInt32->cdata = 10;
var_dump(bin2hex(substr($data, 12, 4))); // "0a000000"

Also could work not only with strings:

$int = 10;

$ffiValueInt64 = FFI::represent('int64_t', &$int); // length argument could be omitted if cast performed to fixed size type
var_dump($ffiValueInt64->cdata); // 10;

$ffiValueInt64->cdata = 20;
var_dump($int); // 20;

$readOverBoundary = FFI::represent('char[]', &$int, 10); // Exception: attempt to read over boundary

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、例に示されている FFI::type、FFI::arrayType、FFI::new、FFI::memcpy のエントリポイントから始め、次に提案された参照セマンティクスが PHP の値とメモリのモデルに適合するかどうかを判断します。提案された FFI::represent の動作について、メンテナーが承認した設計に到達すれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
c, php
領域
backend
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。