php / php/php-src

Ability to pick PHP value by reference in FFI.

未关闭
#7,866 9 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Extension: ffi Feature Status: Needs Triage
主要语言
C
星标
40.4k
派生
8.1k
平均合并
2 天 13 小时
30 天内合并 PR
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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从示例中展示的 FFI::type、FFI::arrayType、FFI::new 和 FFI::memcpy 入口点开始,然后确定所提议的引用语义是否符合 PHP 的值和内存模型。达到由维护者批准的 FFI::represent 拟议行为设计,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
c, php
领域
backend
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。