1
0
Fork 0

Nasal PartitionProcessor added time limiter

the partition processor can now either process a certain number of items per frame, or spend a certain amount of time processing items.

The two options work together - so typically you'd pick a sensible amount of items to process per frame and then maybe also set a maximum amount of time per frame to be sure.

Using the time limit option will take slightly more CPU - but can still be a net benefit
This commit is contained in:
Richard Harrison 2020-10-25 20:02:41 +01:00
parent 4cb857f244
commit bcd59f8370

View file

@ -25,9 +25,14 @@
# of array elements each time it is called This allows for a simple way
# to split up intensive processing across multiple frames.
#
# The limit is the number of elements to process per invocation.
# A future improvement will be to also limit the
# maximum amount of time to spend processing
# The limit is the number of elements to process per invocation or
# a specific amount of time.
#
# To limit the amount of time requires a timestamp object to be set using
# the set_timestamp method and then to set the maximum amount of
# time (in microseconds) by calling set_max_time_usec. A value of 500us is
# a good value to use - but it is upto the implementor to choose a value that
# is suited to their environment
#
# Usually one of more instances of this class will be contained within
# another object, however this will work equally well in global space.
@ -74,7 +79,9 @@
var PartitionProcessor =
{
new : func(_name, _size){
debug_output : 0,
new : func(_name, _size, _timestamp=nil){
var obj = {
parents : [PartitionProcessor],
data_index : 0,
@ -82,10 +89,17 @@ var PartitionProcessor =
name : _name,
end : 0,
partition_size : _size,
timestamp : _timestamp,
max_time_usec : 0,
};
return obj;
},
set_max_time_usec : func(_maxTimeUsec){
me.max_time_usec = _maxTimeUsec;
},
set_timestamp : func(_timestamp){
me.timestamp = _timestamp;
},
process : func (object, data, init_method, process_method, complete_method){
if (me.end != size(data)) {
@ -99,9 +113,16 @@ var PartitionProcessor =
}
if (me.end == 0)
return;
return;
me.start_pos = me.data_index;
if (me.timestamp != nil and me.max_time_usec > 0) {
me.start_time = me.timestamp.elapsedUSec();
me.end_time = me.start_time + me.max_time_usec;
} else {
me.start_time = 0;
me.end_time = 0;
}
for (me.ppos=0;me.ppos < me.partition_size; me.ppos += 1) {
if (me.data_index >= me.end) {
@ -112,14 +133,20 @@ var PartitionProcessor =
if (!process_method(me, object, data[me.data_index])) {
complete_method(me, object, data);
me.data_index = 0;
return; # halt processing requested.
return; # halt processing requested.
} else
me.data_index += 1;
me.data_index += 1;
if (me.data_index == me.start_pos) {
complete_method(me, object, data);
return;
}
if (me.end_time > 0 and me.timestamp.elapsedUSec() > me.end_time) {
if (PartitionProcessor.debug_output)
printf("PartitionProcessor: [%s] out of time %dus (processed# %d)",me.name, me.timestamp.elapsedUSec() - me.start_time, me.ppos);
return;
}
}
},
};