• R/O
  • SSH

fcf-framework-core: Commit

Main functions and classes of the FCF framework


Commit MetaInfo

Révision48d17a9a6bccdd30156eb507ee7b01089ae07f16 (tree)
l'heure2022-12-05 11:24:25
Auteurvmarkin
Commitervmarkin

Message de Log

Added fcf.Cache

Change Summary

Modification

diff -r 2a5ecd6f9145 -r 48d17a9a6bcc fcf.js
--- a/fcf.js Sun Dec 04 23:36:02 2022 +0300
+++ b/fcf.js Mon Dec 05 05:24:25 2022 +0300
@@ -869,6 +869,32 @@
869869 }
870870 }
871871
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+ }
872898
873899
874900 /// @fn object fcf.append(object|array a_dstObject, object|array a_srcObject1, object|array a_srcObject2, ...)
@@ -1863,6 +1889,7 @@
18631889 }
18641890 }
18651891
1892+
18661893 // CACHING CLASSES
18671894
18681895
@@ -1871,60 +1898,78 @@
18711898 /// @brief A simple class for caching results
18721899 fcf.Cache = class {
18731900
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
18751902 // 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.
18761904 // OS: LINUX
18771905 // Node version: v18.11.0:
18781906 // 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;
18811909 this._cachem = new Map();
18821910 this._cachev = [];
1911+ this._msize = 0;
18831912 }
18841913
18851914 has(a_key){
1915+ a_key = a_key.toString();
18861916 return this._cachem.has(a_key);
18871917 }
18881918
18891919 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;
18911923 }
18921924
1893- set(a_key, a_value){
1925+ set(a_key, a_value) {
1926+ a_key = a_key.toString();
18941927 if (!(a_key in this._cachem)) {
18951928 this._cachev.push(a_key);
18961929 }
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;
18981936 this._clear();
18991937 }
19001938
19011939 getSize(){
1902- return this._cachev.size;
1940+ return this._cachev.length;
19031941 }
19041942
1905- getCapacity() {
1906- return this._capacity;
1943+ getMSize(){
1944+ return this._msize;
19071945 }
19081946
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;
19111953 this._clear();
19121954 }
19131955
19141956 _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);
19171962 }
19181963 }
19191964
19201965 };
19211966
1967+
19221968 //
19231969 // TOKENIZE & JS EXECUTION FUNCTIONS
19241970 //
19251971
1926- fcf.checkJSInsturction = (a_instruction, a_options) => {
1927-
1972+ fcf.checkReadOnlyJSInsturction = (a_instruction, a_options) => {
19281973 }
19291974
19301975
Afficher sur ancien navigateur de dépôt.