git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v5 2/3] advice: revamp advise API

From
Emily Shaffer <emilyshaffer@google.com>
Date
Feb 25, 2020, 19:56 UTC
Message-ID
<20200225195648.GA212281@google.com>
In-Reply-To
<xmqqsgiymupf.fsf@gitster-ct.c.googlers.com>
On Tue, Feb 25, 2020 at 09:40:28AM -0800, Junio C Hamano wrote:
Show 92 quoted lines
> "Heba Waly via GitGitGadget" <gitgitgadget@gmail.com> writes:
> > +static int get_config_value(enum advice_type type)
> > +{
> > +	int value = 1;
> > +	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
> > +
> > +	git_config_get_bool(key, &value);
> > +	free(key);
> > +	return value;
> > +}
> 
> So, in this hypothetical but quite realistic example:
> 
> 	if (advice_enabled(ADVICE_FOO)) {
> 		char *foo = expensive_preparation();
> 		advice_if_enabled(ADVICE_FOO, "use of %s is discouraged", foo);
> 	}
> 
> we end up formulating the "advice.*" key twice and ask git_config_get_bool()
> about the same key twice?
> 
> > +void advise_if_enabled(enum advice_type type, const char *advice, ...)
> > +{
> > +	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
> > +	va_list params;
> > +
> > +	if (!advice_enabled(type))
> > +		return;
> 
> Oh, no, make the number of calls to xstrfmr() three times, not
> twice, as I said in the previous example.
> 
> I wonder if it would make the implementation better to do these:
> 
>  - Rename advice_config_keys[] to advice_setting[] that does not
>    imply it is only about the keys;
> 
>  - This table will know, for each enum advice_type, which
>    configuration variable enables it, *and* if it is enabled.
> 
> i.e.
> 
>         static struct {
>                 const char *config_key;
>                 int disabled;
>         } advice_setting[] = {
>                 [ADVICE_ADD_EMBEDED_REPO] = { "addEmbeddedRepo" },
>                 [ADVICE_AM_WORK_DIR]      = { "amWorkDir" },
>                 ...
>                 [ADVICE_WAITING_FOR_EDITOR] = { "waitingForEditor" },
>         };
> 
> 
> Side Note: you have AMWORKDIR that is unreadable.  If the config
>            name uses camelCase by convention, the UPCASED_WORDS
>            should be separated with underscore at the same word
>            boundary.
> 
> Then, upon the first call to advice_enabled(), call git_config()
> with a callback like
> 
> 	static int populate_advice_settings(const char *var, const char *value, void *cb)
> 	{
> 		int advice_type;
> 		const char *name;
> 
> 		if (!skip_prefix(var, "advice.", &name))
> 			return 0;
> 		advice_type = find_advice_type_by_name(advice_setting, name);
> 		if (advice_type < 0)
> 			return 0; /* unknown advice.* variable */
> 		/* advice.foo=false means advice.foo is disabled */
> 		advice_setting[advice_type].disabled = !git_config_bool(var, value);
> 	}
> 
> only once.  Your get_config_value() would then become a mere lookup
> in advice_setting[] array, e.g.
> 
> 	int advice_enabled(unsigned advice_type)
> 	{
> 		static int initialized;
> 
> 		if (!initialized) {
> 			initialized = 1;
> 			git_config(populate_advice_settings, NULL);
> 		}
> 		if (ARRAY_SIZE(advice_setting) <= advice_type)
> 			BUG("OOB advice type requested???");
> 		return !advice_setting[advice_type].disabled;
> 	}
> 
> with your "push-update-rejected has two names" twist added.

I'm a little confused about the need to cache the result of git_config_get_bool() - isn't that a lookup from a hashmap which is already populated at setup time, and therefore inexpensive? I would think the only expensive part here is the xstrfmt() calls, which it seems like would be easy to do away with by storing the fully-qualified advice key in the array instead. What am I missing?

 - Emily
Previous: Junio C HamanoNext: Junio C Hamano
Message 49 of 76 in “advice: refactor advise API”
  1. advice: refactor advise APIHeba Waly via GitGitGadget, Feb 10, 2020
  2. Derrick StoleeFeb 10, 2020
  3. Junio C HamanoFeb 10, 2020
  4. Taylor BlauFeb 10, 2020
  5. Emily ShafferFeb 10, 2020
  6. Heba WalyFeb 11, 2020
  7. Taylor BlauFeb 12, 2020
  8. Heba WalyFeb 10, 2020
  9. Derrick StoleeFeb 11, 2020
  10. Jeff KingFeb 10, 2020
  11. Emily ShafferFeb 10, 2020
  12. Heba WalyFeb 11, 2020
  13. Jeff KingFeb 11, 2020
  14. Jeff KingFeb 11, 2020
  15. Heba WalyFeb 11, 2020
  16. Junio C HamanoFeb 10, 2020
  17. Heba WalyFeb 11, 2020
  18. Junio C HamanoFeb 11, 2020
  19. 0/2 [RFC][Outreachy] advice: refactor advise APIHeba Waly via GitGitGadget, Feb 16, 2020
  20. 2/2 advice: extract vadvise() from advise()Heba Waly via GitGitGadget, Feb 16, 2020
  21. 1/2 advice: refactor advise APIHeba Waly via GitGitGadget, Feb 16, 2020
  22. Junio C HamanoFeb 17, 2020
  23. Heba WalyFeb 17, 2020
  24. Heba WalyFeb 19, 2020
  25. Junio C HamanoFeb 17, 2020
  26. 0/2 [Outreachy] advice: revamp advise APIHeba Waly via GitGitGadget, Feb 19, 2020
  27. 2/2 advice: extract vadvise() from advise()Heba Waly via GitGitGadget, Feb 19, 2020
  28. Emily ShafferFeb 20, 2020
  29. Heba WalyFeb 21, 2020
  30. 1/2 advice: revamp advise APIHeba Waly via GitGitGadget, Feb 19, 2020
  31. Emily ShafferFeb 20, 2020
  32. Heba WalyFeb 21, 2020
  33. 0/3 [Outreachy] advice: revamp advise APIHeba Waly via GitGitGadget, Feb 24, 2020
  34. 1/3 advice: extract vadvise() from advise()Heba Waly via GitGitGadget, Feb 24, 2020
  35. Emily ShafferFeb 24, 2020
  36. 3/3 tag: use new advice API to check visibilityHeba Waly via GitGitGadget, Feb 24, 2020
  37. Junio C HamanoFeb 24, 2020
  38. Emily ShafferFeb 24, 2020
  39. 2/3 advice: revamp advise APIHeba Waly via GitGitGadget, Feb 24, 2020
  40. Junio C HamanoFeb 24, 2020
  41. Eric SunshineFeb 24, 2020
  42. Heba WalyFeb 24, 2020
  43. Heba WalyFeb 24, 2020
  44. Emily ShafferFeb 24, 2020
  45. 0/3 [Outreachy] advice: revamp advise APIHeba Waly via GitGitGadget, Feb 25, 2020
  46. 1/3 advice: extract vadvise() from advise()Heba Waly via GitGitGadget, Feb 25, 2020
  47. 2/3 advice: revamp advise APIHeba Waly via GitGitGadget, Feb 25, 2020
  48. Junio C HamanoFeb 25, 2020
  49. Emily ShafferFeb 25, 2020
  50. Junio C HamanoFeb 25, 2020
  51. Junio C HamanoFeb 25, 2020
  52. Heba WalyFeb 25, 2020
  53. Junio C HamanoFeb 25, 2020
  54. Heba WalyFeb 26, 2020
  55. Junio C HamanoFeb 26, 2020
  56. Heba WalyFeb 26, 2020
  57. Junio C HamanoFeb 26, 2020
  58. Jonathan TanFeb 26, 2020
  59. 3/3 tag: use new advice API to check visibilityHeba Waly via GitGitGadget, Feb 25, 2020
  60. Junio C HamanoFeb 25, 2020
  61. 0/4 [Outreachy] advice: revamp advise APIHeba Waly via GitGitGadget, Feb 27, 2020
  62. 1/4 advice: extract vadvise() from advise()Heba Waly via GitGitGadget, Feb 27, 2020
  63. 2/4 advice: change "setupStreamFailure" to "setUpstreamFailure"Heba Waly via GitGitGadget, Feb 27, 2020
  64. Junio C HamanoFeb 27, 2020
  65. 3/4 advice: revamp advise APIHeba Waly via GitGitGadget, Feb 27, 2020
  66. Junio C HamanoFeb 27, 2020
  67. Heba WalyFeb 29, 2020
  68. 4/4 tag: use new advice API to check visibilityHeba Waly via GitGitGadget, Feb 27, 2020
  69. 0/4 [Outreachy] advice: revamp advise APIHeba Waly via GitGitGadget, Mar 2, 2020
  70. 1/4 advice: extract vadvise() from advise()Heba Waly via GitGitGadget, Mar 2, 2020
  71. 2/4 advice: change "setupStreamFailure" to "setUpstreamFailure"Heba Waly via GitGitGadget, Mar 2, 2020
  72. 4/4 tag: use new advice API to check visibilityHeba Waly via GitGitGadget, Mar 2, 2020
  73. 3/4 advice: revamp advise APIHeba Waly via GitGitGadget, Mar 2, 2020
  74. Junio C HamanoMar 2, 2020
  75. Junio C HamanoMar 3, 2020
  76. Heba WalyMar 4, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.