Main functions and classes of the FCF framework
Révision | 48d17a9a6bccdd30156eb507ee7b01089ae07f16 (tree) |
---|---|
l'heure | 2022-12-05 11:24:25 |
Auteur | vmarkin |
Commiter | vmarkin |
Added fcf.Cache
@@ -869,6 +869,32 @@ | ||
869 | 869 | } |
870 | 870 | } |
871 | 871 | |
872 | + /// @fn fcf.getApproximateSize(a_value) | |
873 | + /// @brief A very approximate estimate of the memory occupied by an object in bytes. | |
874 | + /// @details The error can reach up to 10 times. | |
875 | + /// Used to protect algorithms from memory overflows | |
876 | + /// @param mixed a_value - Checked value | |
877 | + /// @result number - The amount of memory used by the object | |
878 | + fcf.getApproximateSize = (a_value) => { | |
879 | + if (typeof a_value == "string"){ | |
880 | + return 30 * a_value.length; | |
881 | + } else if (Array.isArray(a_value)) { | |
882 | + let s = 30; | |
883 | + for(let i = 0; i < a_value.length; ++i) { | |
884 | + s += fcf.getApproximateSize(a_value[i]); | |
885 | + } | |
886 | + return s; | |
887 | + } else if (typeof a_value == "object" && a_value !== null) { | |
888 | + let s = 30; | |
889 | + for(let k in a_value){ | |
890 | + s += fcf.getApproximateSize(k); | |
891 | + s += fcf.getApproximateSize(a_value[k]); | |
892 | + } | |
893 | + return s; | |
894 | + } else { | |
895 | + return 30; | |
896 | + } | |
897 | + } | |
872 | 898 | |
873 | 899 | |
874 | 900 | /// @fn object fcf.append(object|array a_dstObject, object|array a_srcObject1, object|array a_srcObject2, ...) |
@@ -1863,6 +1889,7 @@ | ||
1863 | 1889 | } |
1864 | 1890 | } |
1865 | 1891 | |
1892 | + | |
1866 | 1893 | // CACHING CLASSES |
1867 | 1894 | |
1868 | 1895 |
@@ -1871,60 +1898,78 @@ | ||
1871 | 1898 | /// @brief A simple class for caching results |
1872 | 1899 | fcf.Cache = class { |
1873 | 1900 | |
1874 | - // 5000 - 1MB // key string size is equal 13 and value string size is equal 50 | |
1901 | + // 5000 items - 1MB // key string size is equal 13 and value string size is equal 50 | |
1875 | 1902 | // Updating an item in a cache with 5000 items takes approximately 0.6 - 1.5 us. |
1903 | + // Updating an item in a cache with 1000 items takes approximately ~0.3 us. | |
1876 | 1904 | // OS: LINUX |
1877 | 1905 | // Node version: v18.11.0: |
1878 | 1906 | // CPU: Intel(R) Core(TM) i5-2400 CPU @ 3.10GHz |
1879 | - constructor(a_options){ | |
1880 | - this._capacity = !isNaN(a_options.capacity) && a_options.capacity > 0 ? a_options.capacity : 5000; | |
1907 | + constructor(a_mcapacity){ | |
1908 | + this._mcapacity = !isNaN(a_mcapacity) && parseInt(a_mcapacity) > 0 ? a_mcapacity : 1000000; | |
1881 | 1909 | this._cachem = new Map(); |
1882 | 1910 | this._cachev = []; |
1911 | + this._msize = 0; | |
1883 | 1912 | } |
1884 | 1913 | |
1885 | 1914 | has(a_key){ |
1915 | + a_key = a_key.toString(); | |
1886 | 1916 | return this._cachem.has(a_key); |
1887 | 1917 | } |
1888 | 1918 | |
1889 | 1919 | get(a_key){ |
1890 | - return this._cachem.get(a_key); | |
1920 | + a_key = a_key.toString(); | |
1921 | + let rec = this._cachem.get(a_key); | |
1922 | + return rec ? rec.v : undefined; | |
1891 | 1923 | } |
1892 | 1924 | |
1893 | - set(a_key, a_value){ | |
1925 | + set(a_key, a_value) { | |
1926 | + a_key = a_key.toString(); | |
1894 | 1927 | if (!(a_key in this._cachem)) { |
1895 | 1928 | this._cachev.push(a_key); |
1896 | 1929 | } |
1897 | - this._cachem.set(a_key, a_value); | |
1930 | + let rec = this._cachem.get(a_key); | |
1931 | + if (rec) | |
1932 | + this._msize -= rec.s; | |
1933 | + let s = fcf.getApproximateSize(a_key) + fcf.getApproximateSize(a_value); | |
1934 | + this._cachem.set(a_key, { v: a_value, s: s }); | |
1935 | + this._msize += s; | |
1898 | 1936 | this._clear(); |
1899 | 1937 | } |
1900 | 1938 | |
1901 | 1939 | getSize(){ |
1902 | - return this._cachev.size; | |
1940 | + return this._cachev.length; | |
1903 | 1941 | } |
1904 | 1942 | |
1905 | - getCapacity() { | |
1906 | - return this._capacity; | |
1943 | + getMSize(){ | |
1944 | + return this._msize; | |
1907 | 1945 | } |
1908 | 1946 | |
1909 | - setCapacity(a_capacity) { | |
1910 | - this._capacity = a_capacity; | |
1947 | + getMCapacity() { | |
1948 | + return this._mcapacity; | |
1949 | + } | |
1950 | + | |
1951 | + setMCapacity(a_mcapacity) { | |
1952 | + this._mcapacity = a_mcapacity; | |
1911 | 1953 | this._clear(); |
1912 | 1954 | } |
1913 | 1955 | |
1914 | 1956 | _clear() { |
1915 | - while(this._cachev.size > this._capacity) { | |
1916 | - this._cachem.delete(this._cachev.shift()); | |
1957 | + while(this._msize > this._mcapacity) { | |
1958 | + let rkey = this._cachev.shift(); | |
1959 | + let rec = this._cachem.get(rkey); | |
1960 | + this._msize -= rec.s; | |
1961 | + this._cachem.delete(rkey); | |
1917 | 1962 | } |
1918 | 1963 | } |
1919 | 1964 | |
1920 | 1965 | }; |
1921 | 1966 | |
1967 | + | |
1922 | 1968 | // |
1923 | 1969 | // TOKENIZE & JS EXECUTION FUNCTIONS |
1924 | 1970 | // |
1925 | 1971 | |
1926 | - fcf.checkJSInsturction = (a_instruction, a_options) => { | |
1927 | - | |
1972 | + fcf.checkReadOnlyJSInsturction = (a_instruction, a_options) => { | |
1928 | 1973 | } |
1929 | 1974 | |
1930 | 1975 |