Currently, some of the BTree functions (by_id BTree reduce function and and by_seq BTree split function) traverse the same input lists several times to produce the output. This is not optimal. For example, the btree_by_id_reduce function, for a database with 300 000+ documents, can easily get a list of 50 or more #full_doc_info records. This functions traverses that list 4 times - 2 filtered list comprehensions plus two length/1 calls.
The following patch changes the implementation to produce the same output with a single list traversal operation.