Changeset fdae913 for src/Common


Ignore:
Timestamp:
Mar 15, 2019, 3:43:39 PM (5 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, persistent-indexer, pthread-emulation, qualifiedEnum
Children:
42f1279c
Parents:
b419abb
Message:

modify persistent map to not re-initialize history nodes facing deletion

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/PersistentMap.h

    rb419abb rfdae913  
    114114        }
    115115
     116        /// check if this is the only reference to itself
     117        /// NOTE: optimizations employing this function are not thread-safe
     118        bool is_shared(long local_ptrs = 0) const {
     119                // 2 accounts for both the pointer which owns "this" and the one created for its use_count
     120                return (this->shared_from_this().use_count() - local_ptrs) > 2;
     121        }
     122
    116123public:
    117124        using size_type = std::size_t;
     
    144151                // remove map from base
    145152                Base base_map = base->take_as<Base>();
    146                 base->reset_as_base();
    147                 // xxx -- investigate checking ref-count and omitting re-initialization if 1
    148153
    149154                // switch base to inverse of self and mutate base map
     
    153158                                auto it = base_map.find( self.key );
    154159
    155                                 base->init<Ins>(
    156                                         mut_this->shared_from_this(), std::move(self.key), std::move(it->second) );
    157                                 base->mode = INS;
     160                                if ( base->is_shared( 1 ) ) {
     161                                        base->reset_as_base();
     162                                        base->init<Ins>(
     163                                                mut_this->shared_from_this(), std::move(self.key), std::move(it->second) );
     164                                        base->mode = INS;
     165                                }
    158166
    159167                                base_map.erase( it );
     
    163171                                Ins& self = mut_this->as<Ins>();
    164172
    165                                 base->init<Rem>( mut_this->shared_from_this(), self.key );
    166                                 base->mode = REM;
     173                                if ( base->is_shared( 1 ) ) {
     174                                        base->reset_as_base();
     175                                        base->init<Rem>( mut_this->shared_from_this(), self.key );
     176                                        base->mode = REM;
     177                                }
    167178
    168179                                base_map.emplace( std::move(self.key), std::move(self.val) );
     
    173184                                auto it = base_map.find( self.key );
    174185
    175                                 base->init<Ins>(
    176                                         mut_this->shared_from_this(), std::move(self.key), std::move(it->second) );
    177                                 base->mode = UPD;
     186                                if ( base->is_shared( 1 ) ) {
     187                                        base->reset_as_base();
     188                                        base->init<Ins>(
     189                                                mut_this->shared_from_this(), std::move(self.key), std::move(it->second) );
     190                                        base->mode = UPD;
     191                                }
    178192
    179193                                it->second = std::move(self.val);
     
    240254                // transfer map to new node
    241255                Ptr ret = std::make_shared<Self>( take_as<Base>() );
    242                 reset_as_base();
    243256                Base& base_map = ret->as<Base>();
    244257
     
    246259                auto it = base_map.find( k );
    247260                if ( it == base_map.end() ) {
    248                         // set self to REM node and insert into base
    249                         init<Rem>( ret, k );
    250                         mode = REM;
     261                        if ( is_shared() ) {
     262                                // set self to REM node and insert into base
     263                                reset_as_base();
     264                                init<Rem>( ret, k );
     265                                mode = REM;
     266                        }
    251267
    252268                        base_map.emplace_hint( it, std::forward<K>(k), std::forward<V>(v) );
    253269                } else {
    254                         // set self to UPD node and modify base
    255                         init<Ins>( ret, std::forward<K>(k), std::move(it->second) );
    256                         mode = UPD;
     270                        if ( is_shared() ) {
     271                                // set self to UPD node and modify base
     272                                reset_as_base();
     273                                init<Ins>( ret, std::forward<K>(k), std::move(it->second) );
     274                                mode = UPD;
     275                        }
    257276
    258277                        it->second = std::forward<V>(v);
     
    271290                // transfer map to new node
    272291                Ptr ret = std::make_shared<Self>( take_as<Base>() );
    273                 reset_as_base();
    274292                Base& base_map = ret->as<Base>();
    275293
    276                 // set self to INS node and remove from base
    277                 init<Ins>( ret, k, base_map[k] );
    278                 mode = INS;
     294                if ( is_shared() ) {
     295                        // set self to INS node and remove from base
     296                        reset_as_base();
     297                        init<Ins>( ret, k, base_map[k] );
     298                        mode = INS;
     299                }
    279300
    280301                base_map.erase( k );
Note: See TracChangeset for help on using the changeset viewer.