diff --git a/lcode.c b/lcode.c
index e6a98bb6..8f08302e 100644
--- a/lcode.c
+++ b/lcode.c
@@ -563,13 +563,13 @@ static int addk (FuncState *fs, Proto *f, TValue *v) {
 static int k2proto (FuncState *fs, TValue *key, TValue *v) {
   TValue val;
   Proto *f = fs->f;
-  int tag = luaH_get(fs->ls->h, key, &val);  /* query scanner table */
+  int tag = luaH_get(fs->kcache, key, &val);  /* query scanner table */
   int k;
-  if (tag == LUA_VNUMINT) {  /* is there an index there? */
+  if (!tagisempty(tag)) {  /* is there an index there? */
     k = cast_int(ivalue(&val));
+    lua_assert(k < fs->nk);
     /* correct value? (warning: must distinguish floats from integers!) */
-    if (k < fs->nk && ttypetag(&f->k[k]) == ttypetag(v) &&
-                      luaV_rawequalobj(&f->k[k], v))
+    if (ttypetag(&f->k[k]) == ttypetag(v) && luaV_rawequalobj(&f->k[k], v))
       return k;  /* reuse index */
   }
   /* constant not found; create a new entry */
@@ -577,7 +577,7 @@ static int k2proto (FuncState *fs, TValue *key, TValue *v) {
   /* cache for reuse; numerical value does not need GC barrier;
      table has no metatable, so it does not need to invalidate cache */
   setivalue(&val, k);
-  luaH_set(fs->ls->L, fs->ls->h, key, &val);
+  luaH_set(fs->ls->L, fs->kcache, key, &val);
   return k;
 }
 
@@ -659,7 +659,7 @@ static int nilK (FuncState *fs) {
   TValue k, v;
   setnilvalue(&v);
   /* cannot use nil as key; instead use table itself to represent nil */
-  sethvalue(fs->ls->L, &k, fs->ls->h);
+  sethvalue(fs->ls->L, &k, fs->kcache);
   return k2proto(fs, &k, &v);
 }
 
diff --git a/llex.c b/llex.c
index b2e77c9c..d913db17 100644
--- a/llex.c
+++ b/llex.c
@@ -130,18 +130,15 @@ l_noret luaX_syntaxerror (LexState *ls, const char *msg) {
 ** Creates a new string and anchors it in scanner's table so that it
 ** will not be collected until the end of the compilation; by that time
 ** it should be anchored somewhere. It also internalizes long strings,
-** ensuring there is only one copy of each unique string.  The table
-** here is used as a set: the string enters as the key, while its value
-** is irrelevant. We use the string itself as the value only because it
-** is a TValue readily available. Later, the code generation can change
-** this value.
+** ensuring there is only one copy of each unique string.
 */
 TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
   lua_State *L = ls->L;
   TString *ts = luaS_newlstr(L, str, l);  /* create new string */
-  TString *oldts = luaH_getstrkey(ls->h, ts);
-  if (oldts != NULL)  /* string already present? */
-    return oldts;  /* use it */
+  TValue oldts;
+  int tag = luaH_getstr(ls->h, ts, &oldts);
+  if (!tagisempty(tag))  /* string already present? */
+    return tsvalue(&oldts);  /* use stored value */
   else {  /* create a new entry */
     TValue *stv = s2v(L->top.p++);  /* reserve stack space for string */
     setsvalue(L, stv, ts);  /* temporarily anchor the string */
@@ -149,8 +146,8 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
     /* table is not a metatable, so it does not need to invalidate cache */
     luaC_checkGC(L);
     L->top.p--;  /* remove string from stack */
+    return ts;
   }
-  return ts;
 }
 
 
diff --git a/lparser.c b/lparser.c
index 3db7df4c..642e43b7 100644
--- a/lparser.c
+++ b/lparser.c
@@ -737,6 +737,7 @@ static void codeclosure (LexState *ls, expdesc *v) {
 
 
 static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {
+  lua_State *L = ls->L;
   Proto *f = fs->f;
   fs->prev = ls->fs;  /* linked list of funcstates */
   fs->ls = ls;
@@ -757,8 +758,11 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {
   fs->firstlabel = ls->dyd->label.n;
   fs->bl = NULL;
   f->source = ls->source;
-  luaC_objbarrier(ls->L, f, f->source);
+  luaC_objbarrier(L, f, f->source);
   f->maxstacksize = 2;  /* registers 0/1 are always valid */
+  fs->kcache = luaH_new(L);  /* create table for function */
+  sethvalue2s(L, L->top.p, fs->kcache);  /* anchor it */
+  luaD_inctop(L);
   enterblock(fs, bl, 0);
 }
 
@@ -780,6 +784,7 @@ static void close_func (LexState *ls) {
   luaM_shrinkvector(L, f->locvars, f->sizelocvars, fs->ndebugvars, LocVar);
   luaM_shrinkvector(L, f->upvalues, f->sizeupvalues, fs->nups, Upvaldesc);
   ls->fs = fs->prev;
+  L->top.p--;  /* pop kcache table */
   luaC_checkGC(L);
 }
 
diff --git a/lparser.h b/lparser.h
index 8a87776d..589befdb 100644
--- a/lparser.h
+++ b/lparser.h
@@ -146,6 +146,7 @@ typedef struct FuncState {
   struct FuncState *prev;  /* enclosing function */
   struct LexState *ls;  /* lexical state */
   struct BlockCnt *bl;  /* chain of current blocks */
+  Table *kcache;  /* cache for reusing constants */
   int pc;  /* next position to code (equivalent to 'ncode') */
   int lasttarget;   /* 'label' of last 'jump label' */
   int previousline;  /* last line that was saved in 'lineinfo' */
diff --git a/ltable.c b/ltable.c
index 052e005e..eb5abf9f 100644
--- a/ltable.c
+++ b/ltable.c
@@ -962,7 +962,7 @@ lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res) {
 */
 const TValue *luaH_Hgetshortstr (Table *t, TString *key) {
   Node *n = hashstr(t, key);
-  lua_assert(key->tt == LUA_VSHRSTR);
+  lua_assert(strisshr(key));
   for (;;) {  /* check whether 'key' is somewhere in the chain */
     if (keyisshrstr(n) && eqshrstr(keystrval(n), key))
       return gval(n);  /* that's it */
@@ -997,15 +997,6 @@ lu_byte luaH_getstr (Table *t, TString *key, TValue *res) {
 }
 
 
-TString *luaH_getstrkey (Table *t, TString *key) {
-  const TValue *o = Hgetstr(t, key);
-  if (!isabstkey(o))  /* string already present? */
-    return keystrval(nodefromval(o));  /* get saved copy */
-  else
-    return NULL;
-}
-
-
 /*
 ** main search function
 */
diff --git a/ltable.h b/ltable.h
index e4aa98f0..ca21e692 100644
--- a/ltable.h
+++ b/ltable.h
@@ -154,8 +154,6 @@ LUAI_FUNC lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res);
 /* Special get for metamethods */
 LUAI_FUNC const TValue *luaH_Hgetshortstr (Table *t, TString *key);
 
-LUAI_FUNC TString *luaH_getstrkey (Table *t, TString *key);
-
 LUAI_FUNC int luaH_psetint (Table *t, lua_Integer key, TValue *val);
 LUAI_FUNC int luaH_psetshortstr (Table *t, TString *key, TValue *val);
 LUAI_FUNC int luaH_psetstr (Table *t, TString *key, TValue *val);