Very large speedup to Census data retrieval post-processing with col type scans and vectorized comparisons - #654
Open
dshkol wants to merge 3 commits into
Open
Very large speedup to Census data retrieval post-processing with col type scans and vectorized comparisons #654dshkol wants to merge 3 commits into
dshkol wants to merge 3 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR can work independently or additively with #653. The speed benchmarks in each are for the fixes individually, however combined results are additive in internal testing. The change is not complex, basically a lookup on what cols are characters and using a vectorized function to do the check instead, but the speed gains in large datasets are huge.
In this original:
For every requested variable, the expression
dat2 == variables[i]compares that value against every cell in the result, even numeric estimate and margin-of-error columns which are a waste. The subsequent data-frame subassignment can also copy substantial portions of the object on every iteration.The replacement first identifies character columns by inspecting each column’s class:
the helper then uses vectorized match() lookups only on those char columns:
In the original, lets say you have 50 variables, 4M rows, and 5 cols that is 1bn cell-level comparisons. The replacement does the same with about 12M vectorized comparisons instead.
Like the other PR, this was flagged and recommended by a run of the r-pkg-opt skill that's a collection of best practices for performant R code in different R styles and use cases, and then verified by me. Model used here is Sol 5.6. I've noticed that these types of packages that process huge amounts of wide tabular data + spatial data seem to have a fair bit of room to get faster.
AI content below
Summary
Real-world benchmark
The benchmark uses actual 2023 ACS 5-year tract extracts for all 48 estimate/MOE pairs in B01001. Raw responses are captured once and replayed through
origin/master(5461f03) and this branch so network variance is excluded. Outputs are checked withidentical()before timing.Times are randomized-order medians after warm-up. Allocations are cumulative
Rprofmem()totals, not peak memory. The national timing ranges were 90.553–91.077 seconds for baseline and 4.713–4.724 seconds for this PR.The fixture capture itself took 2.289 seconds for King County, 14.212 seconds for California, and 147.426 seconds across the 52 state/DC/Puerto Rico responses. Those API times are reported separately and are not counted as speedup.
The benchmark harness, fixture cache, and full reproduction notes are retained locally and intentionally excluded from git.
A direct randomized-order stress test on the 4,098,288-row national result compared the retained explicit-loop helper with the functional formulation: 0.201 vs. 0.204 seconds. The loop controls only requested-variable and character-column selection;
match()still processes every response row at once. The explicit form is both clearer about sequential alias behavior and marginally faster.Validation
devtools::test(): 88 passed, 0 failed, 0 warningsdevtools::check(error_on = "warning"): 0 errors, 0 warnings, 0 notesget_acs()results are byte-for-byte identical at all three benchmark scales