This repository was archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
LZ4 compression support #219
Open
vchekan
wants to merge
5
commits into
jet:master
Choose a base branch
from
vchekan:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d99e0c5
Finish LZ4 support: use framing to be kafka compatible.
vchekan 8165c90
Merge branch 'lz4c' into HEAD
vchekan 2d63aee
Release notes
vchekan 55a58fd
Reworked packaging of native lz4 in order to make OSX work too
vchekan 5ba6b89
PR review changes
vchekan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,6 @@ | |
|
|
||
| [Dd]ebug/ | ||
| [Rr]elease/ | ||
| x64/ | ||
| build/ | ||
| [Bb]in/ | ||
| [Oo]bj/ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| ### 0.1.17 - 12.4.2018 | ||
| * LZ4 compression support is implemented | ||
|
|
||
| ### 0.1.16 - 27.3.2018 | ||
| * Tested 0.1.16-alpha01 | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| namespace Kafunk.Native | ||
|
|
||
| /// Windlows pre-load dll from x86/x64 folder depending on Environment.Is64BitProcess | ||
| module Loader = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can also make internal, rather than
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| open System | ||
| open System.Runtime.InteropServices | ||
| open System.IO | ||
|
|
||
| [<DllImport("Kernel32.dll")>] | ||
| extern IntPtr private LoadLibrary(string _path) | ||
|
|
||
| // | ||
| // Unix | ||
| // | ||
| let RTLD_NOW = 2 | ||
|
|
||
| [<DllImport("libdl")>] | ||
| extern IntPtr private dlopen(string _fileName, int _flags) | ||
|
|
||
| /// Load assembly relative to executing assembly's CodeBase. | ||
| /// This function will not work for multi-assembly configuration, but is ok for kafunk for now. | ||
| /// More elaborative loading strategies can be found here: | ||
| /// https://github.com/mellinoe/nativelibraryloader | ||
| let private resolveLibPath name = | ||
| System.Reflection.Assembly.GetExecutingAssembly().CodeBase | ||
| |> fun path -> (new Uri(path)).LocalPath | ||
| |> Path.GetDirectoryName | ||
| |> fun path -> Path.Combine(path, name) | ||
|
|
||
| let private loadWin name = | ||
| let path = resolveLibPath name | ||
| let ptr = LoadLibrary path | ||
|
|
||
| if ptr = IntPtr.Zero then | ||
| failwithf "Failed to load native dll '%s'" name | ||
|
|
||
| let load name = lazy( | ||
| match (Environment.Is64BitProcess, Environment.OSVersion.Platform) with | ||
| | (true, PlatformID.Win32NT) -> loadWin (sprintf "x64\\%s.dll" name) | ||
| | (false, PlatformID.Win32NT) -> loadWin (sprintf "x86\\%s.dll" name) | ||
| | _ -> () | ||
| ) | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| namespace Kafunk.Native | ||
| #nowarn "9" | ||
| module Lz4Framing = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be internal?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| /// For C API details, see: | ||
| /// https://github.com/lz4/lz4/blob/dev/lib/lz4frame.h | ||
| module private native = | ||
| open System | ||
| open System.Runtime.InteropServices | ||
|
|
||
| let LZ4F_VERSION = nativeint 100 | ||
|
|
||
| type LZ4F_errorCode_t = uint64 | ||
|
|
||
| // Shortcut these enums to int because we do not use them at this time | ||
| type LZ4F_blockSizeID_t = int | ||
| type LZ4F_blockMode_t = int | ||
| type LZ4F_contentChecksum_t = int | ||
| type LZ4F_frameType_t = int | ||
| type LZ4F_blockChecksum_t = int | ||
|
|
||
| [<StructLayout(LayoutKind.Sequential)>] | ||
| type LZ4F_frameInfo_t = | ||
| struct | ||
| val blockSizeID: LZ4F_blockSizeID_t | ||
| val blockMode: LZ4F_blockMode_t | ||
| val contentChecksumFlag: LZ4F_contentChecksum_t | ||
| val frameType: LZ4F_frameType_t | ||
| val mutable contentSize: uint64 | ||
| val dictID: uint32 | ||
| val blockChecksumFlag: LZ4F_blockChecksum_t | ||
| end | ||
|
|
||
| [<StructLayout(LayoutKind.Sequential)>] | ||
| type LZ4F_preferences_t = | ||
| struct | ||
| val mutable frameInfo: LZ4F_frameInfo_t | ||
| val compressionLevel: int32 | ||
| val autoFlush: uint32 | ||
| val reserved1: uint32 | ||
| val reserved2: uint32 | ||
| val reserved3: uint32 | ||
| val reserved4: uint32 | ||
| end | ||
|
|
||
| [<StructLayout(LayoutKind.Sequential)>] | ||
| type LZ4F_decompressOptions_t = | ||
| struct | ||
| val mutable stableDst: uint32 | ||
| val reserved1: uint32 | ||
| val reserved2: uint32 | ||
| val reserved3: uint32 | ||
| end | ||
|
|
||
|
|
||
| [<DllImport("liblz4", CallingConvention=CallingConvention.Cdecl)>] | ||
| extern nativeint LZ4F_compressFrameBound(nativeint _srcSize, IntPtr _preferencesPtr); | ||
|
|
||
| [<DllImport("liblz4", CallingConvention=CallingConvention.Cdecl)>] | ||
| extern nativeint LZ4F_compressFrame(nativeint _dstBuffer, nativeint _dstCapacity, | ||
| nativeint _srcBuffer, nativeint _srcSize, | ||
| LZ4F_preferences_t& _preferences); | ||
|
|
||
| [<DllImport("liblz4", CallingConvention=CallingConvention.Cdecl)>] | ||
| extern uint32 LZ4F_isError(nativeint _code); | ||
|
|
||
| [<DllImport("liblz4", CallingConvention=CallingConvention.Cdecl)>] | ||
| extern nativeint LZ4F_getErrorName(nativeint _code); | ||
|
|
||
| [<DllImport("liblz4", CallingConvention=CallingConvention.Cdecl)>] | ||
| extern nativeint LZ4F_createDecompressionContext(nativeint& _dctxPtr, nativeint _version); | ||
|
|
||
| [<DllImport("liblz4", CallingConvention=CallingConvention.Cdecl)>] | ||
| extern nativeint LZ4F_freeDecompressionContext(nativeint _dctx); | ||
|
|
||
| [<DllImport("liblz4", CallingConvention=CallingConvention.Cdecl)>] | ||
| extern nativeint LZ4F_decompress(nativeint _dctx, nativeint _dstBuffer, nativeint& _dstSize, | ||
| nativeint _srcBuffer, nativeint& _srcSizePtr, nativeint _optionsPtr); | ||
|
|
||
| [<DllImport("liblz4", CallingConvention=CallingConvention.Cdecl)>] | ||
| extern nativeint LZ4F_getFrameInfo(nativeint _dctx, LZ4F_frameInfo_t& _frameInfoPtr, nativeint _srcBuffer, nativeint& _srcSizePtr); | ||
|
|
||
| open System | ||
| open FSharp.NativeInterop | ||
| open native | ||
|
|
||
| let private ensureNativeIsLoaded = Loader.load "liblz4" | ||
|
|
||
| // | ||
| // liblz4 error reporting | ||
| // | ||
| let private isError code = | ||
| LZ4F_isError(code) <> 0u | ||
|
|
||
| let private getErrorName code = | ||
| let stringAddr = LZ4F_getErrorName(code) | ||
| System.Runtime.InteropServices.Marshal.PtrToStringAnsi(stringAddr) | ||
|
|
||
|
|
||
| let failIfError funcName code = | ||
| if isError code then | ||
| let error = getErrorName code | ||
| failwithf "LZ4 native call '%s' failed: '%s'" funcName error | ||
| else | ||
| code | ||
|
|
||
| // | ||
| // Public API | ||
| // | ||
|
|
||
| let compressFrameBound (srcSize: int) : int = | ||
| ensureNativeIsLoaded.Value | ||
| LZ4F_compressFrameBound((nativeint srcSize), IntPtr.Zero) | ||
| |> int | ||
|
|
||
| let compressFrame (src: ArraySegment<byte>) (dst: byte[]) = | ||
| ensureNativeIsLoaded.Value | ||
|
|
||
| let mutable compressParams = LZ4F_preferences_t() | ||
| compressParams.frameInfo.contentSize <- (uint64 src.Count) | ||
|
|
||
| use srcPtr = fixed src.Array | ||
| use dstPtr = fixed dst | ||
|
|
||
| let res = | ||
| LZ4F_compressFrame( | ||
| dstPtr |> NativePtr.toNativeInt, | ||
| (nativeint dst.Length), | ||
| NativePtr.add srcPtr src.Offset |> NativePtr.toNativeInt, (nativeint src.Count), | ||
| &compressParams | ||
| ) | ||
|
|
||
| if isError res then | ||
| let error = getErrorName res | ||
| failwithf "LZ4 native call LZ4F_compressFrame failed: '%s'" error | ||
| else | ||
| new ArraySegment<byte>(dst, 0, (int res)) | ||
|
|
||
| let decompress (src: ArraySegment<byte>): byte[] = | ||
| ensureNativeIsLoaded.Value | ||
|
|
||
| let mutable ctx = IntPtr.Zero | ||
| do LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION) |> failIfError "LZ4F_createDecompressionContext" |> ignore | ||
| try | ||
| // read frame info to get uncompressed size | ||
| let mutable frameInfo = LZ4F_frameInfo_t() | ||
| let mutable srcSize = nativeint src.Count | ||
| use srcPtr = fixed src.Array | ||
| let srcAddr = NativePtr.add srcPtr src.Offset |> NativePtr.toNativeInt | ||
|
|
||
| do LZ4F_getFrameInfo(ctx, &frameInfo, srcAddr, &srcSize) |> failIfError "LZ4F_getFrameInfo" |> ignore | ||
|
|
||
| let decompressedSize = frameInfo.contentSize | ||
| if decompressedSize = 0UL then | ||
| Array.empty | ||
| else | ||
| // LZ4F_getFrameInfo have updated srcSize to consumed bytes | ||
| let dataAddr = srcAddr + srcSize | ||
| srcSize <- (nativeint src.Count) - srcSize | ||
|
|
||
| let decompressed = Array.zeroCreate<byte> (int decompressedSize) | ||
| use decompressedPtr = fixed decompressed | ||
| let decompressedAddr = decompressedPtr |> NativePtr.toNativeInt | ||
| let mutable dstSize = nativeint decompressed.Length | ||
|
|
||
| let before = sprintf "dstSize: %d; srcSize: %d" dstSize srcSize | ||
| let res = LZ4F_decompress(ctx, decompressedAddr, &dstSize, dataAddr, &srcSize, IntPtr.Zero) |> failIfError "LZ4F_decompress" | ||
| let after = sprintf "dstSize: %d; srcSize: %d" dstSize srcSize | ||
| if res <> nativeint 0 then | ||
| failwithf "Expected LZ4F_decompress to return 0 but got %d. Buffer too small?\n %s\n %s" res before after | ||
| else | ||
| decompressed | ||
| finally | ||
| // protect context from leaking | ||
| do LZ4F_freeDecompressionContext(ctx) |> failIfError "LZ4F_freeDecompressionContext" |> ignore | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do an alpha first
0.1.17-alpha1There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done