Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work in progress (do not land): expose arena as type parameter #210

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions benchmark/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#![allow(incomplete_features)]
#![feature(generic_associated_types)]

use std::{io};

use capnp::{message, serialize, serialize_packed};
use capnp::private::arena::{BuilderArena, ReaderArena};
use capnp::traits::Owned;

pub mod common;
Expand All @@ -43,14 +47,20 @@ pub mod eval_capnp {
pub mod eval;

trait TestCase {
type Request: for<'a> Owned<'a>;
type Response: for<'a> Owned<'a>;
type Request: Owned;
type Response: Owned;
type Expectation;

fn setup_request(&self, rnd: &mut crate::common::FastRand, b: <Self::Request as Owned>::Builder) -> Self::Expectation;
fn handle_request(&self, r: <Self::Request as Owned>::Reader, b: <Self::Response as Owned>::Builder)
-> ::capnp::Result<()>;
fn check_response(&self, r: <Self::Response as Owned>::Reader, e: Self::Expectation) -> ::capnp::Result<()>;
fn setup_request<'a, A>(&self, rnd: &mut crate::common::FastRand, b: <Self::Request as Owned>::Builder<'a, A>)
-> Self::Expectation where A: BuilderArena;

fn handle_request<'a, 'b, A, B>(&self,
r: <Self::Request as Owned>::Reader<'a, A>,
b: <Self::Response as Owned>::Builder<'b, B>)
-> ::capnp::Result<()> where A: ReaderArena, B: BuilderArena;

fn check_response<'a, A>(&self, r: <Self::Response as Owned>::Reader<'a, A>, e: Self::Expectation)
-> ::capnp::Result<()> where A: ReaderArena;
}

