From 5db34e55d33baaf5827f674ca81cdac8d7ec99bf Mon Sep 17 00:00:00 2001 From: Tyler Akins Date: Sun, 16 Jun 2024 21:11:02 -0500 Subject: [PATCH] Caching function name lookups Faster than dumping functions each time. Related to #73. --- mo | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/mo b/mo index 9a4707f..e7854b1 100755 --- a/mo +++ b/mo @@ -115,6 +115,8 @@ mo() ( moDoubleHyphens=false MO_OPEN_DELIMITER_DEFAULT="{{" MO_CLOSE_DELIMITER_DEFAULT="}}" + MO_FUNCTION_CACHE_HIT=() + MO_FUNCTION_CACHE_MISS=() if [[ $# -gt 0 ]]; then for arg in "$@"; do @@ -911,10 +913,28 @@ mo::parseValue() { # # Returns 0 if the name is a function, 1 otherwise. mo::isFunction() { + local moFunctionName + + for moFunctionName in "${MO_FUNCTION_CACHE_HIT[@]}"; do + if [[ "$moFunctionName" == "$1" ]]; then + return 0 + fi + done + + for moFunctionName in "${MO_FUNCTION_CACHE_MISS[@]}"; do + if [[ "$moFunctionName" == "$1" ]]; then + return 1 + fi + done + if declare -F "$1" &> /dev/null; then + MO_FUNCTION_CACHE_HIT=( ${MO_FUNCTION_CACHE_HIT[@]+"${MO_FUNCTION_CACHE_HIT[@]}"} "$1" ) + return 0 fi + MO_FUNCTION_CACHE_MISS=( ${MO_FUNCTION_CACHE_MISS[@]+"${MO_FUNCTION_CACHE_MISS[@]}"} "$1" ) + return 1 }