# AstGen: fix O\(N^2\) perf for many decls with same parent · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/73f77f30804f3a512fbb35caad3d87ff2778ecc0)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 73f77f30804f3a512fbb35caad3d87ff2778ecc0

Requested commit: 73f77f30804f3a512fbb35caad3d87ff2778ecc0

Commit: 73f77f30804f3a512fbb35caad3d87ff2778ecc0

Tree: bcb3f80f6faea5847b83e6ed066c2fc3c4679306

Author: Andrew Kelley

Committer: Andrew Kelley

## Message

```
AstGen: fix O(N^2) perf for many decls with same parent

AstGen was calling findLineColumn() for every sibling Decl, using the
parent Decl as the starting point for the search for newlines. This
resulted in poor performance for large numbers of Decls with the same
parent.

The solution is simple: since AstGen progresses monotonically through
the AST, keep a single cursor into the source file, and whenever
line/column information is needed, advance the cursor. This guarantees
O(N) on the number of bytes in the file.

Perf:
As an example I ran ast-check on zigwin32/win32/everything.zig
(a 17 MiB file) in master branch, and after this commit.
With master branch, I killed the process after 17 seconds out of
boredom. With this commit, it completed in 300 milliseconds.

Closes #9234

```

## Parents

- [49b3986417378278cef4157e71c38a817726ddc8](https://git.cafe/gitcafe/zig/commit/49b3986417378278cef4157e71c38a817726ddc8?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/73f77f30804f3a512fbb35caad3d87ff2778ecc0?format=markdown)
