summaryrefslogtreecommitdiff
path: root/js/src/jsapi-tests/testSharedImmutableStringsCache.cpp
blob: 33091e02f65556279ea1f887ab3c826504e542c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/IntegerRange.h"

#include "js/Vector.h"
#include "jsapi-tests/tests.h"
#include "threading/Thread.h"
#include "vm/SharedImmutableStringsCache.h"

const int NUM_THREADS = 256;
const int NUM_ITERATIONS = 256;

const int NUM_STRINGS = 4;
const char16_t *const STRINGS[NUM_STRINGS] = {
    u"uno",
    u"dos",
    u"tres",
    u"quattro"
};

struct CacheAndIndex
{
    js::SharedImmutableStringsCache* cache;
    int index;

    CacheAndIndex(js::SharedImmutableStringsCache* cache, int index)
      : cache(cache)
      , index(index)
    { }
};

static void
getString(CacheAndIndex* cacheAndIndex)
{
    for (int i = 0; i < NUM_ITERATIONS; i++) {
        auto str = STRINGS[cacheAndIndex->index % NUM_STRINGS];

        auto dupe = js::DuplicateString(str);
        MOZ_RELEASE_ASSERT(dupe);

        auto deduped = cacheAndIndex->cache->getOrCreate(mozilla::Move(dupe), js_strlen(str));
        MOZ_RELEASE_ASSERT(deduped.isSome());
        MOZ_RELEASE_ASSERT(js_strcmp(str, deduped->chars()) == 0);

        {
            auto cloned = deduped->clone();
            // We should be de-duplicating and giving back the same string.
            MOZ_RELEASE_ASSERT(deduped->chars() == cloned.chars());
        }
    }

    js_delete(cacheAndIndex);
}

BEGIN_TEST(testSharedImmutableStringsCache)
{
    auto maybeCache = js::SharedImmutableStringsCache::Create();
    CHECK(maybeCache.isSome());
    auto& cache = *maybeCache;

    js::Vector<js::Thread> threads(cx);
    CHECK(threads.reserve(NUM_THREADS));

    for (auto i : mozilla::MakeRange(NUM_THREADS)) {
        auto cacheAndIndex = js_new<CacheAndIndex>(&cache, i);
        CHECK(cacheAndIndex);
        threads.infallibleEmplaceBack();
        CHECK(threads.back().init(getString, cacheAndIndex));
    }

    for (auto& thread : threads)
        thread.join();

    return true;
}
END_TEST(testSharedImmutableStringsCache)