Itcl - the [incr Tcl] extension

Check-in [8a6f5cc182]
Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2019 Conference, Houston/TX, US, Nov 4-8
Send your abstracts to [email protected]
or submit via the online form by Sep 9.

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:on-demand var-resolver ready - code cleanup & review
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sebres-on-dmnd-resolver-perf-branch
Files: files | file ages | folders
SHA3-256: 8a6f5cc182cd11245c07d1ef3bd9cb5245122845174fe66a42ccf87705187be3
User & Date: sebres 2019-04-17 20:47:17
Context
2019-05-29
12:14
merged trunk check-in: 34b8ed67f7 user: sebres tags: sebres-on-dmnd-resolver-perf-branch
2019-04-17
20:47
on-demand var-resolver ready - code cleanup & review check-in: 8a6f5cc182 user: sebres tags: sebres-on-dmnd-resolver-perf-branch
20:46
more performance cases (covering memory preservation, branch sebres-memopt-perf-branch) check-in: ce8cbde7be user: sebres tags: sebres-on-dmnd-resolver-perf-branch
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to generic/itclClass.c.

1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
 * ------------------------------------------------------------------------
 */
Tcl_HashEntry *
ItclResolveVarEntry(
    ItclClass* iclsPtr,       /* class definition where to resolve variable */
    const char *lookupName)      /* name of variable being resolved */
{
#if 0
    Tcl_HashEntry *reshPtr;

    reshPtr = Tcl_FindHashEntry(&iclsPtr->resolveVars, lookupName);
    printf("====get======== %p : %s -- v: %s -> %p ===== \n", iclsPtr, Tcl_GetString(iclsPtr->fullNamePtr), lookupName, reshPtr);
    return reshPtr;
#else
    Tcl_HashEntry *reshPtr, *hPtr;

    /* could be resolved directly */
    if ((reshPtr = Tcl_FindHashEntry(&iclsPtr->resolveVars, lookupName)) != NULL) {
	return reshPtr;
    } else {

    /* try to build virtual table for this var */
    const char *varName, *simpleName;
    Tcl_DString buffer, buffer2, *bufferC;
    ItclHierIter hier;
    ItclClass* iclsPtr2;
    ItclVarLookup *vlookup;
    ItclVariable *ivPtr;
    Tcl_Namespace* nsPtr;
    Tcl_Obj *vnObjPtr;
    int newEntry, processAncestors;
    size_t varLen;
  
    /* (de)qualify to simple name */
    varName = simpleName = lookupName;
    while(*varName) {
        if (*varName++ == ':') {
            if (*varName++ == ':') { simpleName = varName; }
        };
    }
    vnObjPtr = Tcl_NewStringObj(simpleName, -1);
    
    processAncestors = simpleName != lookupName;

    //!!! printf("====get======== %p : %s -- v: %s (%s), pa: %d ===== \n", iclsPtr, Tcl_GetString(iclsPtr->fullNamePtr), lookupName, simpleName, processAncestors);
    
    Tcl_DStringInit(&buffer);
    Tcl_DStringInit(&buffer2);

    /*
     *  Scan through all classes in the hierarchy, from most to
     *  least specific.  Add a lookup entry for each variable
     *  into the table.
     */
    Itcl_InitHierIter(&hier, iclsPtr);
    iclsPtr2 = Itcl_AdvanceHierIter(&hier);
    while (iclsPtr2 != NULL) {

	hPtr = Tcl_FindHashEntry(&iclsPtr2->variables, vnObjPtr);
        //!!!printf("    **** find %p : %s : %p\n", iclsPtr2, Tcl_GetString(iclsPtr2->fullNamePtr), hPtr);
	if (hPtr) {
	    ivPtr = (ItclVariable*)Tcl_GetHashValue(hPtr);

            vlookup = NULL;

            /*
             *  Create all possible names for this variable and enter
             *  them into the variable resolution table:
             *     var
             *     class::var
             *     namesp1::class::var
             *     namesp2::namesp1::class::var
             *     ...
             */
            varName = simpleName; varLen = -1;
            bufferC = &buffer;
            nsPtr = iclsPtr2->nsPtr;

            while (1) {
		hPtr = Tcl_CreateHashEntry(&iclsPtr->resolveVars,
		    varName, &newEntry);
                //!!! printf("    **** %s %p : %s : %s -> %p\n", newEntry ? "find   " : "created", iclsPtr2, Tcl_GetString(iclsPtr2->fullNamePtr), varName, hPtr);

		/* check for same name in current class */
		if (!newEntry) {
		    vlookup = (ItclVarLookup*)Tcl_GetHashValue(hPtr);
		    //!!! printf("    **** exist %p : %p != %p / %s != %s && %d\n", vlookup, vlookup->ivPtr, ivPtr, Tcl_GetString(vlookup->ivPtr->fullNamePtr), Tcl_GetString(ivPtr->fullNamePtr), iclsPtr2 == iclsPtr);
		    if (vlookup->ivPtr != ivPtr && iclsPtr2 == iclsPtr) {
		    	/* if used multiple times - unbind, else - overwrite */
			if (vlookup->usage > 1) {
			    /* correct leastQualName */
			    vlookup->leastQualName = NULL;
			    processAncestors = 1; /* correction in progress */
			    /* should create new lookup */
			    --vlookup->usage;
			    vlookup = NULL;
			} else {
			    /* correct values (overwrite) */
			    vlookup->usage = 0;
			    goto setResVar;
			}
			newEntry = 1;
		    } else {
#if 1
		    	/* var exists and no correction necessary - next var */
			if (!processAncestors) {
			    break;
			}
#endif
			/* check leastQualName correction needed */
			if (!vlookup->leastQualName) {
			    vlookup->leastQualName = 
				Tcl_GetHashKey(&iclsPtr->resolveVars, hPtr);
			}
			/* reset vlookup for full-qualified names - new lookup */
			if (vlookup->ivPtr != ivPtr) {
			    vlookup = NULL;
			}
		    }
		}
		//!!! printf("    **** newEntry %d, vlookup: %p\n", newEntry, vlookup);
		if (newEntry) {
		    if (!vlookup) {
			/* create new (or overwrite) */
			vlookup = (ItclVarLookup *)ckalloc(sizeof(ItclVarLookup));
			vlookup->usage = 0;

		    setResVar:

			vlookup->ivPtr = ivPtr;
			vlookup->leastQualName = 
			    Tcl_GetHashKey(&iclsPtr->resolveVars, hPtr);

			/*
			 *  If this variable is PRIVATE to another class scope,
			 *  then mark it as "inaccessible".
			 */
			vlookup->accessible = (ivPtr->protection != ITCL_PRIVATE ||
				ivPtr->iclsPtr == iclsPtr);

			/*
			 *  Set aside the first object-specific slot for the built-in
			 *  "this" variable.  Only allocate one of these, even though
			 *  there is a definition for "this" in each class scope.
			 *  Set aside the second and third object-specific slot for the built-in
			 *  "itcl_options" and "itcl_option_components" variable.
			 */
			if (!iclsPtr->numInstanceVars) {
			    iclsPtr->numInstanceVars++;
			    iclsPtr->numInstanceVars++;
			    iclsPtr->numInstanceVars++;
			}
			/*
			 *  If this is a reference to the built-in "this"
			 *  variable, then its index is "0".  Otherwise,
			 *  add another slot to the end of the table.
			 */
			if ((ivPtr->flags & ITCL_THIS_VAR) != 0) {
			    vlookup->varNum = 0;
			} else {
			    if ((ivPtr->flags & ITCL_OPTIONS_VAR) != 0) {
				vlookup->varNum = 1;
			    } else {
				vlookup->varNum = iclsPtr->numInstanceVars++;
			    }
			}
		    }

		    Tcl_SetHashValue(hPtr, (ClientData)vlookup);
		    vlookup->usage++;
		}

                /* if we have found it */
                if (simpleName == lookupName || strcmp(varName, lookupName) == 0) {
		    if (!reshPtr) {
			reshPtr = hPtr;
		    }
		    break;
                }
                if (nsPtr == NULL) {
                    break;
                }
                Tcl_DStringSetLength(bufferC, 0);
                Tcl_DStringAppend(bufferC, nsPtr->name, -1);
                Tcl_DStringAppend(bufferC, "::", 2);
                Tcl_DStringAppend(bufferC, varName, varLen);
                varName = Tcl_DStringValue(bufferC);
                varLen = Tcl_DStringLength(bufferC);
                bufferC = (bufferC == &buffer) ? &buffer2 : &buffer;

                nsPtr = nsPtr->parentPtr;
            }

        }

	/* Stop create vars for ancestors (if not needed) */
	if (!processAncestors && reshPtr) {
#if 1
	    /* simple name - don't need to check ancestors */
	    break;
#endif
	}

        iclsPtr2 = Itcl_AdvanceHierIter(&hier);
    }
    Itcl_DeleteHierIter(&hier);

#if 0 /* DUMP var table */
    //!!!
    printf("===DUMP===== %p : %s ===== \n", iclsPtr, Tcl_GetString(iclsPtr->fullNamePtr));

    Itcl_InitHierIter(&hier, iclsPtr);
    iclsPtr2 = Itcl_AdvanceHierIter(&hier);
    while (iclsPtr2 != NULL) {
    	Tcl_HashSearch place;

        //!!!
        printf("  ****** %p : %s\n", iclsPtr2, Tcl_GetString(iclsPtr2->fullNamePtr));
        hPtr = Tcl_FirstHashEntry(&iclsPtr2->variables, &place);
        while (hPtr) {
            ivPtr = (ItclVariable*)Tcl_GetHashValue(hPtr);

            Tcl_DStringSetLength(&buffer, 0);
            Tcl_DStringAppend(&buffer, Tcl_GetString(ivPtr->namePtr), -1);
            nsPtr = iclsPtr2->nsPtr;

            while (1) {
                hPtr = Tcl_FindHashEntry(&iclsPtr->resolveVars, Tcl_DStringValue(&buffer));
                if (hPtr) {
                    vlookup = (ItclVarLookup *)Tcl_GetHashValue(hPtr);
    
                    //!!!
                    printf("    **** %p : %s : %s -> %s, (%d) %p\n", iclsPtr2, Tcl_GetString(iclsPtr2->fullNamePtr), Tcl_DStringValue(&buffer), vlookup->leastQualName ? vlookup->leastQualName : "<NULL>", vlookup->usage, vlookup);
                }
                if (nsPtr == NULL) {
                    break;
                }
                Tcl_DStringSetLength(&buffer2, 0);
                Tcl_DStringAppend(&buffer2, Tcl_DStringValue(&buffer), -1);
                Tcl_DStringSetLength(&buffer, 0);
                Tcl_DStringAppend(&buffer, nsPtr->name, -1);
                Tcl_DStringAppend(&buffer, "::", -1);
                Tcl_DStringAppend(&buffer, Tcl_DStringValue(&buffer2), -1);

                nsPtr = nsPtr->parentPtr;
            }

            hPtr = Tcl_NextHashEntry(&place);
        }
        iclsPtr2 = Itcl_AdvanceHierIter(&hier);
    }
    Itcl_DeleteHierIter(&hier);
#endif

	Tcl_DStringFree(&buffer);
	Tcl_DStringFree(&buffer2);
	Tcl_DecrRefCount(vnObjPtr);

	if (reshPtr == NULL) {
	    reshPtr = Tcl_FindHashEntry(&iclsPtr->resolveVars, lookupName);
	}
	//!!! printf("====ret======== %p : %s ===== \n", reshPtr, reshPtr && ((ItclVarLookup*)Tcl_GetHashValue(reshPtr))->leastQualName ? ((ItclVarLookup*)Tcl_GetHashValue(reshPtr))->leastQualName : "<NULL>");
	return reshPtr;
    }
#endif
}
 
