Git Product home page Git Product logo

Comments (5)

iml6yu avatar iml6yu commented on August 15, 2024

补充一下:
当我使用Lz4Picker类进行操作的时候是完全OK的,只是速度略慢,我需要更快的速度。

from k4os.compression.lz4.

MiloszKrajewski avatar MiloszKrajewski commented on August 15, 2024

Unfortunately I don't read Chinese, but pickler is the simples approach:

var s = "{\"ID\":\"59fd4cb7-dd79-4f9d-953a-ac91dc0b00f9\",\"Key\":\"init\",\"Step\":2147483647,\"Datas\":null}";
var e = LZ4Pickler.Pickle(Encoding.UTF8.GetBytes(s));
var t = Encoding.UTF8.GetString(LZ4Pickler.Unpickle(e));

Debug.Assert(s == t);

If you really need to use low level functions, you need to do all the work around it.
For example, I can see that you are not using value returned by LZ4Codec.Encode(...). Well, value tells you how many bytes were used in compression, which is the value you need to use when doing LZ4Coded.Decode(...)

BTW, using GetBytes, GetString, new byte[] puts strain on GC. Try using IBufferWriter<byte> as much as possible (Pickler works with it).

from k4os.compression.lz4.

iml6yu avatar iml6yu commented on August 15, 2024

Thank you .
My English is very poor. so, I can't describe my problem clearly.

When using the LZ4Codec class to compress and decompress JSON strings, if the Length of the solution byte is set to the Length of the source byte[], it will not be decompressed. If the Length is set to Source.Length * 255, it will be decompressed correctly.

e.g.

//this is source string
            var s = "{\"ID\":\"59fd4cb7-dd79-4f9d-953a-ac91dc0b00f9\",\"Key\":\"init\",\"Step\":2147483647,\"Datas\":null}";             var srcList = new List<byte>(); 
            srcList.AddRange(System.Text.Encoding.UTF8.GetBytes(s));

//Get src byte[]
            var src = srcList.ToArray();
//Init encode byte[]
            var target = new byte[LZ4Codec.MaximumOutputSize(src.Length) ];
       
            LZ4Codec.Encode(src, 0, src.Length, target, 0, target.Length, LZ4Level.L03_HC);
           
//Data copy  
            var rarray = new byte[target.Length];
            Array.Copy(target, rarray, rarray.Length);

//init  decode byte[] 
            var r = new byte[src.Length];
            LZ4Codec.Decode(rarray, r);
// result string 
            var rs = System.Text.Encoding.UTF8.GetString(r);
//this is false rs != s,  
// But, if  s="abcdefg",this is true, rs == s
            Assert.IsTrue(rs == s);

from k4os.compression.lz4.

iml6yu avatar iml6yu commented on August 15, 2024

Pickler very good, it's so great.

from k4os.compression.lz4.

MiloszKrajewski avatar MiloszKrajewski commented on August 15, 2024

Your rarray has maximum bytes not actual bytes.
Take actualLength = LZ4Codec.Encode(...); and use it when creating var rarray = new byte[actualLength];

BUT...

your code is far from optimal. You are making many unnecesary allocation (for example: srcList.AddRange(System.Text.Encoding.UTF8.GetBytes(s)); var src = srcList.ToArray();). Stick to my previous example:

var s = "{\"ID\":\"59fd4cb7-dd79-4f9d-953a-ac91dc0b00f9\",\"Key\":\"init\",\"Step\":2147483647,\"Datas\":null}";
var e = LZ4Pickler.Pickle(Encoding.UTF8.GetBytes(s));
var t = Encoding.UTF8.GetString(LZ4Pickler.Unpickle(e));

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.