php / php/php-src

Ability to pick PHP value by reference in FFI.

Aperta
#7,866 9 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Extension: ffi Feature Status: Needs Triage
Lingua principale
C
Stelle
40.4k
Fork
8.1k
Merge medio
2g 13h
PR unite (30g)
96

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con gli entry point FFI::type, FFI::arrayType, FFI::new e FFI::memcpy mostrati negli esempi, quindi determina se la semantica dei riferimenti proposta è compatibile con il modello dei valori e della memoria di PHP. Il lavoro è completato quando si raggiunge un design approvato dai maintainer per il comportamento proposto di FFI::represent.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
c, php
Ambito
backend
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.