Ticket #326: avoid_string_appends_1.patch

File avoid_string_appends_1.patch, 3.8 KB (added by Richard Boulton, 15 years ago)

patch to speed search by avoiding string appends with output of pack_uint_preserving_sort

  • chert_postlist.h

     
    8686        /// Compose a key from a termname and docid.
    8787        static string make_key(const string & term, Xapian::docid did) {
    8888            string key = make_key(term);
    89             key += pack_uint_preserving_sort(did);
     89            append_packed_uint_preserving_sort(key, did);
    9090            return key;
    9191        }
    9292
  • chert_positionlist.cc

     
    7070              did << ", " << term);
    7171
    7272    string data;
    73     if (!get_exact_entry(pack_uint_preserving_sort(did) + term, data)) {
     73    string key;
     74    // 5 bytes is usually enough for a packed uint
     75    key.reserve(5 + term.size());
     76    append_packed_uint_preserving_sort(key, did);
     77    key.append(term);
     78    if (!get_exact_entry(key, data)) {
    7479        // There's no positional information for this term.
    7580        return 0;
    7681    }
     
    106111    positions.clear();
    107112
    108113    string data;
    109     if (!table->get_exact_entry(pack_uint_preserving_sort(did) + tname, data)) {
     114    string key;
     115    // 5 bytes is usually enough for a packed uint
     116    key.reserve(5 + tname.size());
     117    append_packed_uint_preserving_sort(key, did);
     118    key.append(tname);
     119    if (!table->get_exact_entry(key, data)) {
    110120        // There's no positional information for this term.
    111121        current_pos = positions.begin();
    112122        return false;
  • chert_utils.h

     
    224224 *  of 256 bytes on the length of the integer.  However, this is unlikely to
    225225 *  ever be a problem.
    226226 *
     227 *  @param result A string to append the representation of the integer to.
    227228 *  @param value  The integer to represent.
    228  *
    229  *  @result       A string containing the representation of the integer.
    230229 */
    231230template<class T>
    232 string
    233 pack_uint_preserving_sort(T value)
     231void
     232append_packed_uint_preserving_sort(string & result, T value)
    234233{
    235234    // Check unsigned
    236235    STATIC_ASSERT_UNSIGNED_TYPE(T);
    237236
    238     string result;
     237    string::size_type start = result.size();
    239238    while (value != 0) {
    240239        om_byte part = static_cast<om_byte>(value & 0xff);
    241240        value = value >> 8;
    242         result.insert(string::size_type(0), 1u, char(part));
     241        result.insert(start, 1u, char(part));
    243242    }
    244     result.insert(string::size_type(0), 1u, char(result.size()));
    245     return result;
     243    result.insert(start, 1u, char(result.size() - start));
    246244}
    247245
    248246/** Unpack a unsigned integer, store in sort preserving order.
     
    401399inline string
    402400chert_docid_to_key(Xapian::docid did)
    403401{
    404     return pack_uint_preserving_sort(did);
     402    string result;
     403    append_packed_uint_preserving_sort(result, did);
     404    return result;
    405405}
    406406
    407407#endif /* OM_HGUARD_CHERT_UTILS_H */
  • chert_positionlist.h

     
    3434
    3535class ChertPositionListTable : public ChertTable {
    3636    static string make_key(Xapian::docid did, const string & tname) {
    37         return pack_uint_preserving_sort(did) + tname;
     37        string result;
     38        // 5 bytes is usually enough for a packed uint
     39        result.reserve(5 + tname.size());
     40        append_packed_uint_preserving_sort(result, did);
     41        result.append(tname);
     42        return result + tname;
    3843    }
    3944
    4045  public:
  • chert_values.h

     
    3737{
    3838    std::string key("\0\xd8", 2);
    3939    key += pack_uint(slot);
    40     key += pack_uint_preserving_sort(did);
     40    append_packed_uint_preserving_sort(key, did);
    4141    return key;
    4242}
    4343