Ignore:
Timestamp:
Aug 20, 2009, 10:41:54 PM (16 years ago)
Author:
[email protected]
Message:

Added a number => string cache.

Patch by Geoffrey Garen <[email protected]> on 2009-08-20
Reviewed by Maciej Stachowiak.

1.07x faster on v8 (1.7x faster on v8-splay).
1.004x faster on SunSpider.

  • runtime/JSCell.h: Moved JSValue::toString to JSString.h.
  • runtime/JSGlobalData.h: Holds the cache.
  • runtime/JSNumberCell.cpp:

(JSC::JSNumberCell::toString):
(JSC::JSNumberCell::toThisString): Removed -0 special case.
UString handles this now, since too many clients were
special-casing it.

  • runtime/JSString.h:

(JSC::JSValue::toString): Use the cache when converting
an int or double to string.

  • runtime/Operations.h:

(JSC::concatenateStrings): Call toString to take advantage
of the cache.

  • runtime/SmallStrings.h:

(JSC::NumericStrings::add):
(JSC::NumericStrings::lookup): The cache.

  • runtime/UString.cpp:

(JSC::UString::from): Added -0 special case mentioned above.
Removed appendNumeric because it's mutually exclusive with the
cache.

File:
1 copied

Legend:

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

    r47617 r47622  
    11/*
    2  * Copyright (C) 2008 Apple Inc. All Rights Reserved.
     2 * Copyright (C) 2009 Apple Inc. All Rights Reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    2424 */
    2525
    26 #ifndef SmallStrings_h
    27 #define SmallStrings_h
     26#ifndef NumericStrings_h
     27#define NumericStrings_h
    2828
    2929#include "UString.h"
    30 #include <wtf/OwnPtr.h>
     30#include <wtf/HashFunctions.h>
    3131
    3232namespace JSC {
    3333
    34     class JSGlobalData;
    35     class JSString;
    36 
    37     class SmallStringsStorage;
    38 
    39     class SmallStrings : public Noncopyable {
     34    class NumericStrings {
    4035    public:
    41         SmallStrings();
    42         ~SmallStrings();
    43 
    44         JSString* emptyString(JSGlobalData* globalData)
     36        UString add(double d)
    4537        {
    46             if (!m_emptyString)
    47                 createEmptyString(globalData);
    48             return m_emptyString;
    49         }
    50         JSString* singleCharacterString(JSGlobalData* globalData, unsigned char character)
    51         {
    52             if (!m_singleCharacterStrings[character])
    53                 createSingleCharacterString(globalData, character);
    54             return m_singleCharacterStrings[character];
     38            CacheEntry<double>& entry = lookup(d);
     39            if (d == entry.key && !entry.value.isNull())
     40                return entry.value;
     41            entry.key = d;
     42            entry.value = UString::from(d);
     43            return entry.value;
    5544        }
    5645
    57         UString::Rep* singleCharacterStringRep(unsigned char character);
    58 
    59         void mark();
    60 
    61         unsigned count() const;
     46        UString add(int i)
     47        {
     48            CacheEntry<int>& entry = lookup(i);
     49            if (i == entry.key && !entry.value.isNull())
     50                return entry.value;
     51            entry.key = i;
     52            entry.value = UString::from(i);
     53            return entry.value;
     54        }
    6255
    6356    private:
    64         void createEmptyString(JSGlobalData*);
    65         void createSingleCharacterString(JSGlobalData*, unsigned char);
     57        static const size_t cacheSize = 64;
    6658
    67         JSString* m_emptyString;
    68         JSString* m_singleCharacterStrings[0x100];
    69         OwnPtr<SmallStringsStorage> m_storage;
     59        template<typename T>
     60        struct CacheEntry {
     61            T key;
     62            UString value;
     63        };
     64
     65        CacheEntry<double>& lookup(double d) { return doubleCache[WTF::FloatHash<double>::hash(d) & (cacheSize - 1)]; }
     66        CacheEntry<int>& lookup(int i) { return intCache[WTF::IntHash<int>::hash(i) & (cacheSize - 1)]; }
     67
     68        CacheEntry<double> doubleCache[cacheSize];
     69        CacheEntry<int> intCache[cacheSize];
    7070    };
    7171
    7272} // namespace JSC
    7373
    74 #endif // SmallStrings_h
     74#endif // NumericStrings_h
Note: See TracChangeset for help on using the changeset viewer.