Changeset 79b8dc3


Ignore:
Timestamp:
Apr 14, 2017, 6:24:35 PM (4 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
17f27d40, 1c38f5b, 6eb4398
Parents:
4570131
Message:

Some compaction of benchmark code

Location:
doc/generic_types
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • doc/generic_types/evaluation/cfa-bench.c

    r4570131 r79b8dc3  
    11#include <stdlib>
    2 #include <stdlib.h>
    32#include <stdio.h>
    43#include "pair"
     
    76#include "cfa-print.h"
    87
    9 int main(int argc, char** argv) {
    10         FILE* out = fopen("cfa-out.txt", "w");
    11         srand(20171025);
     8int main( int argc, char *argv[] ) {
     9        FILE * out = fopen( "cfa-out.txt", "w" );
     10        int max = 0;
     11        stack(int) s, t;
     12        REPEAT_TIMED( "push_int", push( &s, _i ); )
     13        TIMED( "copy_int", t = s; )
     14        TIMED( "clear_int", clear( &s ); )
     15        REPEAT_TIMED( "pop_int", max = max( max, pop( &t ) ); )
     16        REPEAT_TIMED( "print_int", print( out, _i, ":", _i, "\n" ); )
    1217
    13         stack(int) s;
    14         REPEAT_TIMED( "push_int",
    15                 push( &s, rand() );
    16         )
    17 
    18         stack(int) t;
    19         TIMED( "copy_int",
    20                 t = s;
    21         )
    22 
    23         TIMED( "clear_int",
    24                 clear( &s );
    25         )
    26 
    27         int max = 0;
    28         REPEAT_TIMED( "pop_int",
    29                 max = max( max, pop( &t ) );
    30         )
    31         print( out, max, "\n" );
    32 
    33         REPEAT_N_TIMED( "print_int", N/2,
    34                 print( out, rand(), ":", rand(), "\n" );
    35         )
    36 
    37         stack(pair(_Bool, char)) s2;
    38         REPEAT_TIMED( "push_bool_char",
    39                 push( &s2, (pair(_Bool, char)){ rand() & 0x1, rand() & 0x7F } );
    40         )
    41 
    42         stack(pair(_Bool, char)) t2;
    43         TIMED( "copy_bool_char",
    44                 t2 = s2;
    45         )
    46 
    47         TIMED( "clear_bool_char",
    48                 clear( &s2 );
    49         )
    50 
    51         pair(_Bool, char) max2 = { (_Bool)0, '\0' };
    52         REPEAT_TIMED( "pop_bool_char",
    53                 max2 = max( max2, pop( &t2 ) );
    54         )
    55         print( out, max2, "\n" );
    56 
    57         REPEAT_N_TIMED( "print_pair", N/2,
    58                 print( out, (pair(_Bool, char)){ rand() & 0x1, rand() & 0x7F }, ":",
    59                                 (pair(_Bool, char)){ rand() & 0x1, rand() & 0x7F }, "\n" );
    60         )
     18        stack(pair(_Bool, char)) s1, t1;
     19        pair(_Bool, char) max = { (_Bool)0, '\0' };
     20        REPEAT_TIMED( "push_pair", push( &s1, (pair(_Bool, char)){ _i & 1, _i &0x7F } ); )
     21        TIMED( "copy_pair", t1 = s1; )
     22        TIMED( "clear_pair", clear( &s1 ); )
     23        REPEAT_TIMED( "pop_pair", max = max( max, pop( &t1 ) ); )
     24        REPEAT_TIMED( "print_pair",
     25                 print( out, (pair(_Bool, char)){  _i & 1, _i &0x7F }, ":", (pair(_Bool, char)){  _i & 1, _i &0x7F }, "\n" ); )
    6126        fclose(out);
    6227}
  • doc/generic_types/evaluation/cpp-bench.cpp

    r4570131 r79b8dc3  
    11#include <algorithm>
    22#include <fstream>
    3 #include <stdlib.h>
    43#include <utility>
    54#include "bench.hpp"
     
    98int main(int argc, char** argv) {
    109        std::ofstream out{"cpp-out.txt"};
    11         srand(20171025);
     10        stack<int> s, t;
     11        int max = 0;
     12        REPEAT_TIMED( "push_int", s.push( _i ); )
     13        TIMED( "copy_int", t = s; )
     14        TIMED( "clear_int", s.clear(); )
     15        REPEAT_TIMED( "pop_int", max = std::max( max, t.pop() ); )
     16        print( out, max, "\n" );
     17        REPEAT_N_TIMED( "print_int", N/2, print( out, _i, ":", _i, "\n" ); )
    1218
    13         stack<int> s;
    14         REPEAT_TIMED( "push_int",
    15                 s.push( rand() );
    16         )
    17 
    18         stack<int> t;
    19         TIMED( "copy_int",
    20                 t = s;
    21         )
    22 
    23         TIMED( "clear_int",
    24                 s.clear();
    25         )
    26 
    27         int max = 0;
    28         REPEAT_TIMED( "pop_int",
    29                 max = std::max( max, t.pop() );
    30         )
    31         print( out, max, "\n" );
    32 
    33         REPEAT_N_TIMED( "print_int", N/2,
    34                 print( out, rand(), ":", rand(), "\n" );
    35         )
    36 
    37         stack<std::pair<bool, char>> s2;
    38         REPEAT_TIMED( "push_bool_char",
    39                 s2.push( std::pair<bool, char>{ rand() & 0x1, rand() & 0x7F } );
    40         )
    41 
    42         stack<std::pair<bool,char>> t2;
    43         TIMED( "copy_bool_char",
    44                 t2 = s2;
    45         )
    46 
    47         TIMED( "clear_bool_char",
    48                 s2.clear();
    49         )
    50 
    51         std::pair<bool, char> max2 = { false, '\0' };
    52         REPEAT_TIMED( "pop_bool_char",
    53                 max2 = std::max( max2, t2.pop() );
    54         )
    55         print( out, max2, "\n" );
    56 
     19        stack<std::pair<bool, char>> s1, t1;
     20        std::pair<bool, char> max1 = { false, '\0' };
     21        REPEAT_TIMED( "push_bool_char", s1.push( std::pair<bool, char>{ _i & 0x1, _i & 0x7F } ); )
     22        TIMED( "copy_bool_char", t1 = s1; )
     23        TIMED( "clear_bool_char", s1.clear(); )
     24        REPEAT_TIMED( "pop_bool_char", max1 = std::max( max1, t1.pop() ); )
     25        print( out, max1, "\n" );
    5726        REPEAT_N_TIMED( "print_pair", N/2,
    58                 print( out, std::pair<bool, char>{ rand() & 0x1, rand() & 0x7F }, ":",
    59                                 std::pair<bool, char>{ rand() & 0x1, rand() & 0x7F }, "\n" );
    60         )
     27                print( out, std::pair<bool, char>{ _i & 0x1, _i & 0x7F }, ":",
     28                                std::pair<bool, char>{ _i & 0x1, _i & 0x7F }, "\n" ); )
    6129}
  • doc/generic_types/evaluation/cpp-vbench.cpp

    r4570131 r79b8dc3  
    11#include <algorithm>
    22#include <fstream>
    3 #include <stdlib.h>
    43#include "bench.hpp"
    54#include "cpp-vstack.hpp"
     
    98int main(int argc, char** argv) {
    109        std::ofstream out{"cpp-vout.txt"};
    11         srand(20171025);
     10        stack s, t;
     11        integer max{ 0 };
     12        REPEAT_TIMED( "push_int", s.push( make<integer>( _i ) ); )
     13        TIMED( "copy_int", t = s; )
     14        TIMED( "clear_int", s.clear(); )
     15        REPEAT_TIMED( "pop_int", max = std::max( max, t.pop()->as<integer>() ); /***/ )
     16        print( out, max, c_string{"\n"} );
     17        REPEAT_N_TIMED( "print_int", N/2,
     18                print( out, integer{_i}, c_string{":"}, integer{_i}, c_string{"\n"} ); )
    1219
    13         stack s;
    14         REPEAT_TIMED( "push_int",
    15                 s.push( std::make_unique<integer>( rand() ) );
    16         )
    17 
    18         stack t;
    19         TIMED( "copy_int",
    20                 t = s;
    21         )
    22 
    23         TIMED( "clear_int",
    24                 s.clear();
    25         )
    26 
    27         integer max;
    28         REPEAT_TIMED( "pop_int",
    29                 max = std::max( max, t.pop()->as<integer>() ); /***/
    30         )
    31         print( out, max, c_string{"\n"} );
    32 
    33         REPEAT_N_TIMED( "print_int", N/2,
    34                 print( out, integer{rand()}, c_string{":"}, integer{rand()}, c_string{"\n"} );
    35         )
    36 
    37         stack s2;
    38         REPEAT_TIMED( "push_bool_char",
    39                 s2.push( std::make_unique<pair>( std::make_unique<boolean>( rand() & 0x1 ),
    40                         std::make_unique<character>( rand() & 0x7F ) ) );
    41         )
    42 
    43         stack t2;
    44         TIMED( "copy_bool_char",
    45                 t2 = s2;
    46         )
    47 
    48         TIMED( "clear_bool_char",
    49                 s2.clear();
    50         )
    51 
    52         auto max2 = std::make_unique<pair>( std::make_unique<boolean>(false),
    53                 std::make_unique<character>('\0') );
     20        stack s1, t1;
     21        ptr<pair> max1 = make<pair>( make<boolean>(false), make<character>('\0') );
     22        REPEAT_TIMED( "push_bool_char",
     23                s1.push( make<pair>( make<boolean>(_i & 1), make<character>(_i & 0x7F) ) ); )
     24        TIMED( "copy_bool_char", t1 = s1; )
     25        TIMED( "clear_bool_char", s1.clear(); )
    5426        REPEAT_TIMED( "pop_bool_char",
    55                 std::unique_ptr<pair> x = as_ptr<pair>( t2.pop() ); /***/
    56                 if ( *x > *max2 ) { max2 = std::move(x); }
    57         )
    58         print( out, *max2, c_string{"\n"} );
    59 
     27                ptr<pair> x = as_ptr<pair>( t1.pop() ); /***/
     28                if ( *x > *max1 ) { max1 = std::move(x); } )
     29        print( out, *max1, c_string{"\n"} );
    6030        REPEAT_N_TIMED( "print_pair", N/2,
    61                 print( out, pair{ std::make_unique<boolean>( rand() & 0x1 ),
    62                         std::make_unique<character>( rand() & 0x7F ) }, c_string{":"},
    63                         pair{ std::make_unique<boolean>( rand() & 0x1 ),
    64                         std::make_unique<character>( rand() & 0x7F ) }, c_string{"\n"} );
    65         )
     31                print( out, pair{ make<boolean>(_i & 1), make<character>(_i & 0x7F) }, c_string{":"},
     32                        pair{ make<boolean>(_i & 1), make<character>(_i & 0x7F) }, c_string{"\n"} ); )
    6633}
  • doc/generic_types/evaluation/cpp-vstack.cpp

    r4570131 r79b8dc3  
    55stack::node::node( const object& v ) : value( v.new_copy() ), next( nullptr ) {}
    66
    7 stack::node::node( std::unique_ptr<object>&& v, node* n ) : value( std::move(v) ), next( n ) {}
     7stack::node::node( ptr<object>&& v, node* n ) : value( std::move(v) ), next( n ) {}
    88
    99void stack::copy(const stack& o) {
     
    5050}
    5151
    52 bool stack::empty() const {
    53         return head == nullptr;
    54 }
     52bool stack::empty() const { return head == nullptr; }
    5553
    56 void stack::push(std::unique_ptr<object>&& value) {
    57         head = new node{ std::move(value), head }; /***/
    58 }
     54void stack::push(ptr<object>&& value) { head = new node{ std::move(value), head }; /***/ }
    5955
    60 std::unique_ptr<object> stack::pop() {
     56ptr<object> stack::pop() {
    6157        node* n = head;
    6258        head = n->next;
    63         std::unique_ptr<object> x = std::move(n->value);
     59        ptr<object> x = std::move(n->value);
    6460        delete n;
    6561        return x;
  • doc/generic_types/evaluation/cpp-vstack.hpp

    r4570131 r79b8dc3  
    55class stack {
    66        struct node {
    7                 std::unique_ptr<object> value;
     7                ptr<object> value;
    88                node* next;
    99
    1010                node( const object& v );
    1111
    12                 node( std::unique_ptr<object>&& v, node* n );
     12                node( ptr<object>&& v, node* n );
    1313        };
    1414
     
    3434        bool empty() const;
    3535
    36         void push(std::unique_ptr<object>&& value);
     36        void push(ptr<object>&& value);
    3737
    38         std::unique_ptr<object> pop();
     38        ptr<object> pop();
    3939};
  • doc/generic_types/evaluation/object.hpp

    r4570131 r79b8dc3  
    2525std::type_index class_of() { return { typeid(T) }; }
    2626
     27template<typename T> using ptr = std::unique_ptr<T>;
     28
    2729class object {
    2830public:
     
    4345        }
    4446
    45         virtual std::unique_ptr<object> new_inst() const = 0;
    46        
    47         virtual std::unique_ptr<object> new_copy() const = 0;
     47        virtual ptr<object> new_inst() const = 0;
     48       
     49        virtual ptr<object> new_copy() const = 0;
    4850       
    4951        virtual object& operator= (const object&) = 0;
     
    5254};
    5355
     56template<typename T, typename... Args>
     57static inline ptr<T> make(Args&&... args) { return std::make_unique<T>(std::forward<Args>(args)...); }
     58
    5459template<typename To, typename From>
    55 std::unique_ptr<To> as_ptr( std::unique_ptr<From>&& p ) {
    56         return std::unique_ptr<To>{ &p.release()->template as<To>() };
    57 }
     60ptr<To> as_ptr( ptr<From>&& p ) { return ptr<To>{ &p.release()->template as<To>() }; }
    5861
    5962class ordered : public virtual object {
     
    8790        boolean(bool x) : x(x) {}
    8891
    89         std::unique_ptr<object> new_inst() const override { return std::make_unique<boolean>(); }
    90        
    91         std::unique_ptr<object> new_copy() const override { return std::make_unique<boolean>(*this); }
     92        ptr<object> new_inst() const override { return make<boolean>(); }
     93       
     94        ptr<object> new_copy() const override { return make<boolean>(*this); }
    9295
    9396        boolean& operator= (const boolean& that) {
     
    115118        character(char x) : x(x) {}
    116119
    117         std::unique_ptr<object> new_inst() const override { return std::make_unique<character>(); }
    118        
    119         std::unique_ptr<object> new_copy() const override { return std::make_unique<character>(*this); }
     120        ptr<object> new_inst() const override { return make<character>(); }
     121       
     122        ptr<object> new_copy() const override { return make<character>(*this); }
    120123
    121124        character& operator= (const character& that) {
     
    146149        integer(int x) : x(x) {}
    147150
    148         std::unique_ptr<object> new_inst() const override { return std::make_unique<integer>(); }
    149        
    150         std::unique_ptr<object> new_copy() const override { return std::make_unique<integer>(*this); }
     151        ptr<object> new_inst() const override { return make<integer>(); }
     152       
     153        ptr<object> new_copy() const override { return make<integer>(*this); }
    151154
    152155        integer& operator= (const integer& that) {
     
    174177        c_string(const char* s) : s(s) {}
    175178
    176         std::unique_ptr<object> new_inst() const override { return std::make_unique<c_string>(); }
    177 
    178         std::unique_ptr<object> new_copy() const override { return std::make_unique<c_string>(s); }
     179        ptr<object> new_inst() const override { return make<c_string>(); }
     180
     181        ptr<object> new_copy() const override { return make<c_string>(s); }
    179182
    180183        c_string& operator= (const c_string& that) {
     
    191194
    192195class pair : public ordered, public printable {
    193         std::unique_ptr<object> x;
    194         std::unique_ptr<object> y;
     196        ptr<object> x;
     197        ptr<object> y;
    195198
    196199public:
    197200        pair() = default;
    198201
    199         pair(std::unique_ptr<object>&& x, std::unique_ptr<object>&& y)
    200                 : x(std::move(x)), y(std::move(y)) {}
    201        
    202         std::unique_ptr<object> new_inst() const override { return std::make_unique<pair>(); }
    203 
    204         std::unique_ptr<object> new_copy() const override {
    205                 return std::make_unique<pair>(x->new_copy(), y->new_copy());
     202        pair(ptr<object>&& x, ptr<object>&& y) : x(std::move(x)), y(std::move(y)) {}
     203       
     204        ptr<object> new_inst() const override { return make<pair>(); }
     205
     206        ptr<object> new_copy() const override {
     207                return make<pair>(x->new_copy(), y->new_copy());
    206208        }
    207209
  • doc/generic_types/generic_types.tex

    r4570131 r79b8dc3  
    194194The new constructs are empirically compared with both standard C and \CC; the results show the new design is comparable in performance.
    195195
    196 
    197196\subsection{Polymorphic Functions}
    198197\label{sec:poly-fns}
     
    11091108
    11101109In conclusion, the authors' design for generic types and tuples, unlike those available in existing work, is both reusable and type-checked, while still supporting a full range of C features, including separately-compiled modules.
    1111 We have experimentally validated the performance of our design against both \CC and standard C, showing it is \TODO{shiny, cap'n}.
     1110We have experimentally validated the performance of our design against both \CC and standard C, showing it is comparable to both, and in some cases better.
    11121111
    11131112
Note: See TracChangeset for help on using the changeset viewer.