summaryrefslogtreecommitdiff
path: root/Build/source/libs/graphite2/graphite2-src/src/inc/SegCache.h
blob: b360f7c9337c168fef0f06745f6bcb9e34a49438 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*  GRAPHITE2 LICENSING

    Copyright 2010, SIL International
    All rights reserved.

    This library is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published
    by the Free Software Foundation; either version 2.1 of License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should also have received a copy of the GNU Lesser General Public
    License along with this library in the file named "LICENSE".
    If not, write to the Free Software Foundation, 51 Franklin Street,
    Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
    internet at http://www.fsf.org/licenses/lgpl.html.

Alternatively, the contents of this file may be used under the terms of the
Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
License, as published by the Free Software Foundation, either version 2
of the License or (at your option) any later version.
*/
#pragma once

#ifndef GRAPHITE2_NSEGCACHE

#include <graphite2/Segment.h>
#include "inc/Main.h"
#include "inc/Slot.h"
#include "inc/FeatureVal.h"
#include "inc/SegCacheEntry.h"
#include "inc/Segment.h"

namespace graphite2 {

class SegCache;
class SegCacheEntry;
class SegCacheStore;

/**
 * SegPrefixEntry stores lists of word/syllable segments
 * with one list for each word length. The prefix size should be chosen so that
 * these list sizes stay small since they will be searched iteratively.
 */
class SegCachePrefixEntry
{
    SegCachePrefixEntry(const SegCachePrefixEntry &);
    SegCachePrefixEntry & operator = (const SegCachePrefixEntry &);

public:
    SegCachePrefixEntry() : m_lastPurge(0)
    {
        memset(m_entryCounts, 0, sizeof m_entryCounts);
        memset(m_entryBSIndex, 0, sizeof m_entryBSIndex);
        memset(m_entries, 0, sizeof m_entries);
    }

    ~SegCachePrefixEntry()
    {
        for (size_t j = 0; j < eMaxSpliceSize; j++)
        {
            if (m_entryCounts[j])
            {
                assert(m_entries[j]);
                for (size_t k = 0; k < m_entryCounts[j]; k++)
                    m_entries[j][k].clear();

                free(m_entries[j]);
            }
        }
    }
    const SegCacheEntry * find(const uint16 * cmapGlyphs, size_t length) const
    {
        if (length <= ePrefixLength)
        {
            assert(m_entryCounts[length-1] <= 1);
            if (m_entries[length-1])
                return m_entries[length-1];
            return NULL;
        }
        SegCacheEntry * entry = NULL;
        findPosition(cmapGlyphs, length, &entry);
        return entry;
    }
    SegCacheEntry * cache(const uint16* cmapGlyphs, size_t length, Segment * seg, size_t charOffset, unsigned long long totalAccessCount)
    {
        size_t listSize = m_entryBSIndex[length-1]? (m_entryBSIndex[length-1] << 1) - 1 : 0;
        SegCacheEntry * newEntries = NULL;
        if (m_entryCounts[length-1] + 1u > listSize)
        {
            if (m_entryCounts[length-1] == 0)
                listSize = 1;
            else
            {
                // the problem comes when you get incremental numeric ids in a large doc
                if (listSize >= eMaxSuffixCount)
                    return NULL;
                listSize = (m_entryBSIndex[length-1] << 2) - 1;
            }
            newEntries = gralloc<SegCacheEntry>(listSize);
            if (!newEntries)
                return NULL;
        }        

        uint16 insertPos = 0;
        if (m_entryCounts[length-1] > 0)
        {
            insertPos = findPosition(cmapGlyphs, length, NULL);
            if (!newEntries)
            {
                // same buffer, shift entries up
                memmove(m_entries[length-1] + insertPos + 1, m_entries[length-1] + insertPos,
                    sizeof(SegCacheEntry) * (m_entryCounts[length-1] - insertPos));
            }
            else
            {
                memcpy(newEntries, m_entries[length-1], sizeof(SegCacheEntry) * (insertPos));
                memcpy(newEntries + insertPos + 1, m_entries[length-1] + insertPos,
                   sizeof(SegCacheEntry) * (m_entryCounts[length-1] - insertPos));
                
                free(m_entries[length-1]);
                m_entries[length-1] = newEntries;
                assert (m_entryBSIndex[length-1]);
                m_entryBSIndex[length-1] <<= 1;
            }
        } 
        else
        {
            m_entryBSIndex[length-1] = 1;
            m_entries[length-1] = newEntries;
        }
        m_entryCounts[length-1] += 1;
        new (m_entries[length-1] + insertPos)
            SegCacheEntry(cmapGlyphs, length, seg, charOffset, totalAccessCount);
        return m_entries[length-1]  + insertPos;
    }
    uint32 purge(unsigned long long minAccessCount, unsigned long long oldAccessTime,
        unsigned long long currentTime);
    CLASS_NEW_DELETE
private:
    uint16 findPosition(const uint16 * cmapGlyphs, uint16 length, SegCacheEntry ** entry) const
    {
        int dir = 0;
        if (m_entryCounts[length-1] == 0)
        {
            if (entry) *entry = NULL;
            return 0;
        }
        else if (m_entryCounts[length-1] == 1)
        {
            // optimize single entry case
            for (int i = ePrefixLength; i < length; i++)
            {
                if (cmapGlyphs[i] > m_entries[length-1][0].m_unicode[i])
                {
                    return 1;
                }
                else if (cmapGlyphs[i] < m_entries[length-1][0].m_unicode[i])
                {
                    return 0;
                }
            }
            if (entry)
                *entry = m_entries[length-1];
            return 0;
        }
        uint16 searchIndex = m_entryBSIndex[length-1] - 1;
        uint16 stepSize = m_entryBSIndex[length-1] >> 1;
        size_t prevIndex = searchIndex;
        do
        {
            dir = 0;
            if (searchIndex >= m_entryCounts[length-1])
            {
                dir = -1;
                searchIndex -= stepSize;
                stepSize >>= 1;
            }
            else
            {
                for (int i = ePrefixLength; i < length; i++)
                {
                    if (cmapGlyphs[i] > m_entries[length-1][searchIndex].m_unicode[i])
                    {
                        dir = 1;
                        searchIndex += stepSize;
                        stepSize >>= 1;
                        break;
                    }
                    else if (cmapGlyphs[i] < m_entries[length-1][searchIndex].m_unicode[i])
                    {
                        dir = -1;
                        searchIndex -= stepSize;
                        stepSize >>= 1;
                        break;
                    }
                }
            }
            if (prevIndex == searchIndex)
                break;
            prevIndex = searchIndex;
        } while (dir != 0);
        if (entry)
        {
            if (dir == 0)
                *entry = m_entries[length-1] + searchIndex;
            else
                *entry = NULL;
        }
        else
        {
            // if entry is null, then this is for inserting a new value, which
            // shouldn't already be in the cache
            assert(dir != 0);
            if (dir > 0)
                ++searchIndex;
        }
        return searchIndex;
    }
    /** m_entries is a null terminated list of entries */
    uint16 m_entryCounts[eMaxSpliceSize];
    uint16 m_entryBSIndex[eMaxSpliceSize];
    SegCacheEntry * m_entries[eMaxSpliceSize];
    unsigned long long m_lastPurge;
};


#define SEG_CACHE_MIN_INDEX (store->maxCmapGid())
#define SEG_CACHE_MAX_INDEX (store->maxCmapGid()+1u)
#define SEG_CACHE_UNSET_INDEX (store->maxCmapGid()+2u)

union SegCachePrefixArray
{
    void ** raw;
    SegCachePrefixArray * array;
    SegCachePrefixEntry ** prefixEntries;
    uintptr * range;
};

class SegCache
{
public:
    SegCache(const SegCacheStore * store, const Features& features);
    ~SegCache();

