Merge pull request #5 from Xymist/rust_xymist

Rust performance improvements
This commit is contained in:
Ade Attwood 2022-08-26 21:06:46 +01:00 committed by GitHub
commit a50a489479
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 591 additions and 150 deletions

3
.gitignore vendored
View file

@ -3,3 +3,6 @@ target
.cache .cache
compile_commands.json compile_commands.json
.luacheckcache .luacheckcache
benchmarks
flamegraph*
perf.data*

467
Cargo.lock generated
View file

@ -11,21 +11,141 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]] [[package]]
name = "bstr" name = "bstr"
version = "0.2.17" version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223" checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223"
dependencies = [ dependencies = [
"lazy_static",
"memchr", "memchr",
"regex-automata",
"serde",
] ]
[[package]]
name = "bumpalo"
version = "3.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1ad822118d20d2c234f427000d5acc36eabe1e29a348c89b63dd60b13f28e5d"
[[package]]
name = "cast"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "2.34.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
dependencies = [
"bitflags",
"textwrap",
"unicode-width",
]
[[package]]
name = "criterion"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b01d6de93b2b6c65e17c634a26653a29d107b3c98c607c765bf38d041531cd8f"
dependencies = [
"atty",
"cast",
"clap",
"criterion-plot",
"csv",
"itertools",
"lazy_static",
"num-traits",
"oorandom",
"plotters",
"rayon",
"regex",
"serde",
"serde_cbor",
"serde_derive",
"serde_json",
"tinytemplate",
"walkdir",
]
[[package]]
name = "criterion-plot"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2673cc8207403546f45f5fd319a974b1e6983ad1a3ee7e6041650013be041876"
dependencies = [
"cast",
"itertools",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2dd04ddaf88237dc3b8d8f9a3c1004b506b54b3313403944054d23c0870c521"
dependencies = [
"cfg-if",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "715e8152b692bba2d374b53d4875445368fdf21a94751410af607a5ac677d1fc"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "045ebe27666471bb549370b4b0b3e51b07f56325befa4284db65fc89c02511b1"
dependencies = [
"autocfg",
"cfg-if",
"crossbeam-utils",
"memoffset",
"once_cell",
"scopeguard",
]
[[package]] [[package]]
name = "crossbeam-utils" name = "crossbeam-utils"
version = "0.8.11" version = "0.8.11"
@ -36,6 +156,34 @@ dependencies = [
"once_cell", "once_cell",
] ]
[[package]]
name = "csv"
version = "1.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22813a6dc45b335f9bade10bf7271dc477e81113e89eb251a0bc2a8a81c536e1"
dependencies = [
"bstr",
"csv-core",
"itoa 0.4.8",
"ryu",
"serde",
]
[[package]]
name = "csv-core"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90"
dependencies = [
"memchr",
]
[[package]]
name = "either"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.7" version = "1.0.7"
@ -64,6 +212,21 @@ dependencies = [
"regex", "regex",
] ]
[[package]]
name = "half"
version = "1.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7"
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "ignore" name = "ignore"
version = "0.4.18" version = "0.4.18"
@ -82,13 +245,45 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "itertools"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9a9d19fa1e79b6215ff29b9d6880b706147f16e9b1dbb1e4e5947b5b02bc5e3"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4"
[[package]]
name = "itoa"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754"
[[package]] [[package]]
name = "ivy" name = "ivy"
version = "0.0.1" version = "0.0.1"
dependencies = [ dependencies = [
"criterion",
"fuzzy-matcher", "fuzzy-matcher",
"ignore", "ignore",
"lazy_static", "lazy_static",
"rayon",
]
[[package]]
name = "js-sys"
version = "0.3.59"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "258451ab10b34f8af53416d1fdab72c22e805f0c92a1136d59470ec0b11138b2"
dependencies = [
"wasm-bindgen",
] ]
[[package]] [[package]]
@ -97,6 +292,12 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.132"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5"
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.17" version = "0.4.17"
@ -112,12 +313,116 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "memoffset"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce"
dependencies = [
"autocfg",
]
[[package]]
name = "num-traits"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1"
dependencies = [
"hermit-abi",
"libc",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.13.0" version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1" checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1"
[[package]]
name = "oorandom"
version = "11.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575"
[[package]]
name = "plotters"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "716b4eeb6c4a1d3ecc956f75b43ec2e8e8ba80026413e70a3f41fd3313d3492b"
dependencies = [
"num-traits",
"plotters-backend",
"plotters-svg",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "plotters-backend"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "193228616381fecdc1224c62e96946dfbc73ff4384fba576e052ff8c1bea8142"
[[package]]
name = "plotters-svg"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9a81d2759aae1dae668f783c308bc5c8ebd191ff4184aaa1b37f65a6ae5a56f"
dependencies = [
"plotters-backend",
]
[[package]]
name = "proc-macro2"
version = "1.0.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rayon"
version = "1.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd99e5772ead8baa5215278c9b15bf92087709e9c1b2d1f97cdb5a183c933a7d"
dependencies = [
"autocfg",
"crossbeam-deque",
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "258bcdb5ac6dad48491bb2992db6b7cf74878b0384908af124823d118c99683f"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils",
"num_cpus",
]
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.6.0" version = "1.6.0"
@ -129,12 +434,24 @@ dependencies = [
"regex-syntax", "regex-syntax",
] ]
[[package]]
name = "regex-automata"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
[[package]] [[package]]
name = "regex-syntax" name = "regex-syntax"
version = "0.6.27" version = "0.6.27"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244"
[[package]]
name = "ryu"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
[[package]] [[package]]
name = "same-file" name = "same-file"
version = "1.0.6" version = "1.0.6"
@ -144,6 +461,70 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "serde"
version = "1.0.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860"
[[package]]
name = "serde_cbor"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bef2ebfde456fb76bbcf9f59315333decc4fda0b2b44b420243c11e0f5ec1f5"
dependencies = [
"half",
"serde",
]
[[package]]
name = "serde_derive"
version = "1.0.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44"
dependencies = [
"itoa 1.0.3",
"ryu",
"serde",
]
[[package]]
name = "syn"
version = "1.0.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]] [[package]]
name = "thread_local" name = "thread_local"
version = "1.1.4" version = "1.1.4"
@ -153,6 +534,28 @@ dependencies = [
"once_cell", "once_cell",
] ]
[[package]]
name = "tinytemplate"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
dependencies = [
"serde",
"serde_json",
]
[[package]]
name = "unicode-ident"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf"
[[package]]
name = "unicode-width"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
[[package]] [[package]]
name = "walkdir" name = "walkdir"
version = "2.3.2" version = "2.3.2"
@ -164,6 +567,70 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "wasm-bindgen"
version = "0.2.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc7652e3f6c4706c8d9cd54832c4a4ccb9b5336e2c3bd154d5cccfbf1c1f5f7d"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "662cd44805586bd52971b9586b1df85cdbbd9112e4ef4d8f41559c334dc6ac3f"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b260f13d3012071dfb1512849c033b1925038373aea48ced3012c09df952c602"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5be8e654bdd9b79216c2929ab90721aa82faf65c48cdf08bdc4e7f51357b80da"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6598dd0bd3c7d51095ff6531a5b23e02acdc81804e30d8f07afb77b7215a140a"
[[package]]
name = "web-sys"
version = "0.3.59"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed055ab27f941423197eb86b2035720b1a3ce40504df082cac2ecc6ed73335a1"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"

