lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

It was thus said that the Great Ranier Vilela once stated:
> Em dom., 15 de nov. de 2020 às 22:17, Andrew Gierth <
> >  Ranier> Strchr is it's more efficient than strcmp.
> >
> > It also does something completely different, which is not applicable
> > here.
> Basically, it does a search for '-', which strchr is much more efficient.
> In addition, strchr is used elsewhere in the code to do the same.

  The bit in question:

--- a/lua.c
+++ b/lua.c
@@ -220,7 +220,7 @@ static int pushargs (lua_State *L) {
 static int handle_script (lua_State *L, char **argv) {
   int status;
   const char *fname = argv[0];
-  if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0)
+  if ((strchr(fname, '-') != NULL) && strncmp(argv[-1], "--", 2) != 0)
     fname = NULL;  /* stdin */
   status = luaL_loadfile(L, fname);
   if (status == LUA_OK) {

strcmp() *compares* two strings for equivalence---it is comparing fname to
see if it is "-", *not* that it contains a '-', which is what strchr() does. 
That's two different things.

  And if string comparisons are of such performance important, you might as
well skip strncmp() entirely (which still has to check for a NUL byte
anyway) and use a straight memcmp():

	if (strcmp(fname, "-") == 0 && memcmp(argv[-1], "--",2) != 0)

  My question to you is then---have you actually made these changes, run the
Lua regression test to see if it passes, *and* profile the code to see if it
indeed makes any difference to the speed?  If you are pushing for these
changes, then I would think it would be up to you to perform such work, if
only to make it easier for the Lua team to accept the patches.