Skip to content

Commit

Permalink
Speed up import of bash history
Browse files Browse the repository at this point in the history
  • Loading branch information
krobelus committed Nov 7, 2019
1 parent 35115d3 commit 692a0a0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
25 changes: 14 additions & 11 deletions src/history.cpp
Expand Up @@ -216,7 +216,7 @@ bool history_item_t::matches_search(const wcstring &term, enum history_search_ty
struct history_impl_t {
// Privately add an item. If pending, the item will not be returned by history searches until a
// call to resolve_pending.
void add(const history_item_t &item, bool pending = false);
void add(const history_item_t &item, bool pending = false, bool save = true);

// Internal function.
void clear_file_state();
Expand Down Expand Up @@ -303,7 +303,7 @@ struct history_impl_t {
// item_at_index until a call to resolve_pending(). Pending items are tracked with an offset
// into the array of new items, so adding a non-pending item has the effect of resolving all
// pending items.
void add(const wcstring &str, history_identifier_t ident = 0, bool pending = false);
void add(const wcstring &str, history_identifier_t ident = 0, bool pending = false, bool save = true);

// Remove a history item.
void remove(const wcstring &str);
Expand Down Expand Up @@ -351,7 +351,7 @@ struct history_impl_t {
size_t size();
};

void history_impl_t::add(const history_item_t &item, bool pending) {
void history_impl_t::add(const history_item_t &item, bool pending, bool save) {
// Try merging with the last item.
if (!new_items.empty() && new_items.back().merge(item)) {
// We merged, so we don't have to add anything. Maybe this item was pending, but it just got
Expand All @@ -361,7 +361,7 @@ void history_impl_t::add(const history_item_t &item, bool pending) {
// We have to add a new item.
new_items.push_back(item);
this->has_pending_item = pending;
save_unless_disabled();
if (save) save_unless_disabled();
}
}

Expand Down Expand Up @@ -399,7 +399,7 @@ void history_impl_t::save_unless_disabled() {
countdown_to_vacuum--;
}

void history_impl_t::add(const wcstring &str, history_identifier_t ident, bool pending) {
void history_impl_t::add(const wcstring &str, history_identifier_t ident, bool pending, bool save) {
time_t when = time(NULL);
// Big hack: do not allow timestamps equal to our boundary date. This is because we include
// items whose timestamps are equal to our boundary when reading old history, so we can catch
Expand All @@ -409,7 +409,7 @@ void history_impl_t::add(const wcstring &str, history_identifier_t ident, bool p
when++;
}

this->add(history_item_t(str, when, ident), pending);
this->add(history_item_t(str, when, ident), pending, save);
}

// Remove matching history entries from our list of new items. This only supports literal,
Expand Down Expand Up @@ -1164,8 +1164,11 @@ void history_impl_t::populate_from_bash(FILE *stream) {

wcstring wide_line = str2wcstring(line);
// Add this line if it doesn't contain anything we know we can't handle.
if (should_import_bash_history_line(wide_line)) this->add(wide_line);
if (should_import_bash_history_line(wide_line)) {
this->add(wide_line, 0, false /* pending */, false /* save */);
}
}
this->save_unless_disabled();
}

void history_impl_t::incorporate_external_changes() {
Expand Down Expand Up @@ -1263,10 +1266,10 @@ bool history_t::is_default() const { return impl()->is_default(); }

bool history_t::is_empty() { return impl()->is_empty(); }

void history_t::add(const history_item_t &item, bool pending) { impl()->add(item, pending); }
void history_t::add(const history_item_t &item, bool pending, bool save) { impl()->add(item, pending, save); }

void history_t::add(const wcstring &str, history_identifier_t ident, bool pending) {
impl()->add(str, ident, pending);
void history_t::add(const wcstring &str, history_identifier_t ident, bool pending, bool save) {
impl()->add(str, ident, pending, save);
}

void history_t::remove(const wcstring &str) { impl()->remove(str); }
Expand Down Expand Up @@ -1336,7 +1339,7 @@ void history_t::add_pending_with_file_detection(const wcstring &str,
// Add the item.
// If we think we're about to exit, save immediately, regardless of any disabling. This may
// cause us to lose file hinting for some commands, but it beats losing history items.
imp->add(str, identifier, true /* pending */);
imp->add(str, identifier, true /* pending */, true /* save */);
if (needs_sync_write) {
imp->save();
}
Expand Down
4 changes: 2 additions & 2 deletions src/history.h
Expand Up @@ -127,7 +127,7 @@ class history_t {

// Privately add an item. If pending, the item will not be returned by history searches until a
// call to resolve_pending.
void add(const history_item_t &item, bool pending = false);
void add(const history_item_t &item, bool pending = false, bool save = true);

public:
explicit history_t(wcstring name);
Expand All @@ -154,7 +154,7 @@ class history_t {
// item_at_index until a call to resolve_pending(). Pending items are tracked with an offset
// into the array of new items, so adding a non-pending item has the effect of resolving all
// pending items.
void add(const wcstring &str, history_identifier_t ident = 0, bool pending = false);
void add(const wcstring &str, history_identifier_t ident = 0, bool pending = false, bool save = true);

// Remove a history item.
void remove(const wcstring &str);
Expand Down

0 comments on commit 692a0a0

Please sign in to comment.