Skip to content

Commit

Permalink
fix: cow implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
karatakis committed Dec 2, 2024
1 parent eb5abd7 commit ee2cbcd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 72 deletions.
13 changes: 13 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ criterion = "0.5.1"
serde_json = { workspace = true }
nom = { workspace = true }

[profile.release]
opt-level = 3
codegen-units = 1
panic = 'abort'
lto = 'fat'
debug = false
incremental = false
overflow-checks = false

[profile.benchmark]
inherits = "release"
debug = true

[workspace]
members = ["tailcall-template"]

Expand Down
12 changes: 6 additions & 6 deletions benches/jaq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,21 +218,21 @@ fn bench_jq(data: &serde_json::Value, filter: &jaq_core::Filter<Native<Val>>, ex
assert_eq!(out.next(), None);
}

fn bench_jsonlike(
data: &serde_json::Value,
filter: &jaq_core::Filter<Native<JsonLikeHelper<serde_json::Value>>>,
fn bench_jsonlike<'a>(
data: &'a serde_json::Value,
filter: &jaq_core::Filter<Native<JsonLikeHelper<'a, serde_json::Value>>>,
expected: &str,
) {
let inputs = RcIter::new(core::iter::empty());

// iterator over the output values
let mut out = filter.run((Ctx::new([], &inputs), JsonLikeHelper(data.clone())));
let mut out = filter.run((Ctx::new([], &inputs), JsonLikeHelper(std::borrow::Cow::Borrowed(&data))));

assert_eq!(
out.next(),
Some(Ok(JsonLikeHelper(serde_json::Value::String(
Some(Ok(JsonLikeHelper(std::borrow::Cow::Owned(serde_json::Value::String(
expected.to_string()
))))
)))))
);
assert_eq!(out.next(), None);
}
90 changes: 24 additions & 66 deletions tailcall-template/src/jq/jq.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
use std::{borrow::Cow, ops::Deref};
use std::{borrow::Cow, fmt::Debug, ops::Deref};

use jaq_core::ValR;

use crate::jsonlike::{JsonLike, JsonObjectLike};
use crate::jsonlike::{JsonLike, JsonLikeOwned, JsonObjectLike};

#[derive(Debug, Clone, PartialEq)]
pub struct JsonLikeHelper<'json,
A: JsonLike<'json> + std::fmt::Display + std::clone::Clone + std::cmp::PartialEq,
>(pub Cow<'json, A>);
pub struct JsonLikeHelper<'json, A: JsonLikeOwned + Clone + PartialEq>(pub Cow<'json, A>);

impl<'json, A> Deref for JsonLikeHelper<'json, A>
where
A: JsonLike<'json> + std::fmt::Display + std::clone::Clone + std::cmp::PartialEq,
impl<'json, A: JsonLikeOwned + Clone + PartialEq> Deref for JsonLikeHelper<'json, A>
{
type Target = A;

Expand All @@ -20,18 +16,14 @@ where
}
}

impl<'json, A> From<A> for JsonLikeHelper<'json, A>
where
A: JsonLike<'json> + std::fmt::Display + std::clone::Clone + std::cmp::PartialEq,
impl<'json, A: JsonLikeOwned + Clone + PartialEq> From<A> for JsonLikeHelper<'json, A>
{
fn from(value: A) -> Self {
Self(Cow::Owned(value))
}
}

