Giter VIP home page Giter VIP logo

ext-encoding's People

Contributors

dependabot[bot] avatar dktapps avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

ajunlonglive

ext-encoding's Issues

Reusable buffers

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.

Unit tests

The following things should be tested:

  • Basic parity with appropriate pack() and unpack() codes with selected values
  • Symmetry of encoding and decoding
  • read*() must correctly handle non-reference types by throwing an error
  • read*() must correctly update reference integer offset parameter if given
  • read*() must correctly handle not being given an offset parameter
  • read*() must read from the correct place when given an offset parameter
  • writeUnsignedVarInt() must handle negative numbers properly (i.e. it must terminate)
  • readUnsignedVarInt() must limit the number of bytes read and error appropriately
  • read*() functions must error properly when not given enough bytes
  • 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 ...

ByteBuffer::__construct() behaviour is always surprising to one use case or the other

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:

  • Make the offset parameter mandatory
  • Make a parameter that indicates whether the offset should be at the beginning or the end (slightly easier to use than a mandatory offset parameter)
  • Make the constructor private and have ::reader() and ::writer() static factory functions
  • Split ByteBuffer into a ByteBufferReader and ByteBufferWriter
  • Separate read offset and write offset (effectively what BinaryStream was implicitly doing)

Inefficient resizing of buffer when buffer length exceeded

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++.

Sub buffers

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.

Missing API equivalents

  • BinaryStream::feof() - tells whether there are bytes left to read
  • BinaryStream::getRemaining() - effectively substr(buffer, offset)

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.