Avoid quadratic complexity in occurrence analysis (fix Trac #1969)