View file

@ -5,13 +5,28 @@ edition = "2021"
[lib] [lib]
name = "ivyrs" name = "ivyrs"
crate-type = ["cdylib"] crate-type = ["cdylib", "rlib"]
path = "rust/lib.rs" path = "rust/lib.rs"
[dependencies] [dependencies]
ignore = "0.4" ignore = "0.4"
fuzzy-matcher = "0.3.7" fuzzy-matcher = "0.3.7"
lazy_static = "1.4.0" lazy_static = "1.4.0"
rayon = "1.5.3"
[dev-dependencies]
criterion = "0.3.6"
[profile.release] [profile.release]
opt-level = 3 opt-level = 3
[profile.bench]
debug = true
[[bench]]
name = "ivy_match"
harness = false
[[bench]]
name = "ivy_files"
harness = false

View file

@ -78,7 +78,7 @@ to optimize, you will probably need to get a baseline on your hardware.
There are fixtures provided that will create the directory structure of the There are fixtures provided that will create the directory structure of the
[kubernetes](https://github.com/kubernetes/kubernetes) source code, from [kubernetes](https://github.com/kubernetes/kubernetes) source code, from
somewhere arround commit sha 985c9202ccd250a5fe22c01faf0d8f83d804b9f3. This will somewhere around commit sha 985c9202ccd250a5fe22c01faf0d8f83d804b9f3. This will
create a directory tree of 23511 files a relative large source tree to get a create a directory tree of 23511 files a relative large source tree to get a
good idea of performance. To create the source tree under good idea of performance. To create the source tree under
`/tmp/ivy-trees/kubernetes` run the following command. This will need to be run `/tmp/ivy-trees/kubernetes` run the following command. This will need to be run
@ -97,14 +97,14 @@ running on GCP.
Rust Rust
| Name | Total | Adverage | Min | Max | | Name | Total | Average | Min | Max |
|--------------------------------|---------------|---------------|---------------|---------------| |--------------------------------|---------------|---------------|---------------|---------------|
| ivy_match(file.lua) 1000000x | 03.961640 (s) | 00.000004 (s) | 00.000003 (s) | 00.002146 (s) | | ivy_match(file.lua) 1000000x | 03.961640 (s) | 00.000004 (s) | 00.000003 (s) | 00.002146 (s) |
| ivy_files(kubernetes) 100x | 03.895758 (s) | 00.038958 (s) | 00.034903 (s) | 00.043660 (s) | | ivy_files(kubernetes) 100x | 03.895758 (s) | 00.038958 (s) | 00.034903 (s) | 00.043660 (s) |
CPP CPP
| Name | Total | Adverage | Min | Max | | Name | Total | Average | Min | Max |
|--------------------------------|---------------|---------------|---------------|---------------| |--------------------------------|---------------|---------------|---------------|---------------|
| ivy_match(file.lua) 1000000x | 01.855197 (s) | 00.000002 (s) | 00.000001 (s) | 00.000177 (s) | | ivy_match(file.lua) 1000000x | 01.855197 (s) | 00.000002 (s) | 00.000001 (s) | 00.000177 (s) |
| ivy_files(kubernetes) 100x | 14.696396 (s) | 00.146964 (s) | 00.056604 (s) | 00.168478 (s) | | ivy_files(kubernetes) 100x | 14.696396 (s) | 00.146964 (s) | 00.056604 (s) | 00.168478 (s) |

17
benches/ivy_files.rs Normal file
View file

@ -0,0 +1,17 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use ivyrs::inner_files;
pub fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("ivy_files(kubernetes)", |b| {
b.iter(|| {
inner_files(
black_box("file.go".to_owned()),
black_box("/tmp/ivy-trees/kubernetes".to_owned()),
)
})
});
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

17
benches/ivy_match.rs Normal file
View file

@ -0,0 +1,17 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use ivyrs::inner_match;
pub fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("ivy_match(file.lua)", |b| {
b.iter(|| {
inner_match(
black_box("file.lua".to_owned()),
black_box("some/long/path/to/file/file.lua".to_owned()),
)
})
});
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

View file

@ -0,0 +1,7 @@
use ivyrs::inner_files;
pub fn main() {
let res = inner_files("file.go".to_owned(), "/tmp/ivy-trees/kubernetes".to_owned());
println!("{}", res);
}

View file

@ -22,5 +22,5 @@ pub fn find_files(options: Options) -> Vec<String> {
files.push(candidate_path.to_str().unwrap().to_string()); files.push(candidate_path.to_str().unwrap().to_string());
} }
return files; files
} }

