git.delta.rocks / jrsonnet / refs/commits / f51b87e6aff4

difftreelog

perf implement std.findSubstr in native

Yaroslav Bolyukin2022-08-07parent: #0831da3.patch.diff
in: master

4 files changed

modifiedCargo.tomldiffbeforeafterboth
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,6 +8,6 @@
 opt-level = 3
 lto = "fat"
 codegen-units = 1
-# debug = 0
+debug = 0
 panic = "abort"
-# strip = true
+strip = true
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -206,9 +206,13 @@
 	pub fn new_eager() -> Self {
 		Self::Eager(Cc::new(Vec::new()))
 	}
+	pub fn empty() -> Self {
+		Self::new_range(0, 0)
+	}
 
 	/// # Panics
 	/// If a > b
+	#[inline]
 	pub fn new_range(a: i32, b: i32) -> Self {
 		assert!(a <= b);
 		Self::Range(a, b)
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
128 ("splitLimit".into(), builtin_splitlimit::INST),128 ("splitLimit".into(), builtin_splitlimit::INST),
129 ("asciiUpper".into(), builtin_ascii_upper::INST),129 ("asciiUpper".into(), builtin_ascii_upper::INST),
130 ("asciiLower".into(), builtin_ascii_lower::INST),130 ("asciiLower".into(), builtin_ascii_lower::INST),
131 ("findSubstr".into(), builtin_find_substr::INST),
131 ]132 ]
132 .iter()133 .iter()
133 .cloned()134 .cloned()
421 Ok(str.to_ascii_lowercase())422 Ok(str.to_ascii_lowercase())
422}423}
424
425#[builtin]
426fn builtin_find_substr(pat: IStr, str: IStr) -> Result<ArrValue> {
427 if pat.is_empty() || str.is_empty() || pat.len() > str.len() {
428 return Ok(ArrValue::empty());
429 }
430
431 let str = str.as_str();
432 let pat = pat.as_bytes();
433 let strb = str.as_bytes();
434
435 let max_pos = str.len() - pat.len();
436
437 let mut out: Vec<Val> = Vec::new();
438 for (ch_idx, (i, _)) in str
439 .char_indices()
440 .take_while(|(i, _)| i <= &max_pos)
441 .enumerate()
442 {
443 if &strb[i..i + pat.len()] == pat {
444 out.push(Val::Num(ch_idx as f64))
445 }
446 }
447 Ok(out.into())
448}
423449
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -477,19 +477,6 @@
     } else
       a,
 
-  findSubstr(pat, str)::
-    if !std.isString(pat) then
-      error 'findSubstr first parameter should be a string, got ' + std.type(pat)
-    else if !std.isString(str) then
-      error 'findSubstr second parameter should be a string, got ' + std.type(str)
-    else
-      local pat_len = std.length(pat);
-      local str_len = std.length(str);
-      if pat_len == 0 || str_len == 0 || pat_len > str_len then
-        []
-      else
-        std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),
-
   find(value, arr)::
     if !std.isArray(arr) then
       error 'find second parameter should be an array, got ' + std.type(arr)