mirror of
https://github.com/VectorCamp/vectorscan.git
synced 2025-06-28 16:41:01 +03:00
rose: move sparse iter cache to RoseEngineBlob
This enables its use for iterators written by instructions.
This commit is contained in:
parent
13b6023a18
commit
9139123642
@ -186,10 +186,6 @@ struct build_context : boost::noncopyable {
|
||||
*/
|
||||
size_t numStates = 0;
|
||||
|
||||
/** \brief Very simple cache from sparse iter to offset, used when building
|
||||
* up iterators in early misc. */
|
||||
map<vector<mmbit_sparse_iter>, u32> iterCache;
|
||||
|
||||
/** \brief Simple cache of programs written to engine blob, used for
|
||||
* deduplication. */
|
||||
ue2::unordered_map<RoseProgram, u32, RoseProgramHash,
|
||||
@ -2178,24 +2174,6 @@ u32 RoseBuildImpl::calcHistoryRequired() const {
|
||||
return m ? m - 1 : 0;
|
||||
}
|
||||
|
||||
// Adds a sparse iterator to the end of the iterator table, returning its
|
||||
// offset.
|
||||
static
|
||||
u32 addIteratorToTable(build_context &bc,
|
||||
const vector<mmbit_sparse_iter> &iter) {
|
||||
if (contains(bc.iterCache, iter)) {
|
||||
DEBUG_PRINTF("cache hit\n");
|
||||
u32 offset = bc.iterCache.at(iter);
|
||||
return offset;
|
||||
}
|
||||
|
||||
u32 offset = bc.engine_blob.add(iter.begin(), iter.end());
|
||||
|
||||
bc.iterCache.insert(make_pair(iter, offset));
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
static
|
||||
u32 buildLastByteIter(const RoseGraph &g, build_context &bc) {
|
||||
vector<u32> lb_roles;
|
||||
@ -2217,7 +2195,7 @@ u32 buildLastByteIter(const RoseGraph &g, build_context &bc) {
|
||||
|
||||
vector<mmbit_sparse_iter> iter;
|
||||
mmbBuildSparseIterator(iter, lb_roles, bc.numStates);
|
||||
return addIteratorToTable(bc, iter);
|
||||
return bc.engine_blob.add_iterator(iter);
|
||||
}
|
||||
|
||||
static
|
||||
@ -2329,7 +2307,7 @@ u32 buildEodNfaIterator(build_context &bc, const u32 activeQueueCount) {
|
||||
|
||||
vector<mmbit_sparse_iter> iter;
|
||||
mmbBuildSparseIterator(iter, keys, activeQueueCount);
|
||||
return addIteratorToTable(bc, iter);
|
||||
return bc.engine_blob.add_iterator(iter);
|
||||
}
|
||||
|
||||
static
|
||||
@ -4669,7 +4647,7 @@ u32 buildEagerQueueIter(const set<u32> &eager, u32 leftfixBeginQueue,
|
||||
|
||||
vector<mmbit_sparse_iter> iter;
|
||||
mmbBuildSparseIterator(iter, vec, queue_count - leftfixBeginQueue);
|
||||
return addIteratorToTable(bc, iter);
|
||||
return bc.engine_blob.add_iterator(iter);
|
||||
}
|
||||
|
||||
static
|
||||
|
@ -34,6 +34,8 @@
|
||||
#include "ue2common.h"
|
||||
#include "util/alloc.h"
|
||||
#include "util/container.h"
|
||||
#include "util/multibit_build.h"
|
||||
#include "util/ue2_containers.h"
|
||||
#include "util/verify_types.h"
|
||||
|
||||
#include <vector>
|
||||
@ -104,6 +106,19 @@ public:
|
||||
return offset;
|
||||
}
|
||||
|
||||
u32 add_iterator(const std::vector<mmbit_sparse_iter> &iter) {
|
||||
auto cache_it = cached_iters.find(iter);
|
||||
if (cache_it != cached_iters.end()) {
|
||||
u32 offset = cache_it->second;
|
||||
DEBUG_PRINTF("cache hit for iter at %u\n", offset);
|
||||
return offset;
|
||||
}
|
||||
|
||||
u32 offset = add(iter.begin(), iter.end());
|
||||
cached_iters.emplace(iter, offset);
|
||||
return offset;
|
||||
}
|
||||
|
||||
void write_bytes(RoseEngine *engine) {
|
||||
copy_bytes((char *)engine + base_offset, blob);
|
||||
}
|
||||
@ -120,6 +135,9 @@ private:
|
||||
blob.resize(s + align - s % align);
|
||||
}
|
||||
|
||||
/** \brief Cache of previously-written sparse iterators. */
|
||||
unordered_map<std::vector<mmbit_sparse_iter>, u32> cached_iters;
|
||||
|
||||
/**
|
||||
* \brief Contents of the Rose bytecode immediately following the
|
||||
* RoseEngine.
|
||||
|
@ -387,7 +387,7 @@ void RoseInstrSparseIterBegin::write(void *dest, RoseEngineBlob &blob,
|
||||
vector<mmbit_sparse_iter> iter;
|
||||
mmbBuildSparseIterator(iter, keys, num_keys);
|
||||
assert(!iter.empty());
|
||||
inst->iter_offset = blob.add(iter.begin(), iter.end());
|
||||
inst->iter_offset = blob.add_iterator(iter);
|
||||
inst->jump_table = blob.add(jump_offsets.begin(), jump_offsets.end());
|
||||
|
||||
// Store offsets for corresponding SPARSE_ITER_NEXT operations.
|
||||
@ -422,7 +422,7 @@ void RoseInstrSparseIterAny::write(void *dest, RoseEngineBlob &blob,
|
||||
vector<mmbit_sparse_iter> iter;
|
||||
mmbBuildSparseIterator(iter, keys, num_keys);
|
||||
assert(!iter.empty());
|
||||
inst->iter_offset = blob.add(iter.begin(), iter.end());
|
||||
inst->iter_offset = blob.add_iterator(iter);
|
||||
}
|
||||
|
||||
void RoseInstrEnginesEod::write(void *dest, RoseEngineBlob &blob,
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2015, Intel Corporation
|
||||
* Copyright (c) 2015-2016, Intel Corporation
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
@ -34,16 +34,18 @@
|
||||
#define MULTIBIT_BUILD_H
|
||||
|
||||
#include "multibit_internal.h"
|
||||
#include "hash.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
/** \brief Comparator for \ref mmbit_sparse_iter structures. */
|
||||
static inline
|
||||
bool operator<(const mmbit_sparse_iter &a, const mmbit_sparse_iter &b) {
|
||||
if (a.mask != b.mask) {
|
||||
return a.mask < b.mask;
|
||||
}
|
||||
return a.val < b.val;
|
||||
inline
|
||||
bool operator==(const mmbit_sparse_iter &a, const mmbit_sparse_iter &b) {
|
||||
return a.mask == b.mask && a.val == b.val;
|
||||
}
|
||||
|
||||
inline
|
||||
size_t hash_value(const mmbit_sparse_iter &iter) {
|
||||
return ue2::hash_all(iter.mask, iter.val);
|
||||
}
|
||||
|
||||
namespace ue2 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user