-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use rust via FFI for memory efficient diff generation (#879)
* feat: use rust via FFI for memory efficient diff generation * chore: add rust setup in test * chore: add toggle via properties * chore: use build tags for rust * chore: add toggle via properties * chore: add make build-prod * chore: add properties to toggle rust invokation
- Loading branch information
1 parent
930fe2e
commit f48806e
Showing
10 changed files
with
203 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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,3 +17,7 @@ config-db.properties | |
output-*/ | ||
configs/ | ||
*.pprof | ||
|
||
|
||
# Rust | ||
external/diffgen/target |
This file contains 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 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,12 @@ | ||
[package] | ||
name = "diffgen" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
libc = "0.2.158" | ||
similar = { git = "https://github.com/mitsuhiko/similar", version = "2.6.0" } | ||
|
||
|
||
[lib] | ||
crate-type = ["cdylib", "staticlib"] |
This file contains 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,6 @@ | ||
#include <stdarg.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
|
||
char *diff(const char *before, const char *after); |
This file contains 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,41 @@ | ||
extern crate similar; | ||
|
||
use similar::TextDiff; | ||
use std::ffi::{CStr, CString}; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn diff( | ||
before: *const libc::c_char, | ||
after: *const libc::c_char, | ||
) -> *mut libc::c_char { | ||
let before_cstr = unsafe { CStr::from_ptr(before) }; | ||
let before_str = before_cstr.to_str().unwrap(); | ||
|
||
let after_cstr = unsafe { CStr::from_ptr(after) }; | ||
let after_str = after_cstr.to_str().unwrap(); | ||
|
||
let diff = TextDiff::from_lines(before_str, after_str); | ||
|
||
CString::new(diff.unified_diff().to_string()) | ||
.unwrap() | ||
.into_raw() | ||
} | ||
|
||
#[cfg(test)] | ||
pub mod test { | ||
use super::*; | ||
use std::ffi::CString; | ||
|
||
#[test] | ||
fn test_diff() { | ||
let diff_result = diff( | ||
CString::new("hello\nworld\n").unwrap().into_raw(), | ||
CString::new("bye\nworld\n").unwrap().into_raw(), | ||
); | ||
|
||
assert_eq!( | ||
unsafe { CStr::from_ptr(diff_result) }.to_str().unwrap(), | ||
"@@ -1,2 +1,2 @@\n-hello\n+bye\n world\n" | ||
) | ||
} | ||
} |
This file contains 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,37 @@ | ||
//go:build rustdiffgen | ||
|
||
package main | ||
|
||
/* | ||
#cgo LDFLAGS: ${SRCDIR}/external/diffgen/target/release/libdiffgen.a -ldl | ||
#include "./external/diffgen/libdiffgen.h" | ||
#include <stdlib.h> | ||
*/ | ||
import "C" | ||
|
||
import ( | ||
"unsafe" | ||
|
||
"github.com/flanksource/config-db/db" | ||
) | ||
|
||
func init() { | ||
db.DiffFunc = func(before, after string) string { | ||
beforeCString := C.CString(before) | ||
defer C.free(unsafe.Pointer(beforeCString)) | ||
|
||
afterCString := C.CString(after) | ||
defer C.free(unsafe.Pointer(afterCString)) | ||
|
||
diffChar := C.diff(beforeCString, afterCString) | ||
defer C.free(unsafe.Pointer(diffChar)) | ||
if diffChar == nil { | ||
return "" | ||
} | ||
|
||
// prefix is required for UI | ||
prefix := "--- before\n+++ after\n" | ||
diff := C.GoString(diffChar) | ||
return prefix + diff | ||
} | ||
} |