-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjsfeat_cache.js
79 lines (65 loc) · 2.42 KB
/
jsfeat_cache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* @author Eugene Zatepyakin / http://inspirit.ru/
*/
(function(global) {
"use strict";
//
var cache = (function() {
// very primitive array cache, still need testing if it helps
// of course V8 has its own powerful cache sys but i'm not sure
// it caches several multichannel 640x480 buffer creations each frame
var _pool_node_t = (function () {
function _pool_node_t(size_in_bytes) {
this.next = null;
this.data = new jsfeat.data_t(size_in_bytes);
this.size = this.data.size;
this.buffer = this.data.buffer;
this.u8 = this.data.u8;
this.i32 = this.data.i32;
this.f32 = this.data.f32;
this.f64 = this.data.f64;
}
_pool_node_t.prototype.resize = function(size_in_bytes) {
delete this.data;
this.data = new jsfeat.data_t(size_in_bytes);
this.size = this.data.size;
this.buffer = this.data.buffer;
this.u8 = this.data.u8;
this.i32 = this.data.i32;
this.f32 = this.data.f32;
this.f64 = this.data.f64;
}
return _pool_node_t;
})();
var _pool_head, _pool_tail;
var _pool_size = 0;
return {
allocate: function(capacity, data_size) {
_pool_head = _pool_tail = new _pool_node_t(data_size);
for (var i = 0; i < capacity; ++i) {
var node = new _pool_node_t(data_size);
_pool_tail = _pool_tail.next = node;
_pool_size++;
}
},
get_buffer: function(size_in_bytes) {
// assume we have enough free nodes
var node = _pool_head;
_pool_head = _pool_head.next;
_pool_size--;
if(size_in_bytes > node.size) {
node.resize(size_in_bytes);
}
return node;
},
put_buffer: function(node) {
_pool_tail = _pool_tail.next = node;
_pool_size++;
}
};
})();
global.cache = cache;
// for now we dont need more than 30 buffers
// if having cache sys really helps we can add auto extending sys
cache.allocate(30, 640*4);
})(jsfeat);