~neon/juk/master

716 by Maks Orlovich
Reduce memory usage by roughly a 1/3rd, by:
1
/***************************************************************************
2
    begin                : Sat Oct 25 2003
3
    copyright            : (C) 2003 by Maksim Orlovich
1561 by Scott Wheeler
Since the annotate is going to be screwed anyway by the time this is ported,
4
    email                : maksim.orlovich@kdemail.net
716 by Maks Orlovich
Reduce memory usage by roughly a 1/3rd, by:
5
***************************************************************************/
6
7
/***************************************************************************
8
 *                                                                         *
9
 *   This program is free software; you can redistribute it and/or modify  *
10
 *   it under the terms of the GNU General Public License as published by  *
11
 *   the Free Software Foundation; either version 2 of the License, or     *
12
 *   (at your option) any later version.                                   *
13
 *                                                                         *
14
 ***************************************************************************/
2019 by Michael Pyne
Maksim pointed out that my change to StringShare took away an important property (most recently inserted
15
#include <QHash>
716 by Maks Orlovich
Reduce memory usage by roughly a 1/3rd, by:
16
#include "stringshare.h"
2018 by Michael Pyne
local8Bit encodings scare me, so save the QString in the shared PlaylistItem/CollectionListItem metadata
17
716 by Maks Orlovich
Reduce memory usage by roughly a 1/3rd, by:
18
const int SIZE = 5003;
19
20
StringShare::Data* StringShare::s_data = 0;
21
22
/**
2018 by Michael Pyne
local8Bit encodings scare me, so save the QString in the shared PlaylistItem/CollectionListItem metadata
23
 * We store the strings in a simple direct-mapped (i.e. no collision handling,
2019 by Michael Pyne
Maksim pointed out that my change to StringShare took away an important property (most recently inserted
24
 * just replace) hash, which contain strings or null objects. This costs only
25
 * 4 bytes per slot on 32-bit archs, so with the default constant size we only
26
 * really use 40K or so.
944 by Scott Wheeler
CVS_SILENT this comment was offending me. :-)
27
 *
28
 * The end result is that many strings end up pointing to the same underlying data
1561 by Scott Wheeler
Since the annotate is going to be screwed anyway by the time this is ported,
29
 * object, instead of each one having its own little copy.
2019 by Michael Pyne
Maksim pointed out that my change to StringShare took away an important property (most recently inserted
30
 *
31
 * More importantly, the way the tryShare function is coded ensures that
32
 * most-recently inserted text stays in the cache, which gives a better chance
33
 * of continuing to share data. (Even if something old ("foo") that was shared
34
 * gets kicked out, all the other "foo"s will still be sharing each other's
35
 * data.
944 by Scott Wheeler
CVS_SILENT this comment was offending me. :-)
36
 */
716 by Maks Orlovich
Reduce memory usage by roughly a 1/3rd, by:
37
38
struct StringShare::Data
39
{
2019 by Michael Pyne
Maksim pointed out that my change to StringShare took away an important property (most recently inserted
40
    QString  qstringHash [SIZE];
716 by Maks Orlovich
Reduce memory usage by roughly a 1/3rd, by:
41
};
42
43
StringShare::Data* StringShare::data()
44
{
45
    if (!s_data)
46
        s_data = new Data;
1561 by Scott Wheeler
Since the annotate is going to be screwed anyway by the time this is ported,
47
    return s_data;
716 by Maks Orlovich
Reduce memory usage by roughly a 1/3rd, by:
48
}
49
50
QString StringShare::tryShare(const QString& in)
51
{
2019 by Michael Pyne
Maksim pointed out that my change to StringShare took away an important property (most recently inserted
52
    uint index = qHash(in) % SIZE;
53
2018 by Michael Pyne
local8Bit encodings scare me, so save the QString in the shared PlaylistItem/CollectionListItem metadata
54
    Data* dat = data();
2019 by Michael Pyne
Maksim pointed out that my change to StringShare took away an important property (most recently inserted
55
    if (dat->qstringHash[index] == in) //Match
56
        return dat->qstringHash[index];
57
    else
58
    {
59
        //Else replace whatever was there before
60
        dat->qstringHash[index] = in;
61
        return in;
62
    }
716 by Maks Orlovich
Reduce memory usage by roughly a 1/3rd, by:
63
}
1562 by Michael Pyne
Since we're standardizing the source style, figured I'd throw in the Big Huge Vim Modeline
64
65
// vim: set et sw=4 tw=0 sta: