Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache for TableRecord.defaultDatabaseTableName #685

Merged
merged 1 commit into from
Jan 16, 2020
Merged

Add cache for TableRecord.defaultDatabaseTableName #685

merged 1 commit into from
Jan 16, 2020

Conversation

gjeck
Copy link
Collaborator

@gjeck gjeck commented Jan 16, 2020

Resolves #637. Reduces cpu/memory impact from repeated calls to TableRecord.defaultDatabaseTableName by cacheing the name in a private global NSCache.

Pull Request Checklist

  • This pull request is submitted against the development branch.
  • Inline documentation has been updated.
  • README.md or another dedicated guide has been updated.
  • Changes are tested.

Resolves cpu/memory impact from repeated calls by cacheing the name in a
private global NSCache.
@groue
Copy link
Owner

groue commented Jan 16, 2020

That's lovely, @gjeck 👍

Did you run some benchmarks and see any difference?

@gjeck
Copy link
Collaborator Author

gjeck commented Jan 16, 2020

I wasn't sure if it was worth committing but I did write a very basic performance test that potentially demonstrated the improvement. This was the test:

class TableRecordPerformanceTests: XCTestCase {
    func testPerformanceExample() {
        self.measure {
            (0...10000).forEach { _ in
                _ = MockRecord.databaseTableName
            }
        }
    }
}
private struct MockRecord: TableRecord {}

This was the result before adding the cache:

Test Case '-[GRDBOSXPerformanceTests.TableRecordPerformanceTests testPerformanceExample]' measured [Time, seconds] average: 0.148, relative standard deviation: 4.136%, values: [0.165622, 0.147685, 0.146469, 0.145622, 0.146661, 0.147595, 0.149581, 0.142303, 0.146900, 0.143601], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100

This was the result after adding the cache:

Test Case '-[GRDBOSXPerformanceTests.TableRecordPerformanceTests testPerformanceExample]' measured [Time, seconds] average: 0.022, relative standard deviation: 10.138%, values: [0.028496, 0.021501, 0.020826, 0.021478, 0.022193, 0.019994, 0.021537, 0.021261, 0.021149, 0.021637], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100

So not an earth shattering improvement, but the average case seems quite a bit better (0.148 vs 0.022).

@groue
Copy link
Owner

groue commented Jan 16, 2020

That's still an improvement!

@groue groue merged commit 7f8a088 into groue:development Jan 16, 2020
@groue
Copy link
Owner

groue commented Jan 16, 2020

Welcome in, @gjeck 🤗 Would you like to be granted a push access to the repository? I'll invite you right away.

@gjeck
Copy link
Collaborator Author

gjeck commented Jan 16, 2020

Sweet! Yeah that would be awesome, thank you 😄

@gjeck gjeck deleted the defaultDatabaseTableName branch January 16, 2020 18:50
@groue
Copy link
Owner

groue commented Jan 17, 2020

Shipped in v4.9.0 :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants