search
HomeBackend DevelopmentPHP TutorialVariable length bytecode algorithm

Recently I was reading the book "Large Scale WEB Service Development Technology". The book mentions the data compression algorithm of "Variable Length Bytecode Algorithm" to compress data and reduce disk IO.
Variable length bytecode algorithm:
The highest bit of any byte (subscript 7) is only used as a flag bit, and it needs to be multiplied by the corresponding power of 128 according to the position of the byte;

This is his pseudo code
Variable length bytecode algorithm

After careful study , I translated it into the PHP version:

<code><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><?php function codeNumber<span>(<span>$n</span>)</span>{
        <span>$bytes</span> = [];
        while <span>(true)</span>{
            array_unshift<span>(<span>$bytes</span>, bcmod<span>(<span>$n</span>, <span>128</span>)</span>)</span>;
            if<span>(<span>$n</span> 128</span>)</span>{
                break;
            }else{
                <span>$n</span> = intval<span>(<span>$n</span>/<span>128</span>)</span>;
            }
        }
        <span>$bytes</span>[count<span>(<span>$bytes</span>)</span> - <span>1</span>] += <span>128</span>;
        return <span>$bytes</span>;
    }

    function encode<span>(<span>$numbers</span>)</span>{
        <span>$bytestream</span> = [];
        foreach <span>(<span>$numbers</span> as <span>$n</span>)</span>{
            <span>$bytestream</span> = array_merge<span>(<span>$bytestream</span>, codeNumber<span>(<span>$n</span>)</span>)</span>;
        }
        return <span>$bytestream</span>;
    }

    function decode<span>(<span>$bytestream</span>)</span>{
        <span>$numbers</span> = [];
        <span>$n</span> = <span>0</span>;
        for <span>(<span>$i</span> = <span>0</span>; <span>$i</span> (<span>$bytestream</span>)</span>; <span>$i</span>++)</span>{
            if<span>(<span>$bytestream</span>[<span>$i</span>] 128</span>)</span>{
                <span>$n</span> = <span>128</span> * <span>$n</span> + <span>$bytestream</span>[<span>$i</span>];
            }else{
                <span>$n</span> = <span>128</span> * <span>$n</span> + <span>(<span>$bytestream</span>[<span>$i</span>] - <span>128</span>)</span>;
                array_push<span>(<span>$numbers</span>, <span>$n</span>)</span>;
                <span>$n</span> = <span>0</span>;
            }
        }
        return <span>$numbers</span>;
    }
    <span>$a</span> = encode<span>([<span>5</span>, <span>130</span>, <span>288</span>])</span>;
    var_dump<span>(<span>$a</span>)</span>;
    var_dump<span>(decode<span>(<span>$a</span>)</span>)</span>;

打印出来的内容是:
array<span>(<span>5</span>)</span> { [<span>0</span>]=> int<span>(<span>133</span>)</span> [<span>1</span>]=> string<span>(<span>1</span>)</span><span>"1"</span> [<span>2</span>]=> int<span>(<span>130</span>)</span> [<span>3</span>]=> string<span>(<span>1</span>)</span><span>"2"</span> [<span>4</span>]=> int<span>(<span>160</span>)</span> }
array<span>(<span>3</span>)</span> { [<span>0</span>]=> int<span>(<span>5</span>)</span> [<span>1</span>]=> int<span>(<span>130</span>)</span> [<span>2</span>]=> int<span>(<span>288</span>)</span> }

    //写二进制
    <span>$h</span> = fopen<span>(<span>'ejz3.txt'</span>, <span>'wb'</span>)</span>;
    foreach <span>(<span>$a</span> as <span>$k</span> => <span>$v</span>)</span>
    {
      <span>$str3</span> =  pack<span>(<span>'H*'</span>, sprintf<span>(<span>"%02x"</span>, <span>$v</span>)</span>)</span>;
      fwrite<span>(<span>$h</span>,  <span>$str3</span>)</span>;
    }
    fclose<span>(<span>$h</span>)</span>;

    //读二进制
    <span>$str2</span> = file_get_contents<span>(<span>'ejz3.txt'</span>)</span>;
    <span>$str2</span> = unpack<span>(<span>"H*"</span>, <span>$str2</span>)</span>;
    <span>$value</span> = str_split<span>(<span>$str2</span>[<span>1</span>], <span>2</span>)</span>;
    foreach <span>(<span>$value</span> as <span>$k</span> => <span>$v</span>)</span>
    {
      <span>$value</span>[<span>$k</span>] = base_convert<span>(<span>$v</span>, <span>16</span>, <span>10</span>)</span>;
    }
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above has introduced the variable length bytecode algorithm, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Where are PHP session files stored by default?Where are PHP session files stored by default?May 01, 2025 am 12:15 AM

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

How do you retrieve data from a PHP session?How do you retrieve data from a PHP session?May 01, 2025 am 12:11 AM

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

How can you use sessions to implement a shopping cart?How can you use sessions to implement a shopping cart?May 01, 2025 am 12:10 AM

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

What is autoloading in PHP?What is autoloading in PHP?Apr 30, 2025 pm 03:37 PM

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

What are PHP streams?What are PHP streams?Apr 30, 2025 pm 03:36 PM

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function