pmmp / ext-encoding Goto Github PK
View Code? Open in Web Editor NEWHigh-performance `ByteBuffer` implementation for PHP, planned for use in a future version of PocketMine-MP
High-performance `ByteBuffer` implementation for PHP, planned for use in a future version of PocketMine-MP
A ByteBuffer which can read from a subsection of another ByteBuffer's memory would be useful for reducing allocations.
For example, Minecraft packet batches contain length-prefixed packet buffers. Each of these buffers currently has to be copied to a new BinaryStream via substr() in order to be decoded. This allocation could be avoided if BinaryStream were capable of directly reading the memory of the original stream.
Currently around 2/3 of the overhead of write* comes from zend_string_init() and/or zend_string_alloc() due to emalloc() of tiny new strings.
These strings are almost immediately discarded in practice - after being appended to a buffer, they are no longer useful.
This means this is a giant waste of CPU time (though, it should be noted that this accounts for less than 1/3rd of the total time in benchmarks due to overhead added by PHP code).
This could be avoided by accepting a string by-reference to write bytes into instead of allocating new strings every time, although it would be better to have some dedicated type which we could reserve() bytes in (like a vector or similar).
This would also enable having large pooled reusable buffers for encoding, which would further improve performance.
The following things should be tested:
clone
must work correctly on ByteBuffer
serialize
and unserialize
must work correctly on ByteBuffer
$buffer = new ByteBuffer($data)
should behave the same as $buffer = new ByteBuffer(); $buffer->writeByteArray($data);
to be continued ...
BinaryStream::feof()
- tells whether there are bytes left to readBinaryStream::getRemaining()
- effectively substr(buffer, offset)
When appending to the end of a ByteBuffer
, only as many bytes as needed are allocated, which will result in many allocations for large data if the initial buffer size was small.
The standard way to do this is to double the size of the buffer whenever it fills up. This is what, for example, std::vector does in C++.
This is a surprising omission, forcing the use of toString() which creates a copy of the buffer.
If the caller wants to read a ByteBuffer for reading, the offset should be 0, so that the bytes can be read from the beginning.
If the caller wants to write a ByteBuffer, the offset should be placed at the end of the input data, as if writeByteArray()
was used.
Possible solutions:
::reader()
and ::writer()
static factory functionsByteBuffer
into a ByteBufferReader
and ByteBufferWriter
BinaryStream
was implicitly doing)this takes away the hassle of dealing with intrinsics, since the library does it on our behalf, meaning basically free performance.
This is low priority right now due to other more effective ways of improving performance.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.