zsh-workers
 help / color / mirror / code / Atom feed
From: Doron Behar <doron.behar@gmail.com>
To: zsh-workers@zsh.org
Subject: Re: [PATCH 1/1] Squashed commit of the following:
Date: Tue, 5 Jun 2018 18:41:27 +0300	[thread overview]
Message-ID: <20180605154121.tfrw6oheq3ce6qkr@NUC.doronbehar.com> (raw)
In-Reply-To: <31972.1527893117@thecus>

On Sat, Jun 02, 2018 at 12:45:17AM +0200, Oliver Kiddle wrote:
> Doron Behar wrote:
> > However, the bad news is that I'm afraid that calling `luarocks config`
> > twice like that whenever I query the cache validity, is a huge
> > performance hit.
> 
> > The solution which will most likely best solve this issue is to use a
> > similar cache mechanism for these values as well. This *inner* cache's
> 
> Sounds rather complicated but perhaps necessary depending on just how
> slow it is. A global variable – typically named _cache_… – is one
> option for caching that avoids much of the complexity of the disk cache
> mechanism if the lifetime of the session is an appropriate policy for
> how long to retain the cached information.
> 
> > I'd be glad to get some feedback, thanks!
> 
> 
> > 	(( $+functions[___luarocks_manually_store_cache_configs_paths] )) ||
> > 	___luarocks_manually_store_cache_configs_paths(){
> > 	  user_config_path="$(_call_program user_config_path luarocks config --user-config)"
> > 	  system_config_path="$(_call_program system_config_path luarocks config --system-config)"
> 
> These variables should be declared local. If the intention is for them
> to be global, use typeset -g and prefix the names with something like
> _cache_luarocks_.
> 
> > 	  print user_config_path=$user_config_path > ${cache_dir}/luarocks_configs_paths
> > 	  print system_config_path=$system_config_path >> ${cache_dir}/luarocks_configs_paths

Yea I forgot to make them local alongside configured_lua_version
configured_user_tree configured_system_tree. Done, thanks.

> 
> You might need to quote the values with ${(qq)user_config_path} in case
> they have spaces in their values. By using braces around the print
> statements only one redirection will be needed instead of the
> redirection and append: { print ...; print ... } > cache

You are right, the `_store_cache` function's definition, uses the
following for every non-array argument given to it:

	print -r "$var=${(Pqq)^^var}"

Since my $var is known, I read the documentation and I understand what
the `P`, and double `q` mean so I'll put:

	print -r var=${(qq)var}

As for the loop running running for every argument `_store_cache` gets,
it ends with >! which should be used here as well according to the
documentation..

> 
> > 	  local where_luarocks=$(where luarocks)
> 
> Use $commands[luarocks] rather than where in a command substitution.
> Command substitution typically requires a forked subshell which will be
> less efficient.

Done.

> 
> > 	  # luarocks_configured_values
> > 	  local configured_lua_version configured_user_tree configured_system_tree
> > 	  # luarocks_configs_paths
> > 	  local config
> > 	  if [[ -e ${cache_dir}/luarocks_configs_paths ]]; then
> > 	    if [ ${where_luarocks} -nt ${cache_dir}/luarocks_configs_paths ]; then
> 
> It is generally better to use [[ ... ]] for all conditions unless you're
> writing a script targeted at /bin/sh – which is not the case here.
> 
> > 	  if [[ -f ${user_manifest_file} ]] || [[ -f ${system_manifest_file} ]]; then
> > 	    if [[ -f ${cache_file} ]]; then
> > 	      # if either one of the manifest files is newer then the cache:
> > 	      if [ ${user_manifest_file} -nt ${cache_file} ] || [ ${system_manifest_file} -nt ${cache_file} ]; then
> > 	        (( 1 ))
> > 	      else
> > 	        (( 0 ))
> > 	      fi
> > 	    else
> > 	      (( 1 ))
> > 	    fi
> > 	  else
> > 	    (( 1 ))
> > 	  fi

I've blindly took the (( 1 )) from a cache policy function I saw on the
documentation.. It's like a return status write?

> 
> I find this (( 1 )) stuff confusing. If I'm not mistaken the whole thing
> is equivalent to:
> 
>   [[ ( ! -f ${user_manifest_file} && ! -f ${system_manifest_file} ) ||
>       ! -f ${cache_file} || ${user_manifest_file} -nt ${cache_file} ||
>       ${system_manifest_file} -nt ${cache_file} ]]
> 
> As for the logic, I'd mainly question what happens when one but not both
> of the manifest files is found to not exist.

The idea with the 1st condition that tests the existence of the
manifests files, is that if non of them exists (which usually never
happens if luarocks is installed properly), is that only one of them is
needed for continuing with the modification date of them vs the cache
file..

Now that I'm thinking about it, I think it would be preferred to improve
the readability of the logic behind this procedure and use `return 0`
instead of `(( 1 ))` and `return 1` instead of `(( 0 ))`. In addition, I
think translating the whole thing to something like the equivalent you
wrote won't help either for readability so I was thinking about something
like this:

	local cache_status=1
	if [[ -f ${cache_file} ]]; then
	  if [[ -f ${user_manifest_file} ]]; then
	    if [[ ${user_manifest_file} -nt ${cache_file} ]]; then
	      cache_status=0
	    fi
	  fi
	  if [[ -f ${system_manifest_file} ]]; then
	    if [[ ${system_manifest_file} -nt ${cache_file} ]]; then
	      cache_status=0
	    else
	      cache_status=1
	    fi
	  fi
	fi
	return cache_status

Better?

> 
> Note that && and || can be used inside [[ ... ]].

Got it.

> 
> Oliver


  parent reply	other threads:[~2018-06-05 15:41 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-26 16:14 [PATCH 0/1] *** Add luarocks completion *** doron.behar
2018-05-26 16:14 ` [PATCH 1/1] Squashed commit of the following: doron.behar
2018-05-28 10:48   ` Oliver Kiddle
2018-05-29 15:38     ` Doron Behar
2018-05-29 22:00       ` Oliver Kiddle
2018-05-30 12:47         ` Doron Behar
2018-05-30 15:43           ` Oliver Kiddle
2018-06-01  7:18             ` Doron Behar
2018-06-01 13:30               ` Doron Behar
2018-06-01 22:45                 ` Oliver Kiddle
2018-06-03 21:46                   ` Daniel Shahaf
2018-06-05 15:41                   ` Doron Behar [this message]
2018-06-07 15:59                     ` Oliver Kiddle
2018-06-07 16:20                       ` Doron Behar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180605154121.tfrw6oheq3ce6qkr@NUC.doronbehar.com \
    --to=doron.behar@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).