View file

@ -1,30 +1,34 @@
mod matcher;
mod finder; mod finder;
mod matcher;
mod sorter; mod sorter;
mod thread_pool;
use std::sync::Mutex;
use std::collections::HashMap; use std::collections::HashMap;
use std::os::raw::{c_int, c_char};
use std::ffi::CString;
use std::ffi::CStr; use std::ffi::CStr;
use std::ffi::CString;
use std::os::raw::{c_char, c_int};
use std::sync::Mutex;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
lazy_static! { lazy_static! {
static ref GLOBAL_FILE_CACHE: Mutex<HashMap<String, Vec<String>>> = return Mutex::new(HashMap::new()) ; static ref GLOBAL_FILE_CACHE: Mutex<HashMap<String, Vec<String>>> = Mutex::new(HashMap::new());
} }
fn to_string(input: *const c_char) -> String { fn to_string(input: *const c_char) -> String {
return unsafe { CStr::from_ptr(input) }.to_str().unwrap().to_string(); unsafe { CStr::from_ptr(input) }
.to_str()
.unwrap()
.to_string()
} }
fn get_files(directory: &String) -> Vec<String> { fn get_files(directory: &String) -> Vec<String> {
let mut cache = GLOBAL_FILE_CACHE.lock().unwrap(); let mut cache = GLOBAL_FILE_CACHE.lock().unwrap();
if !cache.contains_key(directory) { if !cache.contains_key(directory) {
let finder_options = finder::Options{ directory: directory.clone() }; let finder_options = finder::Options {
cache.insert( directory.clone(), finder::find_files(finder_options)); directory: directory.clone(),
};
cache.insert(directory.clone(), finder::find_files(finder_options));
} }
return cache.get(directory).unwrap().to_vec(); return cache.get(directory).unwrap().to_vec();
@ -41,8 +45,13 @@ pub extern "C" fn ivy_match(c_pattern: *const c_char, c_text: *const c_char) ->
let pattern = to_string(c_pattern); let pattern = to_string(c_pattern);
let text = to_string(c_text); let text = to_string(c_text);
let m = matcher::Matcher::new( pattern ); inner_match(pattern, text)
return m.score(text) as i32; }
pub fn inner_match(pattern: String, text: String) -> i32 {
let m = matcher::Matcher::new(pattern);
m.score(text.as_str()) as i32
} }
#[no_mangle] #[no_mangle]
@ -50,22 +59,28 @@ pub extern "C" fn ivy_files(c_pattern: *const c_char, c_base_dir: *const c_char)
let pattern = to_string(c_pattern); let pattern = to_string(c_pattern);
let directory = to_string(c_base_dir); let directory = to_string(c_base_dir);
// Bail out early if the pattern is empty its never going to find anything let output = inner_files(pattern, directory);
CString::new(output).unwrap().into_raw()
}
pub fn inner_files(pattern: String, base_dir: String) -> String {
let mut output = String::new();
// Bail out early if the pattern is empty; it's never going to find anything
if pattern.is_empty() { if pattern.is_empty() {
return CString::new("").unwrap().into_raw() return output;
} }
let files = get_files(&directory); let files = get_files(&base_dir);
let mut output = String::new();
let sorter_options = sorter::Options::new(pattern); let sorter_options = sorter::Options::new(pattern);
let files = sorter::sort_strings(sorter_options, files); let files = sorter::sort_strings(sorter_options, files);
for file in files.lock().unwrap().iter() { for file in files.iter() {
output.push_str(&file.content); output.push_str(&file.content);
output.push('\n'); output.push('\n');
} }
return CString::new(output).unwrap().into_raw() output
} }

