Ignore:
Timestamp:
Apr 7, 2009, 2:15:33 PM (16 years ago)
Author:
[email protected]
Message:

2009-04-07 David Levin <[email protected]>

Reviewed by Sam Weinig and Geoff Garen.

https://bugs.webkit.org/show_bug.cgi?id=25039
UString refactoring to support UChar* sharing.

No change in sunspider perf.

  • runtime/SmallStrings.cpp: (JSC::SmallStringsStorage::SmallStringsStorage):
  • runtime/UString.cpp: (JSC::initializeStaticBaseString): (JSC::initializeUString): (JSC::UString::BaseString::isShared): Encapsulate the meaning behind the refcount == 1 checks because this needs to do slightly more when sharing is added. (JSC::concatenate): (JSC::UString::append): (JSC::UString::operator=):
  • runtime/UString.h: Make m_baseString part of a union to get rid of casts, but make it protected because it is tricky to use it correctly since it is only valid when the Rep is not a BaseString. The void* will be filled in when sharing is added.

Add constructors due to the making members protected and it make ensuring proper
initialization work better (like in SmallStringsStorage).
(JSC::UString::Rep::create):
(JSC::UString::Rep::Rep):
(JSC::UString::Rep::):
(JSC::UString::BaseString::BaseString):
(JSC::UString::Rep::setBaseString):
(JSC::UString::Rep::baseString):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/runtime/UString.h

    r39815 r42282  
    8080            friend class JIT;
    8181
    82             static PassRefPtr<Rep> create(UChar*, int);
     82            static PassRefPtr<Rep> create(UChar* buffer, int length)
     83            {
     84                return adoptRef(new BaseString(buffer, length));
     85            }
     86
    8387            static PassRefPtr<Rep> createCopying(const UChar*, int);
    8488            static PassRefPtr<Rep> create(PassRefPtr<Rep> base, int offset, int length);
     
    125129            mutable unsigned _hash;
    126130            PtrAndFlags<IdentifierTable, UStringFlags> m_identifierTableAndFlags;
    127             void* m_baseString; // If "this" is a BaseString instance, it is 0. BaseString* otherwise.
    128131
    129132            static BaseString& null() { return *nullBaseString; }
    130133            static BaseString& empty() { return *emptyBaseString; }
    131134
     135        protected:
     136            Rep(int length)
     137                : offset(0)
     138                , len(length)
     139                , rc(1)
     140                , _hash(0)
     141                , m_nothing(0)
     142            {
     143            }
     144
     145            Rep(PassRefPtr<BaseString> base, int offsetInBase, int length)
     146                : offset(offsetInBase)
     147                , len(length)
     148                , rc(1)
     149                , _hash(0)
     150                , m_baseString(base.releaseRef())
     151            {
     152                checkConsistency();
     153            }
     154
     155            union {
     156                // If !baseIsSelf()
     157                BaseString* m_baseString;
     158                // If baseIsSelf()
     159                void* m_nothing;
     160            };
     161
    132162        private:
     163            // For SmallStringStorage which allocates an array and does initialization manually.
     164            Rep() { }
     165
     166            friend class SmallStringsStorage;
    133167            friend void initializeUString();
    134168            static BaseString* nullBaseString;
     
    136170        };
    137171
     172
    138173        struct BaseString : public Rep {
    139             BaseString()
    140             {
    141                 m_identifierTableAndFlags.setFlag(BaseStringFlag);
    142             }
     174            bool isShared() { return rc != 1; }
    143175
    144176            // potentially shared data.
     
    150182
    151183            size_t reportedCost;
     184
     185        private:
     186            BaseString(UChar* buffer, int length)
     187                : Rep(length)
     188                , buf(buffer)
     189                , preCapacity(0)
     190                , usedPreCapacity(0)
     191                , capacity(length)
     192                , usedCapacity(length)
     193                , reportedCost(0)
     194            {
     195                m_identifierTableAndFlags.setFlag(BaseStringFlag);
     196                checkConsistency();
     197            }
     198
     199            friend struct Rep;
     200            friend class SmallStringsStorage;
     201            friend void initializeUString();
    152202        };
    153203
     
    321371    bool equal(const UString::Rep*, const UString::Rep*);
    322372
     373    inline PassRefPtr<UString::Rep> UString::Rep::create(PassRefPtr<UString::Rep> rep, int offset, int length)
     374    {
     375        ASSERT(rep);
     376        rep->checkConsistency();
     377
     378        int repOffset = rep->offset;
     379
     380        PassRefPtr<BaseString> base = rep->baseString();
     381
     382        ASSERT(-(offset + repOffset) <= base->usedPreCapacity);
     383        ASSERT(offset + repOffset + length <= base->usedCapacity);
     384
     385        // Steal the single reference this Rep was created with.
     386        return adoptRef(new Rep(base, repOffset + offset, length));
     387    }
     388
    323389    inline UChar* UString::Rep::data() const
    324390    {
     
    339405    {
    340406        ASSERT(base != this);
     407        ASSERT(!baseIsSelf());
    341408        m_baseString = base.releaseRef();
    342409    }
     
    344411    inline UString::BaseString* UString::Rep::baseString()
    345412    {
    346         return reinterpret_cast<BaseString*>(baseIsSelf() ? this : m_baseString);
     413        return baseIsSelf() ? reinterpret_cast<BaseString*>(this) : m_baseString;
    347414    }
    348415
    349416    inline const UString::BaseString* UString::Rep::baseString() const
    350417    {
    351         return const_cast<const BaseString*>(const_cast<Rep*>(this)->baseString());
     418        return const_cast<Rep*>(this)->baseString();
    352419    }
    353420
Note: See TracChangeset for help on using the changeset viewer.