Skip to content

Commit

Permalink
feat(job stack, performance): max one stack item per session
Browse files Browse the repository at this point in the history
Cuts first load to 1/3
  • Loading branch information
olets committed Apr 5, 2020
1 parent d23f350 commit f50df95
Showing 1 changed file with 50 additions and 113 deletions.
163 changes: 50 additions & 113 deletions zsh-abbr.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ _zsh_abbr() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:erase"

local abbreviation
local job
local job_group
local success

if [[ $# > 1 ]]; then
Expand All @@ -81,13 +79,8 @@ _zsh_abbr() {
fi

abbreviation=$1
job_group='erase'
success=0

echo $RANDOM >/dev/null
job=$(_zsh_abbr_job:name)
! (( ZSH_ABBR_INITIALIZING )) && _zsh_abbr_job:push $job $job_group

if [[ $scope == 'session' ]]; then
if [[ $type == 'global' ]]; then
if (( ${+ZSH_ABBR_SESSION_GLOBALS[$abbreviation]} )); then
Expand Down Expand Up @@ -138,8 +131,6 @@ _zsh_abbr() {
fi
fi

! (( ZSH_ABBR_INITIALIZING )) && _zsh_abbr_job:pop $job $job_group

if ! (( success )); then
_zsh_abbr:util_error " erase: No ${type:-regular} ${scope:-user} abbreviation $abbreviation found"
fi
Expand Down Expand Up @@ -333,8 +324,6 @@ _zsh_abbr() {

local err
local expansion
local job
local job_group
local new
local old

Expand All @@ -347,10 +336,6 @@ _zsh_abbr() {
new_abbreviation=$2
job_group='_zsh_abbr:rename'

echo $RANDOM >/dev/null
job=$(_zsh_abbr_job:name)
! (( ZSH_ABBR_INITIALIZING )) && _zsh_abbr_job:push $job $job_group

if [[ $scope == 'session' ]]; then
if [[ $type == 'global' ]]; then
expansion=${ZSH_ABBR_SESSION_GLOBALS[$current_abbreviation]}
Expand All @@ -371,28 +356,20 @@ _zsh_abbr() {
else
_zsh_abbr:util_error " rename: No ${type:-regular} ${scope:-user} abbreviation $current_abbreviation exists"
fi

! (( ZSH_ABBR_INITIALIZING )) && _zsh_abbr_job:pop $job $job_group
}

function _zsh_abbr:util_add() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:util_add"

local abbreviation
local expansion
local job
local job_group
local success

abbreviation=$1
expansion=$2
job_group='_zsh_abbr:util_add'
success=0

echo $RANDOM >/dev/null
job=$(_zsh_abbr_job:name)
! (( ZSH_ABBR_INITIALIZING )) && _zsh_abbr_job:push $job $job_group

if [[ ${(w)#abbreviation} > 1 ]]; then
_zsh_abbr:util_error " add: ABBREVIATION ('$abbreviation') must be only one word"
return
Expand Down Expand Up @@ -448,8 +425,6 @@ _zsh_abbr() {
fi
fi

! (( ZSH_ABBR_INITIALIZING )) && _zsh_abbr_job:pop $job $job_group

if ! (( success )); then
_zsh_abbr:util_error " add: A ${type:-regular} ${scope:-user} abbreviation $abbreviation already exists"
fi
Expand Down Expand Up @@ -581,23 +556,8 @@ _zsh_abbr() {
function _zsh_abbr:util_sync_user() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:util_sync_user"

local job
local job_group
local user_updated

<<<<<<< HEAD
if [[ -n $(ls ${TMPDIR:-/tmp/}zsh-abbr-jobs/current 2>/dev/null | grep 'init*') ]]; then
return
fi
=======
(( ZSH_ABBR_INITIALIZING )) && return
>>>>>>> refactor,feat(performance): syntax + design changes for init speed...

job_group='_zsh_abbr:util_sync_user'

echo $RANDOM >/dev/null
job=$(_zsh_abbr_job:name)
! (( ZSH_ABBR_INITIALIZING )) && _zsh_abbr_job:push $job $job_group
local user_updated

user_updated=${TMPDIR:-/tmp/}zsh-user-abbreviations_updated
rm $user_updated 2> /dev/null
Expand All @@ -615,8 +575,6 @@ _zsh_abbr() {
done

mv $user_updated $ZSH_ABBR_USER_PATH

! (( ZSH_ABBR_INITIALIZING )) && _zsh_abbr_job:pop $job $job_group
}

function _zsh_abbr:util_usage() {
Expand All @@ -625,6 +583,11 @@ _zsh_abbr() {
man abbr 2>/dev/null || cat ${ZSH_ABBR_SOURCE_PATH}/man/abbr.txt | less -F
}

if ! (( ZSH_ABBR_INITIALIZING )); then
job=$(_zsh_abbr_job_name)
_zsh_abbr_job_push $job
fi

for opt in "$@"; do
if (( should_exit )); then
should_exit=0
Expand Down Expand Up @@ -726,6 +689,10 @@ _zsh_abbr() {
# default if no argument is provided
_zsh_abbr:list_abbreviations $@
fi

if ! (( ZSH_ABBR_INITIALIZING )); then
_zsh_abbr_job_pop $job
fi
} always {
unfunction -m _zsh_abbr:add
unfunction -m _zsh_abbr:clear_session
Expand Down Expand Up @@ -832,14 +799,11 @@ _zsh_abbr_init() {

local line
local job
local job_group
local session_shwordsplit_on

job_group='init'
job=$(_zsh_abbr_job_name)
session_shwordsplit_on=false

ZSH_ABBR_INITIALIZING=1

typeset -gA ZSH_ABBR_USER_COMMANDS
typeset -gA ZSH_ABBR_SESSION_COMMANDS
typeset -gA ZSH_ABBR_USER_GLOBALS
Expand All @@ -849,8 +813,8 @@ _zsh_abbr_init() {
ZSH_ABBR_USER_GLOBALS=()
ZSH_ABBR_SESSION_GLOBALS=()

function _zsh_abbr:init:configure() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:init:configure"
function _zsh_abbr_init:configure() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr_init:configure"

if [[ $options[shwordsplit] = on ]]; then
session_shwordsplit_on=true
Expand All @@ -866,8 +830,8 @@ _zsh_abbr_init() {
chmod 600 ${TMPDIR:-/tmp/}zsh-user-global-abbreviations
}

function _zsh_abbr:init:seed() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:init:seed"
function _zsh_abbr_init:seed() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr_init:seed"

# Load saved user abbreviations
if [ -f $ZSH_ABBR_USER_PATH ]; then
Expand All @@ -887,127 +851,97 @@ _zsh_abbr_init() {
typeset -p ZSH_ABBR_USER_GLOBALS > ${TMPDIR:-/tmp/}zsh-user-global-abbreviations
}

# open job
echo $RANDOM >/dev/null
job=$(_zsh_abbr_job:name)
_zsh_abbr_job:push $job $job_group

# init
_zsh_abbr:init:configure
_zsh_abbr:init:seed

# close job
_zsh_abbr_job:pop $job $job_group
ZSH_ABBR_INITIALIZING=0
_zsh_abbr_job_push $job
_zsh_abbr_init:configure
_zsh_abbr_init:seed
_zsh_abbr_job_pop $job
} always {
unfunction -m _zsh_abbr:init:configure
unfunction -m _zsh_abbr:init:seed
unfunction -m _zsh_abbr_init:configure
unfunction -m _zsh_abbr_init:seed
}
}

_zsh_abbr_job:push() {
_zsh_abbr_job_push() {
{
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr_job:push"
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr_job_push"

local next_job
local next_job_age
local next_job_path
local job
local job_dir
local job_group
local job_path
local timeout_age

job=${(q)1}
job_group=${(q)2}
timeout_age=30 # seconds

job_dir=${TMPDIR:-/tmp/}zsh-abbr-jobs/$job_group
job_dir=${TMPDIR:-/tmp/}zsh-abbr-jobs
job_path=$job_dir/$job
timeout_age=30 # seconds

function _zsh_abbr_job:push:add_job() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:_zsh_abbr_job:push:add_job"
function _zsh_abbr_job_push:add_job() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr_job_push:add_job"

if ! [ -d $job_dir ]; then
mkdir -p $job_dir
fi

if ! [ -d ${TMPDIR:-/tmp/}zsh-abbr-jobs/current ]; then
mkdir ${TMPDIR:-/tmp/}zsh-abbr-jobs/current
fi

echo $job_group > $job_path
touch $job_path
}

function _zsh_abbr_job:push:get_next_job() {
function _zsh_abbr_job_push:next_job() {
# cannout support debug message

ls -t $job_dir | tail -1
}

function _zsh_abbr_job:push:handle_timeout() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:_zsh_abbr_job:push:handle_timeout"
function _zsh_abbr_job_push:handle_timeout() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr_job_push:handle_timeout"

next_job_path=$job_dir/$next_job

echo "abbr: An job added at"
echo "abbr: A job added at"
echo " $(strftime '%T %b %d %Y' ${next_job%.*})"
echo "has timed out. The job was related to"
echo " $(cat $next_job_path)"
echo "Please report this at https://github.com/olets/zsh-abbr/issues/new"
echo

rm $next_job_path &>/dev/null
rm ${TMPDIR:-/tmp/}zsh-abbr-jobs/current/$job_group* &>/dev/null
}

function _zsh_abbr_job:push:wait_turn() {
while [[ $(_zsh_abbr_job:push:get_next_job) != $job ]]; do
next_job=$(_zsh_abbr_job:push:get_next_job)
function _zsh_abbr_job_push:wait_turn() {
while [[ $(_zsh_abbr_job_push:next_job) != $job ]]; do
next_job=$(_zsh_abbr_job_push:next_job)
next_job_age=$(( $(date +%s) - ${next_job%.*} ))

if (( $next_job_age > $timeout_age )); then
_zsh_abbr_job:push:handle_timeout
_zsh_abbr_job_push:handle_timeout
fi

sleep 0.01
done

cp $job_path ${TMPDIR:-/tmp/}zsh-abbr-jobs/current/$job_group-$job
}

_zsh_abbr_job:push:add_job
_zsh_abbr_job:push:wait_turn
_zsh_abbr_job_push:add_job
_zsh_abbr_job_push:wait_turn
} always {
unfunction -m _zsh_abbr_job:push:add_job
unfunction -m _zsh_abbr_job:push:get_next_job
unfunction -m _zsh_abbr_job:push:handle_timeout
unfunction -m _zsh_abbr_job:push:wait_turn
unfunction -m _zsh_abbr_job_push:add_job
unfunction -m _zsh_abbr_job_push:next_job
unfunction -m _zsh_abbr_job_push:handle_timeout
unfunction -m _zsh_abbr_job_push:wait_turn
}
}

_zsh_abbr_job:pop() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr_job:pop"
_zsh_abbr_job_pop() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr_job_pop"

local current
local currents
local job
local job_group

job=${(q)1}
job_group=$2

typeset -a currents
currents=(${(@f)$(ls -d ${TMPDIR:-/tmp/}zsh-abbr-jobs/current/$job_group* 2>/dev/null)})

for current in $currents; do
rm $current &>/dev/null
done

rm ${TMPDIR:-/tmp/}zsh-abbr-jobs/${job_group}/${job} &>/dev/null
rm ${TMPDIR:-/tmp/}zsh-abbr-jobs/$job &>/dev/null
}

_zsh_abbr_job:name() {
_zsh_abbr_job_name() {
# cannout support debug message

echo "$(date +%s).$RANDOM"
Expand Down Expand Up @@ -1054,12 +988,15 @@ abbr() {

# INITIALIZATION
# --------------

ZSH_ABBR_DEBUG=0
# ZSH_ABBR_RUNNING=0
ZSH_ABBR_SOURCE_PATH=${0:A:h}

ZSH_ABBR_INITIALIZING=1
_zsh_abbr_init

ZSH_ABBR_INITIALIZING=0

if (( $ZSH_ABBR_DEFAULT_BINDINGS )) || [ $ZSH_ABBR_DEFAULT_BINDINGS = true ]; then
_zsh_abbr_bind_widgets
fi

0 comments on commit f50df95

Please sign in to comment.