Changeset a381b46


Ignore:
Timestamp:
Apr 15, 2017, 7:09:59 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:
c57d1935
Parents:
308880c
Message:

Minor cleanup, also filled in benchmark source appendix

Location:
doc/generic_types
Files:
1 added
1 deleted
23 edited

Legend:

Unmodified
Added
Removed
  • doc/generic_types/Makefile

    r308880c ra381b46  
    6666## Define the default recipes.
    6767
    68 ${GRAPHS} : evaluation/timing.gp evaluation/timing.csv
     68${GRAPHS} : evaluation/timing.gp evaluation/timing.dat
    6969        gnuplot evaluation/timing.gp
    7070
  • doc/generic_types/evaluation/Makefile

    r308880c ra381b46  
    6767        @/usr/bin/time -f 'max_memory:\t%M kilobytes' ./c-bench
    6868        @printf 'source_size:\t%8d lines\n' `cat $(CFILES) | wc -l`
    69         @printf 'redundant_type_annotations:%8d count\n' `cat $(CFILES) | fgrep '/***/' -c`
     69        @printf 'redundant_type_annotations:%8d lines\n' `cat $(CFILES) | fgrep '/***/' -c`
    7070        @printf 'binary_size:\t%8d bytes\n' `stat -c %s c-bench`
    7171
     
    7575        @/usr/bin/time -f 'max_memory:\t %M kilobytes' ./cfa-bench
    7676        @printf 'source_size:\t%8d lines\n' `cat $(CFAFILES) | wc -l`
    77         @printf 'redundant_type_annotations:%8d count\n' `cat $(CFAFILES) | fgrep '/***/' -c`
     77        @printf 'redundant_type_annotations:%8d lines\n' `cat $(CFAFILES) | fgrep '/***/' -c`
    7878        @printf 'binary_size:\t%8d bytes\n' `stat -c %s cfa-bench`
    7979
     
    8383        @/usr/bin/time -f 'max_memory:\t %M kilobytes' ./cpp-bench
    8484        @printf 'source_size:\t%8d lines\n' `cat $(CPPFILES) | wc -l`
    85         @printf 'redundant_type_annotations:%8d count\n' `cat $(CPPFILES) | fgrep '/***/' -c`
     85        @printf 'redundant_type_annotations:%8d lines\n' `cat $(CPPFILES) | fgrep '/***/' -c`
    8686        @printf 'binary_size:\t%8d bytes\n' `stat -c %s cpp-bench`
    8787
    8888run-cppv: cpp-vbench
    8989        @echo
    90         @echo '## C++ virtual ##'
     90        @echo '## C++obj ##'
    9191        @/usr/bin/time -f 'max_memory:\t%M kilobytes' ./cpp-vbench
    9292        @printf 'source_size:\t%8d lines\n' `cat $(CPPVFILES) | wc -l`
    93         @printf 'redundant_type_annotations:%8d count\n' `cat $(CPPVFILES) | fgrep '/***/' -c`
     93        @printf 'redundant_type_annotations:%8d lines\n' `cat $(CPPVFILES) | fgrep '/***/' -c`
    9494        @printf 'binary_size:\t%8d bytes\n' `stat -c %s cpp-vbench`
    9595
  • doc/generic_types/evaluation/bench.h

    r308880c ra381b46  
    11#pragma once
    2 
    32#include <stdio.h>
    43#include <time.h>
    54
    6 #ifndef N
     5long ms_between(clock_t start, clock_t end) { return (end - start) / (CLOCKS_PER_SEC / 1000); }
     6
    77#define N 40000000
    8 #endif
    9 
    10 long ms_between(clock_t start, clock_t end) {
    11         return (end - start) / (CLOCKS_PER_SEC / 1000);
    12 }
    13 
    148#define TIMED(name, code) { \
    159        volatile clock_t _start, _end; \
     
    1913        printf("%s:\t%8ld ms\n", name, ms_between(_start, _end)); \
    2014}
    21 
    2215#define REPEAT_N_TIMED(name, n, code) TIMED( name, for (int _i = 0; _i < n; ++_i) { code } )
    23 
    2416#define REPEAT_TIMED(name, code) REPEAT_N_TIMED(name, N, code)
  • doc/generic_types/evaluation/bench.hpp

    r308880c ra381b46  
    11#pragma once
    2 
    32#include <iomanip>
    43#include <iostream>
    54#include <time.h>
    65
    7 #ifndef N
     6long ms_between(clock_t start, clock_t end) { return (end - start) / (CLOCKS_PER_SEC / 1000); }
     7
    88static const int N = 40000000;
    9 #endif
    10 
    11 long ms_between(clock_t start, clock_t end) {
    12         return (end - start) / (CLOCKS_PER_SEC / 1000);
    13 }
    14 
    159#define TIMED(name, code) { \
    1610        volatile clock_t _start, _end; \
     
    1812        code \
    1913        _end = clock(); \
    20         std::cout << name << ":\t" << std::setw(8) << ms_between(_start, _end) << std::setw(0) << " ms" << std::endl; \
     14        std::cout << name << ":\t" << std::setw(8) << ms_between(_start, _end) \
     15                << std::setw(0) << " ms" << std::endl; \
    2116}
    22 
    2317#define REPEAT_N_TIMED(name, n, code) TIMED( name, for (int _i = 0; _i < n; ++_i) { code } )
    24 
    2518#define REPEAT_TIMED(name, code) REPEAT_N_TIMED(name, N, code)
  • doc/generic_types/evaluation/c-bench.c

    r308880c ra381b46  
    2525
    2626void* copy_bool( const void* p ) { return new_bool( *(const _Bool*)p ); } /***/
    27 
    2827void* copy_char( const void* p ) { return new_char( *(const char*)p ); } /***/
    29 
    3028void* copy_int( const void* p ) { return new_int( *(const int*)p ); } /***/
    31 
    3229void* copy_pair_bool_char( const void* p ) { return copy_pair( p, copy_bool, copy_char ); } /***/
    33 
    3430void free_pair_bool_char( void* p ) { free_pair( p, free, free ); } /***/
    3531
    36 int cmp_bool( const void* a, const void* b ) {
    37         return *(const _Bool*)a == *(const _Bool*)b ? 0 : *(const _Bool*)a < *(const _Bool*)b ? -1 : 1; /***/
     32int cmp_bool( const void* a, const void* b ) { /***/
     33        return *(const _Bool*)a == *(const _Bool*)b ? 0 : *(const _Bool*)a < *(const _Bool*)b ? -1 : 1;
    3834}
    3935
     
    4440int main(int argc, char** argv) {
    4541        FILE* out = fopen("c-out.txt", "w");
    46         srand(20171025);
    47 
    48         struct stack s = new_stack();
    49         REPEAT_TIMED( "push_int",
    50                 push_stack(&s, new_int( rand() ));
    51         )
    52 
    53         struct stack t;
    54         TIMED( "copy_int",
    55                 copy_stack(&t, &s, copy_int); /***/
    56         )
    57 
    58         TIMED( "clear_int",
    59                 clear_stack(&s, free); /***/
    60         )
    61 
    6242        int max = 0;
     43        struct stack s = new_stack(), t;
     44        REPEAT_TIMED( "push_int", push_stack(&s, new_int( _i )); )
     45        TIMED( "copy_int",      copy_stack(&t, &s, copy_int); /***/ )
     46        TIMED( "clear_int", clear_stack(&s, free); /***/ )
    6347        REPEAT_TIMED( "pop_int",
    6448                int* x = pop_stack(&t); /***/
    6549                if ( *x > max ) { max = *x; }
    66                 free(x);
    67         )
    68         print( out, "d", max, "\n" ); /***/
    69 
    70         REPEAT_N_TIMED( "print_int", N/2,
    71                 print( out, "dsds", rand(), ":", rand(), "\n" ); /***/
    72         )
    73 
    74         struct stack s2 = new_stack();
    75         REPEAT_TIMED( "push_bool_char",
    76                 push_stack(&s2, new_pair( new_bool( rand() & 0x1 ), new_char( rand() & 0x7F ) ));
    77         )
    78 
    79         struct stack t2;
    80         TIMED( "copy_bool_char",
    81                 copy_stack(&t2, &s2, copy_pair_bool_char); /***/
    82         )
    83 
    84         TIMED( "clear_bool_char",
    85                 clear_stack(&s2, free_pair_bool_char); /***/
    86         )
     50                free(x); )
     51        REPEAT_N_TIMED( "print_int", N/2, print( out, "dsds", _i, ":", _i, "\n" ); /***/ )
    8752
    8853        struct pair* max2 = new_pair( new_bool(0), new_char('\0') );
     54        struct stack s2 = new_stack(), t2;
     55
     56        REPEAT_TIMED( "push_bool_char",
     57                push_stack(&s2, new_pair( new_bool( _i & 0x1 ), new_char( _i & 0x7F ) )); )
     58        TIMED( "copy_bool_char", copy_stack(&t2, &s2, copy_pair_bool_char); /***/ )
     59        TIMED( "clear_bool_char", clear_stack(&s2, free_pair_bool_char); /***/ )
    8960        REPEAT_TIMED( "pop_bool_char",
    9061                struct pair* x = pop_stack(&t2); /***/
     
    9465                } else {
    9566                        free_pair_bool_char( x ); /***/
    96                 }
    97         )
    98         print( out, "pbc", *max2, "\n" ); /***/
    99         free_pair_bool_char( max2 ); /***/
    100 
     67                } )
    10168        REPEAT_N_TIMED( "print_pair", N/2,
    10269                struct pair p1 = ((struct pair){ new_bool(rand() & 0x1), new_char(rand() & 0x7F) }); /***/
     
    10471                print( out, "pbcspbcs", p1, ":", p2, "\n" ); /***/
    10572                free(p1.first); free(p1.second); /***/
    106                 free(p2.first); free(p2.second); /***/
    107         )
    108        
     73                free(p2.first); free(p2.second); /***/ )
     74        free_pair_bool_char( max2 ); /***/
    10975        fclose(out);
    11076}
  • doc/generic_types/evaluation/c-print.h

    r308880c ra381b46  
    11#pragma once
    2 
    32#include <stdio.h>
    43
    54void print_string(FILE* out, const char* x);
    6 
    75void print_bool(FILE* out, _Bool x);
    8 
    96void print_char(FILE* out, char x);
    10 
    117void print_int(FILE* out, int x);
    128
  • doc/generic_types/evaluation/c-stack.c

    r308880c ra381b46  
    77};
    88
    9 struct stack new_stack() {
    10         return (struct stack){ NULL }; /***/
    11 }
     9struct stack new_stack() { return (struct stack){ NULL }; /***/ }
    1210
    1311void copy_stack(struct stack* s, const struct stack* t, void* (*copy)(const void*)) {
     
    3432}
    3533
    36 _Bool stack_empty(const struct stack* s) {
    37         return s->head == NULL;
    38 }
     34_Bool stack_empty(const struct stack* s) { return s->head == NULL; }
    3935
    4036void push_stack(struct stack* s, void* value) {
  • doc/generic_types/evaluation/c-stack.h

    r308880c ra381b46  
    22
    33struct stack_node;
    4 
    54struct stack {
    65        struct stack_node* head;
     
    87
    98struct stack new_stack();
    10 
    119void copy_stack(struct stack* dst, const struct stack* src, void* (*copy)(const void*));
    12 
    1310void clear_stack(struct stack* s, void (*free_el)(void*));
    1411
    1512_Bool stack_empty(const struct stack* s);
    16 
    1713void push_stack(struct stack* s, void* value);
    18 
    1914void* pop_stack(struct stack* s);
  • doc/generic_types/evaluation/cfa-bench.c

    r308880c ra381b46  
    1010        int max = 0;
    1111        stack(int) s, t;
     12
    1213        REPEAT_TIMED( "push_int", push( &s, _i ); )
    1314        TIMED( "copy_int", t = s; )
    1415        TIMED( "clear_int", clear( &s ); )
    15         REPEAT_TIMED( "pop_int", max = max( max, pop( &t ) ); )
     16        REPEAT_TIMED( "pop_int",
     17                int x = pop(&t);
     18                if ( x > max ) { max = x; } )
    1619        REPEAT_TIMED( "print_int", print( out, _i, ":", _i, "\n" ); )
    1720
    1821        stack(pair(_Bool, char)) s1, t1;
    1922        pair(_Bool, char) max = { (_Bool)0, '\0' };
     23
    2024        REPEAT_TIMED( "push_pair", push( &s1, (pair(_Bool, char)){ _i & 1, _i &0x7F } ); )
    2125        TIMED( "copy_pair", t1 = s1; )
    2226        TIMED( "clear_pair", clear( &s1 ); )
    23         REPEAT_TIMED( "pop_pair", max = max( max, pop( &t1 ) ); )
     27        REPEAT_TIMED( "pop_pair",
     28                pair(_Bool, char) x = pop(&t1);
     29                if ( x > max ) { max = x; } )
    2430        REPEAT_TIMED( "print_pair",
    25                  print( out, (pair(_Bool, char)){  _i & 1, _i &0x7F }, ":", (pair(_Bool, char)){  _i & 1, _i &0x7F }, "\n" ); )
     31                 print( out, (pair(_Bool, char)){  _i & 1, _i &0x7F }, ":",
     32                        (pair(_Bool, char)){  _i & 1, _i &0x7F }, "\n" ); )
    2633        fclose(out);
    2734}
  • doc/generic_types/evaluation/cfa-print.h

    r308880c ra381b46  
    11#pragma once
    2 
    32#include <stdio.h>
    43#include "pair"
     
    87
    98void print(FILE* out, const char* x);
    10 
    119void print(FILE* out, _Bool x);
    12 
    1310void print(FILE* out, char x);
    14 
    1511void print(FILE* out, int x);
    1612
  • doc/generic_types/evaluation/cfa-stack.c

    r308880c ra381b46  
    77};
    88
    9 forall(otype T) void ?{}(stack(T)* s) {
    10         (&s->head){ 0 };
    11 }
    12 
    13 forall(otype T) void copy(stack(T)* s, stack(T)* t) {
    14         stack_node(T)** crnt = &s->head;
    15         stack_node(T)* next = t->head;
    16         while ( next ) {
    17                 *crnt = ((stack_node(T)*)malloc()){ next->value }; /***/
    18                 stack_node(T)* acrnt = *crnt;
    19                 crnt = &acrnt->next;
    20                 next = next->next;
    21         }
    22         *crnt = 0;
    23 }
     9forall(otype T) void ?{}(stack(T)* s) { (&s->head){ 0 }; }
    2410
    2511forall(otype T) void ?{}(stack(T)* s, stack(T) t) {
     
    4228}
    4329
    44 forall(otype T) void ^?{}(stack(T)* s) {
    45         clear(s);
    46 }
     30forall(otype T) void ^?{}(stack(T)* s) { clear(s); }
    4731
    48 forall(otype T) _Bool empty(const stack(T)* s) {
    49         return s->head == 0;
    50 }
     32forall(otype T) _Bool empty(const stack(T)* s) { return s->head == 0; }
    5133
    5234forall(otype T) void push(stack(T)* s, T value) {
  • doc/generic_types/evaluation/cfa-stack.h

    r308880c ra381b46  
    22
    33forall(otype T) struct stack_node;
    4 
    54forall(otype T) struct stack {
    65        stack_node(T)* head;
     
    87
    98forall(otype T) void ?{}(stack(T)* s);
    10 
    119forall(otype T) void ?{}(stack(T)* s, stack(T) t);
    12 
    1310forall(otype T) stack(T) ?=?(stack(T)* s, stack(T) t);
    14 
    1511forall(otype T) void ^?{}(stack(T)* s);
    1612
    1713forall(otype T) _Bool empty(const stack(T)* s);
    18 
    1914forall(otype T) void push(stack(T)* s, T value);
    20 
    2115forall(otype T) T pop(stack(T)* s);
    22 
    2316forall(otype T) void clear(stack(T)* s);
  • doc/generic_types/evaluation/cpp-bench.cpp

    r308880c ra381b46  
    88int main(int argc, char** argv) {
    99        std::ofstream out{"cpp-out.txt"};
     10        int max = 0;
    1011        stack<int> s, t;
    11         int max = 0;
    12         REPEAT_TIMED( "push_int", s.push( _i ); )
     12       
     13        REPEAT_TIMED( "push_int", s.push( int{_i} ); )
    1314        TIMED( "copy_int", t = s; )
    1415        TIMED( "clear_int", s.clear(); )
     
    1718        REPEAT_N_TIMED( "print_int", N/2, print( out, _i, ":", _i, "\n" ); )
    1819
     20        std::pair<bool, char> max1 = { false, '\0' };
    1921        stack<std::pair<bool, char>> s1, t1;
    20         std::pair<bool, char> max1 = { false, '\0' };
     22       
    2123        REPEAT_TIMED( "push_bool_char", s1.push( std::pair<bool, char>{ _i & 0x1, _i & 0x7F } ); )
    2224        TIMED( "copy_bool_char", t1 = s1; )
  • doc/generic_types/evaluation/cpp-print.hpp

    r308880c ra381b46  
    11#pragma once
    2 
    32#include <iomanip>
    43#include <iostream>
    54#include <utility>
    65
    7 template<typename T>
    8 void print(std::ostream& out, const T& x) { out << x; }
     6template<typename T> void print(std::ostream& out, const T& x) { out << x; }
    97
    10 template<>
    11 void print<bool>(std::ostream& out, const bool& x) { out << (x ? "true" : "false"); }
     8template<> void print<bool>(std::ostream& out, const bool& x) { out << (x ? "true" : "false"); }
    129
    13 template<>
    14 void print<char>(std::ostream& out, const char& x ) {
     10template<> void print<char>(std::ostream& out, const char& x ) {
    1511        if ( 0x20 <= x && x <= 0x7E ) { out << "'" << x << "'"; }
    1612        else { out << "'\\" << std::hex << (unsigned int)x << std::setbase(0) << "'"; }
    1713}
    1814
    19 template<typename R, typename S>
     15template<typename R, typename S> 
    2016std::ostream& operator<< (std::ostream& out, const std::pair<R, S>& x) {
    2117        out << "[";
     
    2622}
    2723
    28 template<typename T, typename... Args>
     24template<typename T, typename... Args> 
    2925void print(std::ostream& out, const T& arg, const Args&... rest) {
    3026        out << arg;
  • doc/generic_types/evaluation/cpp-stack.hpp

    r308880c ra381b46  
    11#pragma once
    2 
    32#include <utility>
    43
     
    1110                node( T&& v, node* n ) : value(std::move(v)), next(n) {}
    1211        };
    13        
    1412        node* head;
    1513
     
    2422                *crnt = nullptr;
    2523        }
    26 
    2724public:
    2825        void clear() {
     
    3734
    3835        stack() : head(nullptr) {}
    39 
    4036        stack(const stack<T>& o) { copy(o); }
    41 
    4237        stack(stack<T>&& o) : head(o.head) { o.head = nullptr; }
    43 
    4438        ~stack() { clear(); }
    4539
     
    5852        }
    5953
    60         bool empty() const {
    61                 return head == nullptr;
    62         }
     54        bool empty() const { return head == nullptr; }
    6355
    64         void push(T&& value) {
    65                 head = new node{ std::move(value), head };  /***/
    66         }
     56        void push(T&& value) { head = new node{ std::move(value), head };  /***/ }
    6757
    6858        T pop() {
  • doc/generic_types/evaluation/cpp-vbench.cpp

    r308880c ra381b46  
    88int main(int argc, char** argv) {
    99        std::ofstream out{"cpp-vout.txt"};
     10        integer max{ 0 };
    1011        stack s, t;
    11         integer max{ 0 };
     12       
    1213        REPEAT_TIMED( "push_int", s.push( make<integer>( _i ) ); )
    1314        TIMED( "copy_int", t = s; )
     
    1819                print( out, integer{_i}, c_string{":"}, integer{_i}, c_string{"\n"} ); )
    1920
     21        ptr<pair> max1 = make<pair>( make<boolean>(false), make<character>('\0') );
    2022        stack s1, t1;
    21         ptr<pair> max1 = make<pair>( make<boolean>(false), make<character>('\0') );
     23       
    2224        REPEAT_TIMED( "push_bool_char",
    2325                s1.push( make<pair>( make<boolean>(_i & 1), make<character>(_i & 0x7F) ) ); )
  • doc/generic_types/evaluation/cpp-vprint.hpp

    r308880c ra381b46  
    11#pragma once
    2 
    32#include <ostream>
    43#include "object.hpp"
     
    65void print(std::ostream& out, const printable& x) { x.print(out); }
    76
    8 template<typename... Args>
    9 void print(std::ostream& out, const printable& x, const Args&... rest) {
     7template<typename... Args> void print(std::ostream& out, const printable& x, const Args&... rest) {
    108        x.print(out);
    119        print(out, rest...);
  • doc/generic_types/evaluation/cpp-vstack.cpp

    r308880c ra381b46  
    11#include "cpp-vstack.hpp"
    2 
    32#include <utility>
    43
    54stack::node::node( const object& v ) : value( v.new_copy() ), next( nullptr ) {}
    6 
    75stack::node::node( ptr<object>&& v, node* n ) : value( std::move(v) ), next( n ) {}
    86
     
    1816}
    1917
    20 void stack::clear() {
    21         node* next = head;
    22         while ( next ) {
    23                 node* crnt = next;
    24                 next = crnt->next;
    25                 delete crnt;
    26         }
    27         head = nullptr;
    28 }
    29 
    3018stack::stack() : head(nullptr) {}
    31 
    3219stack::stack(const stack& o) { copy(o); }
    33 
    3420stack::stack(stack&& o) : head(o.head) { o.head = nullptr; }
    35 
    3621stack::~stack() { clear(); }
    3722
     
    5035}
    5136
     37void stack::clear() {
     38        node* next = head;
     39        while ( next ) {
     40                node* crnt = next;
     41                next = crnt->next;
     42                delete crnt;
     43        }
     44        head = nullptr;
     45}
     46
     47
    5248bool stack::empty() const { return head == nullptr; }
    5349
  • doc/generic_types/evaluation/cpp-vstack.hpp

    r308880c ra381b46  
    11#pragma once
    2 
    32#include "object.hpp"
    43
     
    98
    109                node( const object& v );
    11 
    1210                node( ptr<object>&& v, node* n );
    1311        };
    14 
    1512        node* head;
    1613
    1714        void copy(const stack& o);
    18 
    1915public:
    20         void clear();
    21 
    2216        stack();
    23 
    2417        stack(const stack& o);
    25 
    2618        stack(stack&& o);
    27 
    2819        ~stack();
    29 
    3020        stack& operator= (const stack& o);
    31        
    3221        stack& operator= (stack&& o);
    3322
     23        void clear();
    3424        bool empty() const;
    35 
    3625        void push(ptr<object>&& value);
    37 
    3826        ptr<object> pop();
    3927};
  • doc/generic_types/evaluation/object.hpp

    r308880c ra381b46  
    11#pragma once
    2 
    32#include <cstddef>
    43#include <exception>
     
    1615                why = std::string{"bad cast of "} + f.name() + " to " + t.name();
    1716        }
    18 
    1917        ~bad_cast() override = default;
    2018       
     
    2220};
    2321
    24 template<typename T>
    25 std::type_index class_of() { return { typeid(T) }; }
     22template<typename T> std::type_index class_of() { return { typeid(T) }; }
    2623
    2724template<typename T> using ptr = std::unique_ptr<T>;
    2825
    29 class object {
    30 public:
     26struct object {
    3127        std::type_index get_class() const { return { this ? typeid(*this) : typeid(std::nullptr_t) }; }
    3228
    33         template<typename T>
    34         T& as() {
     29        template<typename T> T& as() {
    3530                T* p = dynamic_cast<T*>(this);
    3631                if ( !p ) throw bad_cast{ get_class(), class_of<T>() };
     
    3833        }
    3934
    40         template<typename T>
    41         const T& as() const {
     35        template<typename T> const T& as() const {
    4236                const T* p = dynamic_cast<const T*>(this);
    4337                if ( !p ) throw bad_cast{ get_class(), class_of<T>() };
     
    4640
    4741        virtual ptr<object> new_inst() const = 0;
    48        
    4942        virtual ptr<object> new_copy() const = 0;
    50        
    5143        virtual object& operator= (const object&) = 0;
    52        
    5344        virtual ~object() = default;
    5445};
    5546
    56 template<typename T, typename... Args>
    57 static inline ptr<T> make(Args&&... args) { return std::make_unique<T>(std::forward<Args>(args)...); }
     47template<typename T, typename... Args> static inline ptr<T> make(Args&&... args) {
     48        return std::make_unique<T>(std::forward<Args>(args)...);
     49}
    5850
    59 template<typename To, typename From>
     51template<typename To, typename From> 
    6052ptr<To> as_ptr( ptr<From>&& p ) { return ptr<To>{ &p.release()->template as<To>() }; }
    6153
    62 class ordered : public virtual object {
    63 public:
     54struct ordered : public virtual object {
    6455        virtual int cmp(const ordered&) const = 0;
    6556
    6657        bool operator< (const ordered& that) const { return cmp(that) < 0; }
    67 
    6858        bool operator<= ( const ordered& that ) const { return cmp(that) <= 0; }
    69 
    7059        bool operator== ( const ordered& that ) const { return cmp(that) == 0; }
    71 
    7260        bool operator!= ( const ordered& that ) const { return cmp(that) != 0; }
    73 
    7461        bool operator> ( const ordered& that ) const { return cmp(that) > 0; }
    75 
    7662        bool operator>= ( const ordered& that ) const { return cmp(that) >= 0; }
    7763};
    7864
    79 class printable : public virtual object {
    80 public:
     65struct printable : public virtual object {
    8166        virtual void print(std::ostream&) const = 0;
    8267};
     
    8469class boolean : public ordered, public printable {
    8570        bool x;
    86 
    8771public:
    8872        boolean() = default;
    89 
    9073        boolean(bool x) : x(x) {}
    91 
    9274        ptr<object> new_inst() const override { return make<boolean>(); }
    93        
    9475        ptr<object> new_copy() const override { return make<boolean>(*this); }
    95 
    9676        boolean& operator= (const boolean& that) {
    9777                x = that.x;
    9878                return *this;   
    9979        }
    100 
    10180        object& operator= (const object& that) override { return *this = that.as<boolean>(); } /***/
    102 
    10381        ~boolean() override = default;
    10482
    10583        int cmp(const boolean& that) const { return x == that.x ? 0 : x == false ? -1 : 1; }
    106 
    10784        int cmp(const ordered& that) const override { return cmp( that.as<boolean>() ); } /***/
    10885
     
    11289class character : public ordered, public printable {
    11390        char x;
    114 
    11591public:
    11692        character() = default;
    117 
    11893        character(char x) : x(x) {}
    119 
    12094        ptr<object> new_inst() const override { return make<character>(); }
    121        
    12295        ptr<object> new_copy() const override { return make<character>(*this); }
    123 
    12496        character& operator= (const character& that) {
    12597                x = that.x;
    12698                return *this;   
    12799        }
    128 
    129100        object& operator= (const object& that) override { return *this = that.as<character>(); } /***/
    130 
    131101        ~character() override = default;
    132102
    133103        int cmp(const character& that) const { return x == that.x ? 0 : x < that.x ? -1 : 1; }
    134 
    135104        int cmp(const ordered& that) const override { return cmp( that.as<character>() ); } /***/
    136105
     
    143112class integer : public ordered, public printable {
    144113        int x;
    145 
    146114public:
    147115        integer() = default;
    148 
    149116        integer(int x) : x(x) {}
    150 
    151117        ptr<object> new_inst() const override { return make<integer>(); }
    152        
    153118        ptr<object> new_copy() const override { return make<integer>(*this); }
    154 
    155119        integer& operator= (const integer& that) {
    156120                x = that.x;
    157121                return *this;   
    158122        }
    159 
    160123        object& operator= (const object& that) override { return *this = that.as<integer>(); } /***/
    161 
    162124        ~integer() override = default;
    163125
    164126        int cmp(const integer& that) const { return x == that.x ? 0 : x < that.x ? -1 : 1; }
    165 
    166127        int cmp(const ordered& that) const override { return cmp( that.as<integer>() ); } /***/
    167128
     
    174135public:
    175136        c_string() : s(empty) {}
    176 
    177137        c_string(const char* s) : s(s) {}
    178 
    179138        ptr<object> new_inst() const override { return make<c_string>(); }
    180 
    181139        ptr<object> new_copy() const override { return make<c_string>(s); }
    182 
    183140        c_string& operator= (const c_string& that) {
    184141                s = that.s;
    185142                return *this;
    186143        }
    187 
    188144        object& operator= (const object& that) override { return *this = that.as<c_string>(); } /***/
    189 
    190145        ~c_string() override = default;
    191146
     
    196151        ptr<object> x;
    197152        ptr<object> y;
    198 
    199153public:
    200154        pair() = default;
    201 
    202155        pair(ptr<object>&& x, ptr<object>&& y) : x(std::move(x)), y(std::move(y)) {}
    203        
    204156        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());
    208         }
    209 
     157        ptr<object> new_copy() const override { return make<pair>(x->new_copy(), y->new_copy()); }
    210158        pair& operator= (const pair& that) {
    211159                x = that.x->new_copy();
     
    213161                return *this;
    214162        }
    215 
    216163        object& operator= (const object& that) override { return *this = that.as<pair>(); } /***/
    217 
    218164        ~pair() override = default;
    219165
     
    223169                return y->as<ordered>().cmp( that.y->as<ordered>() ); /***/
    224170        }
    225 
    226171        int cmp(const ordered& that) const override { return cmp( that.as<pair>() ); }
    227172
  • doc/generic_types/evaluation/timing.gp

    r308880c ra381b46  
    11# set terminal pdfcairo linewidth 3 size 6,3
    2 #set terminal postscript eps linewidth 3 size 6,3
     2# set output "timing.pdf"
    33set terminal pslatex size 6.25,2.25 color solid
    44set output "timing.tex"
     
    2020set linetype 4 lc rgb 'green'
    2121
    22 set ylabel "milli-seconds"
     22set ylabel "milliseconds"
    2323set yrange [0:*] ;
    2424
    25 set datafile separator ","
    26 plot for [COL=2:5] 'evaluation/timing.csv' using COL:xticlabels(1) title columnheader
     25# set datafile separator ","
     26plot for [COL=2:5] 'evaluation/timing.dat' using COL:xticlabels(1) title columnheader
  • doc/generic_types/generic_types.tex

    r308880c ra381b46  
    10161016The benchmarks were run on an Ubuntu 16.04 workstation with 16 GB of RAM and a 6-core AMD FX-6300 CPU with 3.5 GHz maximum clock frequency.
    10171017The C and \CCV variants are generally the slowest and most memory-hungry, due to their less-efficient memory layout and the pointer-indirection necessary to implement generic types in these languages; this problem is exacerbated by the second level of generic types in the pair-based benchmarks.
    1018 By contrast, the \CFA and \CC variants run in roughly equivalent time for both the integer and pair of boolean and char tests, which makes sense given that an integer is actually larger than the pair in both languages.
     1018By contrast, the \CFA and \CC variants run in roughly equivalent time for both the integer and pair of boolean and char tests, which makes sense given that an integer is actually larger than the pair in both languages.
     1019\CCV is slower than C largely due to the cost of runtime type-checking of down-casts (implemented here using the \CC @dynamic_cast@ mechanism); our C benchmark uses unchecked casts due to the lack of a language mechanism to perform such checks, while \CFA and \CC can enforce type-safety statically at compilation.
    10191020
    10201021\CC performs best because it uses header-only inlined libraries (\ie no separate compilation).
     
    11231124\bibliography{cfa}
    11241125
    1125 
    11261126\appendix
    11271127
    1128 
    1129 \section{BenchMarks}
     1128\section{Benchmark Source Code}
    11301129\label{sec:BenchMarks}
    11311130
    1132 TODO
     1131Throughout, @/***/@ designates a counted redundant type annotation.
     1132
     1133\subsubsection{bench.h}
     1134(\texttt{bench.hpp} is similar.)
     1135
     1136\lstinputlisting{evaluation/bench.h}
     1137
     1138\subsection{C}
     1139
     1140\subsubsection{c-stack.h} ~
     1141
     1142\lstinputlisting{evaluation/c-stack.h}
     1143
     1144\subsubsection{c-stack.c} ~
     1145
     1146\lstinputlisting{evaluation/c-stack.c}
     1147
     1148\subsubsection{c-pair.h} ~
     1149
     1150\lstinputlisting{evaluation/c-pair.h}
     1151
     1152\subsubsection{c-pair.c} ~
     1153
     1154\lstinputlisting{evaluation/c-pair.c}
     1155
     1156\subsubsection{c-print.h} ~
     1157
     1158\lstinputlisting{evaluation/c-print.h}
     1159
     1160\subsubsection{c-print.c} ~
     1161
     1162\lstinputlisting{evaluation/c-print.c}
     1163
     1164\subsubsection{c-bench.c} ~
     1165
     1166\lstinputlisting{evaluation/c-bench.c}
     1167
     1168\subsection{\CFA}
     1169
     1170\subsubsection{cfa-stack.h} ~
     1171
     1172\lstinputlisting{evaluation/cfa-stack.h}
     1173
     1174\subsubsection{cfa-stack.c} ~
     1175
     1176\lstinputlisting{evaluation/cfa-stack.c}
     1177
     1178\subsubsection{cfa-print.h} ~
     1179
     1180\lstinputlisting{evaluation/cfa-print.h}
     1181
     1182\subsubsection{cfa-print.c} ~
     1183
     1184\lstinputlisting{evaluation/cfa-print.c}
     1185
     1186\subsubsection{cfa-bench.c} ~
     1187
     1188\lstinputlisting{evaluation/cfa-bench.c}
     1189
     1190\subsection{\CC}
     1191
     1192\subsubsection{cpp-stack.hpp} ~
     1193
     1194\lstinputlisting[language=c++]{evaluation/cpp-stack.hpp}
     1195
     1196\subsubsection{cpp-print.hpp} ~
     1197
     1198\lstinputlisting[language=c++]{evaluation/cpp-print.hpp}
     1199
     1200\subsubsection{cpp-bench.cpp} ~
     1201
     1202\lstinputlisting[language=c++]{evaluation/cpp-bench.cpp}
     1203
     1204\subsection{\CCV}
     1205
     1206\subsubsection{object.hpp} ~
     1207
     1208\lstinputlisting[language=c++]{evaluation/object.hpp}
     1209
     1210\subsubsection{cpp-vstack.hpp} ~
     1211
     1212\lstinputlisting[language=c++]{evaluation/cpp-vstack.hpp}
     1213
     1214\subsubsection{cpp-vstack.cpp} ~
     1215
     1216\lstinputlisting[language=c++]{evaluation/cpp-vstack.cpp}
     1217
     1218\subsubsection{cpp-vprint.hpp} ~
     1219
     1220\lstinputlisting[language=c++]{evaluation/cpp-vprint.hpp}
     1221
     1222\subsubsection{cpp-vbench.cpp} ~
     1223
     1224\lstinputlisting[language=c++]{evaluation/cpp-vbench.cpp}
    11331225
    11341226\end{document}
Note: See TracChangeset for help on using the changeset viewer.