Collection constructor is very slow with large lists · Issue #609 · RDFLib/rdflib (original) (raw)

The collection constructor uses the append method, which iterates to the end of the list for every addition. This results in a n! performance. Propose replacing:

    for item in seq:
         self.append(item)

with:

    if seq:
        nxt = self.uri
        for item in seq:
            graph.add((nxt, RDF.first, item))
            nxt2 = BNode()
            graph.add((nxt, RDF.rest, nxt2))
            nxt = nxt2
        graph.add((nxt, RDF.first, RDF.nil))

in the constructor