File indexing completed on 2023-05-30 11:30:51
0001 /** 0002 * Copyright (C) 2003 Maksim Orlovich <maksim.orlovich@kdemail.net> 0003 * 0004 * This program is free software; you can redistribute it and/or modify it under 0005 * the terms of the GNU General Public License as published by the Free Software 0006 * Foundation; either version 2 of the License, or (at your option) any later 0007 * version. 0008 * 0009 * This program is distributed in the hope that it will be useful, but WITHOUT ANY 0010 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 0011 * PARTICULAR PURPOSE. See the GNU General Public License for more details. 0012 * 0013 * You should have received a copy of the GNU General Public License along with 0014 * this program. If not, see <http://www.gnu.org/licenses/>. 0015 */ 0016 #include "stringshare.h" 0017 0018 #include <QHash> 0019 0020 const int SIZE = 8191; 0021 0022 static unsigned num_attempts = 0; 0023 static unsigned num_hits = 0; 0024 0025 /** 0026 * We store the strings in a simple direct-mapped (i.e. no collision handling, 0027 * just replace) hash, which contain strings or null objects. This costs only 0028 * 4 bytes per slot on 32-bit archs, so with the default constant size we only 0029 * really use 40K or so. 0030 * 0031 * The end result is that many strings end up pointing to the same underlying data 0032 * object, instead of each one having its own little copy. 0033 * 0034 * More importantly, the way the tryShare function is coded ensures that 0035 * most-recently inserted text stays in the cache, which gives a better chance 0036 * of continuing to share data. (Even if something old ("foo") that was shared 0037 * gets kicked out, all the other "foo"s will still be sharing each other's 0038 * data. 0039 */ 0040 0041 struct StringShare::Data 0042 { 0043 QString qstringHash [SIZE]; 0044 }; 0045 0046 StringShare::Data* StringShare::data() 0047 { 0048 static Data *data = new Data; 0049 return data; 0050 } 0051 0052 QString StringShare::tryShare(const QString& in) 0053 { 0054 uint index = qHash(in) % SIZE; 0055 0056 num_attempts++; 0057 0058 Data* dat = data(); 0059 if (dat->qstringHash[index] == in) { 0060 // Match 0061 num_hits++; 0062 return dat->qstringHash[index]; 0063 } else { 0064 // Else replace whatever was there before 0065 dat->qstringHash[index] = in; 0066 return in; 0067 } 0068 } 0069 0070 unsigned StringShare::numHits() 0071 { 0072 return num_hits; 0073 } 0074 0075 unsigned StringShare::numAttempts() 0076 { 0077 return num_attempts; 0078 } 0079 0080 // vim: set et sw=4 tw=0 sta: