diff --git a/src/lib_shell/mempool_worker.ml b/src/lib_shell/mempool_worker.ml index e313d233d..ca7da1a97 100644 --- a/src/lib_shell/mempool_worker.ml +++ b/src/lib_shell/mempool_worker.ml @@ -62,7 +62,13 @@ module type T = sig end -module Make(Proto: Registered_protocol.T) : T with module Proto = Proto = struct +module type STATIC = sig + val max_size_parsed_cache: int +end + +module Make(Static: STATIC)(Proto: Registered_protocol.T) + : T with module Proto = Proto += struct module Proto = Proto @@ -221,32 +227,34 @@ module Make(Proto: Registered_protocol.T) : T with module Proto = Proto = struct (* parsed operations' cache. used for memoization *) module ParsedCache = struct - type t = operation tzresult Operation_hash.Table.t + type t = { + table: operation tzresult Operation_hash.Table.t ; + ring: Operation_hash.t Ring.t ; + } - let encoding = - (Operation_hash.Table.encoding - (Error_monad.result_encoding operation_encoding)) - - let create () : t = - Operation_hash.Table.create 1000 + let create () : t = { + table = Operation_hash.Table.create Static.max_size_parsed_cache ; + ring = Ring.create Static.max_size_parsed_cache ; + } let add t raw_op parsed_op = let hash = Operation.hash raw_op in - Operation_hash.Table.replace t hash parsed_op - - let mem t raw_op = - let hash = Operation.hash raw_op in - Operation_hash.Table.mem t hash + Option.iter + ~f:(Operation_hash.Table.remove t.table) + (Ring.add_and_return_erased t.ring hash); + Operation_hash.Table.replace t.table hash parsed_op let find_opt t raw_op = let hash = Operation.hash raw_op in - Operation_hash.Table.find_opt t hash + Operation_hash.Table.find_opt t.table hash let find_hash_opt t hash = - Operation_hash.Table.find_opt t hash + Operation_hash.Table.find_opt t.table hash let rem t hash = - Operation_hash.Table.remove t hash + (* NOTE: hashes are not removed from the ring. As a result, the cache size + * bound can be lowered. This is a non-issue because it's only a cache. *) + Operation_hash.Table.remove t.table hash end diff --git a/src/lib_shell/mempool_worker.mli b/src/lib_shell/mempool_worker.mli index be9fdd4a4..f5c33e8c9 100644 --- a/src/lib_shell/mempool_worker.mli +++ b/src/lib_shell/mempool_worker.mli @@ -63,5 +63,9 @@ module type T = sig end -module Make (Proto : Registered_protocol.T) : T +module type STATIC = sig + val max_size_parsed_cache: int +end + +module Make (Static : STATIC) (Proto : Registered_protocol.T) : T with module Proto = Proto