    const SegCacheEntry * find(const uint16 * cmapGlyphs, size_t length) const;
    SegCacheEntry * cache(SegCacheStore * store, const uint16 * cmapGlyphs, size_t length, Segment * seg, size_t charOffset);
    void purge(SegCacheStore * store);

    long long totalAccessCount() const { return m_totalAccessCount; }
    size_t segmentCount() const { return m_segmentCount; }
    const Features & features() const { return m_features; }
    void clear(SegCacheStore * store);

    CLASS_NEW_DELETE
private:
    void freeLevel(SegCacheStore * store, SegCachePrefixArray prefixes, size_t level);
    void purgeLevel(SegCacheStore * store, SegCachePrefixArray prefixes, size_t level,
                    unsigned long long minAccessCount, unsigned long long oldAccessTime);

    uint16 m_prefixLength;
//    uint16 m_maxCachedSegLength;
    size_t m_segmentCount;
    SegCachePrefixArray m_prefixes;
    Features m_features;
    mutable unsigned long long m_totalAccessCount;
    mutable unsigned long long m_totalMisses;
    float m_purgeFactor;
};

inline const SegCacheEntry * SegCache::find(const uint16 * cmapGlyphs, size_t length) const
{
    uint16 pos = 0;
    if (!length || length > eMaxSpliceSize) return NULL;
    SegCachePrefixArray pEntry = m_prefixes.array[cmapGlyphs[0]];
    while (++pos < m_prefixLength - 1)
    {
        if (!pEntry.raw)
        {
            ++m_totalMisses;
            return NULL;
        }
        pEntry = pEntry.array[(pos < length)? cmapGlyphs[pos] : 0];
    }
    if (!pEntry.raw)
    {
        ++m_totalMisses;
        return NULL;
    }
    SegCachePrefixEntry * prefixEntry = pEntry.prefixEntries[(pos < length)? cmapGlyphs[pos] : 0];
    if (!prefixEntry)
    {
        ++m_totalMisses;
        return NULL;
    }
    const SegCacheEntry * entry = prefixEntry->find(cmapGlyphs, length);
    if (entry)
    {
        ++m_totalAccessCount;
        entry->accessed(m_totalAccessCount);
    }
    else
    {
        ++m_totalMisses;
    }   
    return entry;
}
    
} // namespace graphite2

#endif