impl<'json, A> jaq_core::ValT for JsonLikeHelper<'json, A>
where
A: JsonLike<'json> + std::fmt::Display + std::clone::Clone + std::cmp::PartialEq,
impl<'json, A: JsonLikeOwned + Clone + PartialEq + std::fmt::Display> jaq_core::ValT for JsonLikeHelper<'json, A>
{
fn from_num(n: &str) -> ValR<Self> {
match n.parse::<f64>() {
Expand Down Expand Up @@ -62,17 +54,7 @@ where
}

fn values(self) -> Box<dyn Iterator<Item = ValR<Self>>> {
if let Some(arr) = self.0.as_ref().as_array() {
let owned_array: Vec<_> = arr.iter().cloned().collect();
Box::new(owned_array.into_iter().map(|a| Ok(JsonLikeHelper(Cow::Owned(a)))))
} else if let Some(obj) = self.0.as_ref().as_object() {
let owned_array: Vec<_> = obj.iter().map(|(_k, v)| v.clone()).collect();
Box::new(owned_array.into_iter().map(|a| Ok(JsonLikeHelper(Cow::Owned(a)))))
} else {
Box::new(core::iter::once(ValR::Err(jaq_core::Error::str(
"Value is not object or array",
))))
}
unimplemented!()
}

fn index(self, index: &Self) -> ValR<Self> {
Expand Down Expand Up @@ -101,7 +83,7 @@ where

fn range(self, range: jaq_core::val::Range<&Self>) -> ValR<Self> {
let (from, upto) = (range.start, range.end);
if let Some(a) = self.0.into_owned().into_array() {
if let Some(a) = self.0.clone().into_owned().into_array() {
let len = a.len();

let from = from
Expand Down Expand Up @@ -138,7 +120,7 @@ where
.map(|v| JsonLikeHelper(Cow::Owned(v)))
.collect()
})
} else if let Some(s) = self.0.as_ref().as_str() {
} else if let Some(s) = &self.0.as_ref().as_str() {
let len = s.chars().count();

let from = from
Expand Down Expand Up @@ -180,10 +162,10 @@ where
opt: jaq_core::path::Opt,
f: impl Fn(Self) -> I,
) -> jaq_core::ValX<'a, Self> {
if let Some(arr) = self.0.as_ref().as_array() {
if let Some(arr) = self.0.as_array() {
let iter = arr.iter().map(|a| JsonLikeHelper(Cow::Owned(a.clone()))).flat_map(f);
Ok(iter.collect::<Result<_, _>>()?)
} else if let Some(obj) = self.0.clone().as_ref().as_object() {
} else if let Some(obj) = self.0.as_object() {
let iter = obj
.iter()
.filter_map(|(k, v)| f(JsonLikeHelper(Cow::Owned(v.clone()))).next().map(|v| Ok((k, v?.0.into_owned()))));
Expand Down Expand Up @@ -340,65 +322,51 @@ where
}
}

impl<'json, A> PartialOrd for JsonLikeHelper<'json, A>
where
A: JsonLike<'json> + std::fmt::Display + std::clone::Clone + std::cmp::PartialEq,
impl<'json, A: JsonLikeOwned + Clone + PartialEq + std::fmt::Display> PartialOrd for JsonLikeHelper<'json, A>
{
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
// TODO: compare properly
self.0.to_string().partial_cmp(&other.0.to_string())
}
}

impl<'json, A> std::fmt::Display for JsonLikeHelper<'json, A>
where
A: JsonLike<'json> + std::fmt::Display + std::clone::Clone + std::cmp::PartialEq,
impl<'json, A: JsonLikeOwned + Clone + PartialEq + std::fmt::Display> std::fmt::Display for JsonLikeHelper<'json, A>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}

impl<'json, A> From<bool> for JsonLikeHelper<'json, A>
where
A: JsonLike<'json> + std::fmt::Display + std::clone::Clone + std::cmp::PartialEq,
impl<'json, A: JsonLikeOwned + Clone + PartialEq> From<bool> for JsonLikeHelper<'json, A>
{
fn from(value: bool) -> Self {

Check failure on line 342 in tailcall-template/src/jq/jq.rs

View workflow job for this annotation

GitHub Actions / Build and Test

unused variable: `value`
todo!()
}
}

impl<'json, A> From<isize> for JsonLikeHelper<'json, A>
where
A: JsonLike<'json> + std::fmt::Display + std::clone::Clone + std::cmp::PartialEq,
impl<'json, A: JsonLikeOwned + Clone + PartialEq> From<isize> for JsonLikeHelper<'json, A>
{
fn from(value: isize) -> Self {

Check failure on line 349 in tailcall-template/src/jq/jq.rs

View workflow job for this annotation

GitHub Actions / Build and Test

unused variable: `value`
todo!()
}
}

impl<'json, A> From<String> for JsonLikeHelper<'json, A>
where
A: JsonLike<'json> + std::fmt::Display + std::clone::Clone + std::cmp::PartialEq,
impl<'json, A: JsonLikeOwned + Clone + PartialEq> From<String> for JsonLikeHelper<'json, A>
{
fn from(value: String) -> Self {
JsonLikeHelper(Cow::Owned(JsonLike::string(Cow::Owned(value))))
}
}

impl<'json, A> FromIterator<Self> for JsonLikeHelper<'json, A>
where
A: JsonLike<'json> + std::fmt::Display + std::clone::Clone + std::cmp::PartialEq,
impl<'json, A: JsonLikeOwned + Clone + PartialEq> FromIterator<Self> for JsonLikeHelper<'json, A>
{
fn from_iter<T: IntoIterator<Item = Self>>(iter: T) -> Self {

Check failure on line 363 in tailcall-template/src/jq/jq.rs

View workflow job for this annotation

GitHub Actions / Build and Test

unused variable: `iter`
todo!()
}
}
impl<'json, A> std::ops::Add for JsonLikeHelper<'json, A>
where
A: JsonLike<'json> + std::fmt::Display + std::clone::Clone + std::cmp::PartialEq,
impl<'json, A: JsonLikeOwned + Clone + PartialEq> std::ops::Add for JsonLikeHelper<'json, A>
{
type Output = ValR<JsonLikeHelper<'json, A>>;
type Output = ValR<Self>;
fn add(mut self, rhs: Self) -> Self::Output {
if self.0.as_ref().is_null() && rhs.0.as_ref().is_null() {
return Ok(self);
Expand All @@ -423,49 +391,39 @@ where
}
}

impl<'json, A> std::ops::Sub for JsonLikeHelper<'json, A>
where
A: JsonLike<'json> + std::fmt::Display + std::clone::Clone + std::cmp::PartialEq,
impl<'json, A: JsonLikeOwned + Clone + PartialEq> std::ops::Sub for JsonLikeHelper<'json, A>
{
type Output = ValR<Self>;
fn sub(self, rhs: Self) -> Self::Output {

Check failure on line 397 in tailcall-template/src/jq/jq.rs

View workflow job for this annotation

GitHub Actions / Build and Test

unused variable: `rhs`
todo!()
}
}

impl<'json, A> std::ops::Mul for JsonLikeHelper<'json, A>
where
A: JsonLike<'json> + std::fmt::Display + std::clone::Clone + std::cmp::PartialEq,
impl<'json, A: JsonLikeOwned + Clone + PartialEq> std::ops::Mul for JsonLikeHelper<'json, A>
{
type Output = ValR<Self>;
fn mul(self, rhs: Self) -> Self::Output {

Check failure on line 405 in tailcall-template/src/jq/jq.rs

View workflow job for this annotation

GitHub Actions / Build and Test

unused variable: `rhs`
todo!()
}
}

impl<'json, A> std::ops::Div for JsonLikeHelper<'json, A>
where
A: JsonLike<'json> + std::fmt::Display + std::clone::Clone + std::cmp::PartialEq,
impl<'json, A: JsonLikeOwned + Clone + PartialEq> std::ops::Div for JsonLikeHelper<'json, A>
{
type Output = ValR<Self>;
fn div(self, rhs: Self) -> Self::Output {

Check failure on line 413 in tailcall-template/src/jq/jq.rs

View workflow job for this annotation

GitHub Actions / Build and Test

unused variable: `rhs`
todo!()
}
}

impl<'json, A> std::ops::Rem for JsonLikeHelper<'json, A>
where
A: JsonLike<'json> + std::fmt::Display + std::clone::Clone + std::cmp::PartialEq,
impl<'json, A: JsonLikeOwned + Clone + PartialEq> std::ops::Rem for JsonLikeHelper<'json, A>
{
type Output = ValR<Self>;
fn rem(self, rhs: Self) -> Self::Output {

Check failure on line 421 in tailcall-template/src/jq/jq.rs

View workflow job for this annotation

GitHub Actions / Build and Test

unused variable: `rhs`
todo!()
}
}

impl<'json, A> std::ops::Neg for JsonLikeHelper<'json, A>
where
A: JsonLike<'json> + std::fmt::Display + std::clone::Clone + std::cmp::PartialEq,
impl<'json, A: JsonLikeOwned + Clone + PartialEq> std::ops::Neg for JsonLikeHelper<'json, A>
{
type Output = ValR<Self>;
fn neg(self) -> Self::Output {
Expand Down

0 comments on commit ee2cbcd

Please sign in to comment.