Caching function name lookups

Faster than dumping functions each time.
Related to #73.
This commit is contained in:
Tyler Akins 2024-06-16 21:11:02 -05:00
parent 26ca5059d8
commit 5db34e55d3
No known key found for this signature in database
GPG Key ID: 8F3B8C432F4393BD

22
mo
View File

@ -115,6 +115,8 @@ mo() (
moDoubleHyphens=false moDoubleHyphens=false
MO_OPEN_DELIMITER_DEFAULT="{{" MO_OPEN_DELIMITER_DEFAULT="{{"
MO_CLOSE_DELIMITER_DEFAULT="}}" MO_CLOSE_DELIMITER_DEFAULT="}}"
MO_FUNCTION_CACHE_HIT=()
MO_FUNCTION_CACHE_MISS=()
if [[ $# -gt 0 ]]; then if [[ $# -gt 0 ]]; then
for arg in "$@"; do for arg in "$@"; do
@ -911,9 +913,27 @@ mo::parseValue() {
# #
# Returns 0 if the name is a function, 1 otherwise. # Returns 0 if the name is a function, 1 otherwise.
mo::isFunction() { mo::isFunction() {
if declare -F "$1" &> /dev/null; then local moFunctionName
for moFunctionName in "${MO_FUNCTION_CACHE_HIT[@]}"; do
if [[ "$moFunctionName" == "$1" ]]; then
return 0 return 0
fi 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 return 1
} }