/*
 * ------------------------------------------------------------------------
 *  Itcl_BuildVirtualTables()
 *
 *  Invoked whenever the class heritage changes or members are added or






<
<
<
<
<
<
<






<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

<
<
|
|

|
|
|
|
|
|
|
|

|
<
|
|

|

|
|
|
|
|
|
|
|
|
|
|
|

|
|
|
<

|
|
|
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
|
|
|
|
<
|
|
|
|
|
|
|
|
|
|
|
<
|
|
|
|
|

|

|
|
|

|
|
|
|
|
|

|
|
|
|
|
|
|
|
|
<
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|
|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

|
|

|

|
|
<
|
|
<
|

|
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<








<


<







1695
1696
1697
1698
1699
1700
1701







1702
1703
1704
1705
1706
1707

1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730


1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743

1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764

1765
1766
1767
1768

1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784

1785
1786
1787
1788

1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799

1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827


1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873

1874
1875

1876
1877
1878
1879
1880















































1881
1882
1883
1884
1885
1886
1887
1888

1889
1890

1891
1892
1893
1894
1895
1896
1897
 * ------------------------------------------------------------------------
 */
Tcl_HashEntry *
ItclResolveVarEntry(
    ItclClass* iclsPtr,       /* class definition where to resolve variable */
    const char *lookupName)      /* name of variable being resolved */
{







    Tcl_HashEntry *reshPtr, *hPtr;

    /* could be resolved directly */
    if ((reshPtr = Tcl_FindHashEntry(&iclsPtr->resolveVars, lookupName)) != NULL) {
	return reshPtr;
    } else {

	/* try to build virtual table for this var */
	const char *varName, *simpleName;
	Tcl_DString buffer, buffer2, *bufferC;
	ItclHierIter hier;
	ItclClass* iclsPtr2;
	ItclVarLookup *vlookup;
	ItclVariable *ivPtr;
	Tcl_Namespace* nsPtr;
	Tcl_Obj *vnObjPtr;
	int newEntry, processAncestors;
	size_t varLen;
      
	/* (de)qualify to simple name */
	varName = simpleName = lookupName;
	while(*varName) {
	    if (*varName++ == ':') {
		if (*varName++ == ':') { simpleName = varName; }
	    };
	}
	vnObjPtr = Tcl_NewStringObj(simpleName, -1);
	
	processAncestors = simpleName != lookupName;



	Tcl_DStringInit(&buffer);
	Tcl_DStringInit(&buffer2);

	/*
	 *  Scan through all classes in the hierarchy, from most to
	 *  least specific.  Add a lookup entry for each variable
	 *  into the table.
	 */
	Itcl_InitHierIter(&hier, iclsPtr);
	iclsPtr2 = Itcl_AdvanceHierIter(&hier);
	while (iclsPtr2 != NULL) {

	    hPtr = Tcl_FindHashEntry(&iclsPtr2->variables, vnObjPtr);

	    if (hPtr) {
		ivPtr = (ItclVariable*)Tcl_GetHashValue(hPtr);

		vlookup = NULL;

		/*
		 *  Create all possible names for this variable and enter
		 *  them into the variable resolution table:
		 *     var
		 *     class::var
		 *     namesp1::class::var
		 *     namesp2::namesp1::class::var
		 *     ...
		 */
		varName = simpleName; varLen = -1;
		bufferC = &buffer;
		nsPtr = iclsPtr2->nsPtr;

		while (1) {
		    hPtr = Tcl_CreateHashEntry(&iclsPtr->resolveVars,
			varName, &newEntry);


		    /* check for same name in current class */
		    if (!newEntry) {
			vlookup = (ItclVarLookup*)Tcl_GetHashValue(hPtr);

			if (vlookup->ivPtr != ivPtr && iclsPtr2 == iclsPtr) {
			    /* if used multiple times - unbind, else - overwrite */
			    if (vlookup->usage > 1) {
				/* correct leastQualName */
				vlookup->leastQualName = NULL;
				processAncestors = 1; /* correction in progress */
				/* should create new lookup */
				--vlookup->usage;
				vlookup = NULL;
			    } else {
				/* correct values (overwrite) */
				vlookup->usage = 0;
				goto setResVar;
			    }
			    newEntry = 1;
			} else {

			    /* var exists and no correction necessary - next var */
			    if (!processAncestors) {
				break;
			    }

			    /* check leastQualName correction needed */
			    if (!vlookup->leastQualName) {
				vlookup->leastQualName = 
				    Tcl_GetHashKey(&iclsPtr->resolveVars, hPtr);
			    }
			    /* reset vlookup for full-qualified names - new lookup */
			    if (vlookup->ivPtr != ivPtr) {
				vlookup = NULL;
			    }
			}
		    }

		    if (newEntry) {
			if (!vlookup) {
			    /* create new (or overwrite) */
			    vlookup = (ItclVarLookup *)ckalloc(sizeof(ItclVarLookup));
			    vlookup->usage = 0;

			setResVar:

			    vlookup->ivPtr = ivPtr;
			    vlookup->leastQualName = 
				Tcl_GetHashKey(&iclsPtr->resolveVars, hPtr);

			    /*
			     *  If this variable is PRIVATE to another class scope,
			     *  then mark it as "inaccessible".
			     */
			    vlookup->accessible = (ivPtr->protection != ITCL_PRIVATE ||
				    ivPtr->iclsPtr == iclsPtr);

			    /*
			     *  Set aside the first object-specific slot for the built-in
			     *  "this" variable.  Only allocate one of these, even though
			     *  there is a definition for "this" in each class scope.
			     *  Set aside the second and third object-specific slot for the built-in
			     *  "itcl_options" and "itcl_option_components" variable.
			     */
			    if (!iclsPtr->numInstanceVars) {
				iclsPtr->numInstanceVars += 3;


			    }
			    /*
			     *  If this is a reference to the built-in "this"
			     *  variable, then its index is "0".  Otherwise,
			     *  add another slot to the end of the table.
			     */
			    if ((ivPtr->flags & ITCL_THIS_VAR) != 0) {
				vlookup->varNum = 0;
			    } else {
				if ((ivPtr->flags & ITCL_OPTIONS_VAR) != 0) {
				    vlookup->varNum = 1;
				} else {
				    vlookup->varNum = iclsPtr->numInstanceVars++;
				}
			    }
			}

			Tcl_SetHashValue(hPtr, (ClientData)vlookup);
			vlookup->usage++;
		    }

		    /* if we have found it */
		    if (simpleName == lookupName || strcmp(varName, lookupName) == 0) {
			if (!reshPtr) {
			    reshPtr = hPtr;
			}
			break;
		    }
		    if (nsPtr == NULL) {
			break;
		    }
		    Tcl_DStringSetLength(bufferC, 0);
		    Tcl_DStringAppend(bufferC, nsPtr->name, -1);
		    Tcl_DStringAppend(bufferC, "::", 2);
		    Tcl_DStringAppend(bufferC, varName, varLen);
		    varName = Tcl_DStringValue(bufferC);
		    varLen = Tcl_DStringLength(bufferC);
		    bufferC = (bufferC == &buffer) ? &buffer2 : &buffer;

		    nsPtr = nsPtr->parentPtr;
		}

	    }

	    /* Stop create vars for ancestors (if not needed) */
	    if (!processAncestors && reshPtr) {

		/* simple name - don't need to check ancestors */
		break;

	    }

	    iclsPtr2 = Itcl_AdvanceHierIter(&hier);
	}
	Itcl_DeleteHierIter(&hier);
















































	Tcl_DStringFree(&buffer);
	Tcl_DStringFree(&buffer2);
	Tcl_DecrRefCount(vnObjPtr);

	if (reshPtr == NULL) {
	    reshPtr = Tcl_FindHashEntry(&iclsPtr->resolveVars, lookupName);
	}

	return reshPtr;
    }

}
 
/*
 * ------------------------------------------------------------------------
 *  Itcl_BuildVirtualTables()
 *
 *  Invoked whenever the class heritage changes or members are added or