trait Serialize {
Expand Down
25 changes: 16 additions & 9 deletions benchmark/carsales.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

use capnp::private::arena::{BuilderArena, ReaderArena};

use crate::common::*;
use crate::carsales_capnp::{parking_lot, total_value, Color, car};

Expand All @@ -27,8 +29,8 @@ trait CarValue {
}

macro_rules! car_value_impl(
($typ:ident) => (
impl <'a> CarValue for car::$typ<'a> {
($typ:ident, $arena_bound:ident) => (
impl <'a, A> CarValue for car::$typ<'a, A> where A: $arena_bound {
fn car_value (mut self) -> ::capnp::Result<u64> {
#![allow(unused_mut)]
let mut result : u64 = 0;
Expand Down Expand Up @@ -75,13 +77,13 @@ macro_rules! car_value_impl(
)
);

car_value_impl!(Reader);
car_value_impl!(Builder);
car_value_impl!(Reader, ReaderArena);
car_value_impl!(Builder, BuilderArena);

const MAKES : [&'static str; 5] = ["Toyota", "GM", "Ford", "Honda", "Tesla"];
const MODELS : [&'static str; 6] = ["Camry", "Prius", "Volt", "Accord", "Leaf", "Model S"];

pub fn random_car(rng: &mut FastRand, mut car: car::Builder) {
pub fn random_car<A>(rng: &mut FastRand, mut car: car::Builder<A>) where A: BuilderArena {
car.set_make(MAKES[rng.next_less_than(MAKES.len() as u32) as usize]);
car.set_model(MODELS[rng.next_less_than(MODELS.len() as u32) as usize]);

Expand Down Expand Up @@ -133,7 +135,9 @@ impl crate::TestCase for CarSales {
type Response = total_value::Owned;
type Expectation = u64;

fn setup_request(&self, rng: &mut FastRand, request: parking_lot::Builder) -> u64 {
fn setup_request<A>(&self, rng: &mut FastRand, request: parking_lot::Builder<A>) -> u64
where A: BuilderArena
{
let mut result = 0;
let mut cars = request.init_cars(rng.next_less_than(200));
for ii in 0.. cars.len() {
Expand All @@ -145,8 +149,9 @@ impl crate::TestCase for CarSales {
result
}

fn handle_request(&self, request: parking_lot::Reader, mut response: total_value::Builder)
-> ::capnp::Result<()>
fn handle_request<A,B>(&self, request: parking_lot::Reader<A>, mut response: total_value::Builder<B>)
-> ::capnp::Result<()>
where A: ReaderArena, B: BuilderArena
{
let mut result = 0;
for car in request.get_cars()?.iter() {
Expand All @@ -156,7 +161,9 @@ impl crate::TestCase for CarSales {
Ok(())
}

fn check_response(&self, response: total_value::Reader, expected: u64) -> ::capnp::Result<()> {
fn check_response<A>(&self, response: total_value::Reader<A>, expected: u64) -> ::capnp::Result<()>
where A: ReaderArena
{
if response.get_amount() == expected {
Ok(())
} else {
Expand Down
34 changes: 25 additions & 9 deletions benchmark/catrank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,27 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

use capnp::private::arena::{BuilderArena, ReaderArena};

use crate::common::*;
use crate::catrank_capnp::*;

#[derive(Clone, Copy)]
pub struct ScoredResult<'a> {
pub struct ScoredResult<'a, A> {
score: f64,
result: search_result::Reader<'a>
result: search_result::Reader<'a, A>
}

impl <'a, A> Clone for ScoredResult<'a, A> {
fn clone(&self) -> Self {
Self {
score: self.score,
result: self.result,
}
}
}

impl <'a, A> Copy for ScoredResult<'a, A> {}

const URL_PREFIX: &'static str = "http://example.com";

pub struct CatRank;
Expand All @@ -37,7 +49,9 @@ impl crate::TestCase for CatRank {
type Response = search_result_list::Owned;
type Expectation = i32;

fn setup_request(&self, rng: &mut FastRand, request: search_result_list::Builder) -> i32 {
fn setup_request<A>(&self, rng: &mut FastRand, request: search_result_list::Builder<A>) -> i32
where A: BuilderArena
{
let count = rng.next_less_than(1000);
let mut good_count: i32 = 0;

Expand Down Expand Up @@ -84,10 +98,11 @@ impl crate::TestCase for CatRank {
good_count
}

fn handle_request(&self, request: search_result_list::Reader,
response: search_result_list::Builder) -> ::capnp::Result<()>
fn handle_request<A,B>(&self, request: search_result_list::Reader<A>,
response: search_result_list::Builder<B>) -> ::capnp::Result<()>
where A: ReaderArena, B: BuilderArena
{
let mut scored_results: Vec<ScoredResult> = Vec::new();
let mut scored_results: Vec<ScoredResult<A>> = Vec::new();

let results = request.get_results()?;
for i in 0..results.len() {
Expand Down Expand Up @@ -119,8 +134,9 @@ impl crate::TestCase for CatRank {
Ok(())
}

fn check_response(&self, response: search_result_list::Reader, expected_good_count: i32)
-> ::capnp::Result<()>
fn check_response<A>(&self, response: search_result_list::Reader<A>, expected_good_count: i32)
-> ::capnp::Result<()>
where A: ReaderArena
{
let mut good_count : i32 = 0;
let results = response.get_results()?;
Expand Down
17 changes: 11 additions & 6 deletions benchmark/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

use capnp::private::arena::{BuilderArena, ReaderArena};

use crate::common::*;
use crate::eval_capnp::{expression, evaluation_result, Operation};

fn make_expression(rng: &mut FastRand, mut exp: expression::Builder, depth : u32) -> i32 {
fn make_expression(rng: &mut FastRand, mut exp: expression::Builder<impl BuilderArena>, depth : u32) -> i32 {
exp.set_op(::capnp::traits::FromU16::from_u16(rng.next_less_than( Operation::Modulus as u32 + 1) as u16).unwrap());

let left : i32 =
Expand Down Expand Up @@ -52,7 +54,7 @@ fn make_expression(rng: &mut FastRand, mut exp: expression::Builder, depth : u32
}
}

fn evaluate_expression(exp: expression::Reader) -> ::capnp::Result<i32> {
fn evaluate_expression(exp: expression::Reader<impl ReaderArena>) -> ::capnp::Result<i32> {
let left = match exp.get_left().which()? {
expression::left::Value(v) => v,
expression::left::Expression(e) => evaluate_expression(e?)?,
Expand All @@ -78,18 +80,21 @@ impl crate::TestCase for Eval {
type Response = evaluation_result::Owned;
type Expectation = i32;

fn setup_request(&self, rng: &mut FastRand, request: expression::Builder) -> i32 {
fn setup_request<A>(&self, rng: &mut FastRand, request: expression::Builder<A>) -> i32 where A: BuilderArena {
make_expression(rng, request, 0)
}

fn handle_request(&self, request: expression::Reader, mut response: evaluation_result::Builder)
-> ::capnp::Result<()>
fn handle_request<A,B>(&self, request: expression::Reader<A>, mut response: evaluation_result::Builder<B>)
-> ::capnp::Result<()>
where A: ReaderArena, B: BuilderArena
{
response.set_value(evaluate_expression(request)?);
Ok(())
}

fn check_response(&self, response: evaluation_result::Reader, expected : i32) -> ::capnp::Result<()> {
fn check_response<A>(&self, response: evaluation_result::Reader<A>, expected : i32) -> ::capnp::Result<()>
where A: ReaderArena
{
if response.get_value() == expected {
Ok(())
} else {
Expand Down
Loading