Git Product home page Git Product logo

Comments (4)

MiloszKrajewski avatar MiloszKrajewski commented on July 18, 2024 1

It absolutely works as described in the stream compression chapter in the README but you misunderstood how it works.

Let me go through your code.

var random = new Random();
var data = new byte[ushort.MaxValue];
random.NextBytes(data);

You don't understand the idea of random data. This is does not matter too much here as it will work with random bytes, but still, you need to understand that:

[...] The central idea is that a string of bits is random if and only if it is shorter than any computer program that can produce that string [...], which means that random strings are those that cannot be compressed [...]

var compressionOutput = new MemoryStream(ushort.MaxValue);
var compressionInput = new MemoryStream(ushort.MaxValue);

This pre-allocation might be useful in some high performance scenario, but this code, even after fixing obvious bugs is far from optimized, so it just makes things complex for no reason.

compressionInput.SetLength(0);
compressionInput.Write(data, 0, data.Length);
compressionInput.Position = 0;

Writing it to stream, to copy it later is a waste of resources. LZ4 is a stream already, why not write it directly?

using (var compressionStream = LZ4Stream.Encode(compressionInput, leaveOpen: true))
    compressionStream.CopyTo(compressionOutput);

This is bad in both lines. You switched input and output and then tried to copy in wrong direction.
The correct(-ish) version would be:

using (var compressionStream = LZ4Stream.Encode(compressionOutput, leaveOpen: true))
    compressionInput.CopyTo(compressionStream);
var compressed = compressionOutput.ToArray();

So you have compressed bytes... which you never use again!

var decompressionOutput = new MemoryStream(ushort.MaxValue);
var decompressionInput = new MemoryStream(ushort.MaxValue);

Unnecessary pre-allocation again.

decompressionInput.SetLength(0);
decompressionInput.Write(data, 0, data.Length);
decompressionInput.Position = 0;

This is also wrong, this should be compressed not data.

using (var decompressionStream = LZ4Stream.Decode(compressionInput, leaveOpen: true))
    decompressionStream.CopyTo(decompressionOutput);

Not compressionInput but decompressionInput.

var decompressed = decompressionOutput.ToArray();
Console.WriteLine(new
{
    ReferenceEquals = ReferenceEquals(data, decompressed),
    SequenceEqual = Enumerable.SequenceEqual(data, decompressed),
});

So bug-free code would be:

using System;
using System.IO;
using System.Linq;
using K4os.Compression.LZ4.Streams;

public static class Program
{
    static void Main(string[] args)
    {
        var random = new Random();
        var data = new byte[ushort.MaxValue];
        random.NextBytes(data);

        var compressionOutput = new MemoryStream();
        var compressionInput = new MemoryStream();
        compressionInput.SetLength(0);
        compressionInput.Write(data, 0, data.Length);
        compressionInput.Position = 0;
		
        using (var compressionStream = LZ4Stream.Encode(compressionOutput, leaveOpen: true))
            compressionInput.CopyTo(compressionStream);
        var compressed = compressionOutput.ToArray();

        var decompressionOutput = new MemoryStream();
        var decompressionInput = new MemoryStream();
        decompressionInput.SetLength(0);
        decompressionInput.Write(compressed, 0, compressed.Length);
        decompressionInput.Position = 0;
		
        using (var decompressionStream = LZ4Stream.Decode(decompressionInput, leaveOpen: true))
            decompressionStream.CopyTo(decompressionOutput);
        var decompressed = decompressionOutput.ToArray();
		
        Console.WriteLine(new
        {
            ReferenceEquals = ReferenceEquals(data, decompressed),
            SequenceEqual = Enumerable.SequenceEqual(data, decompressed),
        });

        Console.WriteLine("\n(press any key to exit)");
        Console.ReadKey();
    }
}

after removing intermediate streams which are just consuming memory, faster version would be:

using System;
using System.IO;
using System.Linq;
using K4os.Compression.LZ4.Streams;

public static class Program
{
    static void Main(string[] args)
    {
        var random = new Random();
        var data = new byte[ushort.MaxValue];
        random.NextBytes(data);

        var compressionOutput = new MemoryStream();
		
        using (var compressionStream = LZ4Stream.Encode(compressionOutput, leaveOpen: true))
            compressionStream.Write(data, 0, data.Length);

        compressionOutput.Position = 0;

	var decompressed = new byte[data.Length];
        using (var decompressionStream = LZ4Stream.Decode(compressionOutput, leaveOpen: true))
            decompressionStream.Read(decompressed, 0, decompressed.Length);
		
        Console.WriteLine(new
        {
            ReferenceEquals = ReferenceEquals(data, decompressed),
            SequenceEqual = Enumerable.SequenceEqual(data, decompressed),
        });

        Console.WriteLine("\n(press any key to exit)");
        Console.ReadKey();
    }
}

from k4os.compression.lz4.

MiloszKrajewski avatar MiloszKrajewski commented on July 18, 2024

I wrote a Stream test program according to the instructions in the README file [...]

Does not feel like it. Please point to the part of README which made you think it works this way so I can rephase it if needed.

Not sure what you want to do but there is no bug here: LZ4Stream.Encode(...) creates a stream to be written to (Encode/Compress), so:

using (var compressionStream = LZ4Stream.Encode(...))
    compressionStream.CopyTo(compressionOutput);

tries to read from (copy from) stream which is write-only.

from k4os.compression.lz4.

overing avatar overing commented on July 18, 2024

My English is not very fluent
I apologize to you for my rudeness

This is the code I used to test
I expected it to work as described in the stream compression chapter in the README but I got an exception
By the way, my target framework is dotnet 6.0

'System.Private.CoreLib.dll': 'Stream does not support reading.'
   at System.ThrowHelper.ThrowNotSupportedException_UnreadableStream()
   at System.IO.Stream.CopyTo(Stream destination, Int32 bufferSize)
   at System.IO.Stream.CopyTo(Stream destination)
using System;
using System.IO;
using System.Linq;
using System.Text;
using K4os.Compression.LZ4.Streams;

var data = Encoding.UTF8.GetBytes("Try K4OS LZ4 Stream :)");

var compressionInput = new MemoryStream(data);
var compressionOutput = new MemoryStream(ushort.MaxValue);
using (var compressionStream = LZ4Stream.Encode(compressionInput, leaveOpen: true))
    compressionStream.CopyTo(compressionOutput);

var compressed = compressionOutput.ToArray();

var decompressionInput = new MemoryStream(compressed);
var decompressionOutput = new MemoryStream(ushort.MaxValue);
using (var decompressionStream = LZ4Stream.Decode(decompressionInput, leaveOpen: true, interactive: false))
    decompressionStream.CopyTo(decompressionOutput);

var decompressed = decompressionOutput.ToArray();
Console.WriteLine(new
{
    ReferenceEquals = ReferenceEquals(data, decompressed),
    SequenceEqual = Enumerable.SequenceEqual(data, decompressed),
});

Console.WriteLine("\n(press any key to exit)");
Console.ReadKey();

image

from k4os.compression.lz4.

overing avatar overing commented on July 18, 2024

Code works successfully
Thank you very much for your help and advice
they are useful to me

from k4os.compression.lz4.

Related Issues (20)

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.