View file

@ -1,5 +1,5 @@
use fuzzy_matcher::FuzzyMatcher;
use fuzzy_matcher::skim::SkimMatcherV2; use fuzzy_matcher::skim::SkimMatcherV2;
use fuzzy_matcher::FuzzyMatcher;
pub struct Matcher { pub struct Matcher {
/// The search pattern that we want to match against some text /// The search pattern that we want to match against some text
@ -9,17 +9,16 @@ pub struct Matcher {
impl Matcher { impl Matcher {
pub fn new(pattern: String) -> Self { pub fn new(pattern: String) -> Self {
return Self { Self {
pattern, pattern,
matcher: SkimMatcherV2::default(), matcher: SkimMatcherV2::default(),
} }
} }
pub fn score(self: &Self, text: String) -> i64 { pub fn score(&self, text: &str) -> i64 {
if let Some((score, _indices)) = self.matcher.fuzzy_indices(&text, &self.pattern) { self.matcher
return score; .fuzzy_indices(text, &self.pattern)
} .map(|(score, _indices)| score)
.unwrap_or_default()
return 0;
} }
} }

View file

@ -1,9 +1,5 @@
use super::matcher; use super::matcher;
use super::thread_pool; use rayon::prelude::*;
use std::sync::Mutex;
use std::sync::Arc;
pub struct Match { pub struct Match {
pub score: i64, pub score: i64,
@ -12,37 +8,29 @@ pub struct Match {
pub struct Options { pub struct Options {
pub pattern: String, pub pattern: String,
pub minimun_score: i64, pub minimum_score: i64,
} }
impl Options { impl Options {
pub fn new(pattern: String) -> Self { pub fn new(pattern: String) -> Self {
return Self { pattern, minimun_score: 20 }; Self {
pattern,
minimum_score: 25,
}
} }
} }
pub fn sort_strings(options: Options, strings: Vec<String>) -> Arc<Mutex<Vec<Match>>> { pub fn sort_strings(options: Options, strings: Vec<String>) -> Vec<Match> {
let matches: Arc<Mutex<Vec<Match>>> = Arc::new(Mutex::new(Vec::new())); let matcher = matcher::Matcher::new(options.pattern);
let matcher = Arc::new(Mutex::new(matcher::Matcher::new(options.pattern)));
let pool = thread_pool::ThreadPool::new(std::thread::available_parallelism().unwrap().get()); let mut matches = strings
.into_par_iter()
for string in strings { .map(|candidate| Match {
let thread_matcher = Arc::clone(&matcher); score: matcher.score(candidate.as_str()),
let thread_matches = Arc::clone(&matches); content: candidate,
pool.execute(move || {
let score = thread_matcher.lock().unwrap().score(string.to_string());
if score > 25 {
let mut tmp = thread_matches.lock().unwrap();
let content = string.clone();
tmp.push(Match{ score, content });
}
}) })
} .filter(|m| m.score > options.minimum_score)
.collect::<Vec<Match>>();
drop(pool); matches.par_sort_unstable_by(|a, b| a.score.cmp(&b.score));
matches
matches.lock().unwrap().sort_by(|a, b| a.score.cmp(&b.score));
return matches;
} }

View file

@ -1,87 +0,0 @@
use std::sync::mpsc;
use std::sync::Arc;
use std::sync::Mutex;
use std::thread;
enum Message {
NewJob(Job),
Terminate,
}
pub struct ThreadPool {
jobs: mpsc::Sender<Message>,
threads: Vec<Worker>,
}
trait FnBox {
fn call_box(self: Box<Self>);
}
impl<F: FnOnce()> FnBox for F {
fn call_box(self: Box<F>) {
(*self)()
}
}
type Job = Box<dyn FnBox + Send + 'static>;
impl ThreadPool {
pub fn new(thread_count: usize) -> Self {
let (jobs, receiver) = mpsc::channel();
let receiver = Arc::new(Mutex::new(receiver));
let mut threads: Vec<Worker> = Vec::new();
for id in 1..thread_count {
threads.push(Worker::new(id, Arc::clone(&receiver)));
}
return ThreadPool { jobs, threads };
}
pub fn execute<F>(&self, f: F)
where
F: FnOnce() + Send + 'static,
{
let job = Box::new(f);
self.jobs.send(Message::NewJob(job)).unwrap();
}
}
impl Drop for ThreadPool {
fn drop(&mut self) {
for _ in &mut self.threads {
self.jobs.send(Message::Terminate).unwrap();
}
for worker in &mut self.threads {
if let Some(thread) = worker.thread.take() {
thread.join().unwrap();
}
}
}
}
struct Worker {
id: usize,
thread: Option<thread::JoinHandle<()>>,
}
impl Worker {
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Message>>>) -> Worker {
let thread = thread::spawn(move || loop {
let message = receiver.lock().unwrap().recv().unwrap();
match message {
Message::NewJob(job) => job.call_box(),
Message::Terminate => {
break;
}
}
});
return Worker {
id,
thread: Some(thread),
};
}
}

View file

@ -35,7 +35,7 @@ local benchmark = function(name, n, callback)
) )
end end
print "| Name | Total | Adverage | Min | Max |" print "| Name | Total | Average | Min | Max |"
print "|--------------------------------|---------------|---------------|---------------|---------------|" print "|--------------------------------|---------------|---------------|---------------|---------------|"
benchmark("ivy_match(file.lua) 1000000x", 1000000, function() benchmark("ivy_match(file.lua) 1000000x", 1000000, function()