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

Memoize convertColor to add speed. #936

Merged
merged 1 commit into from
Oct 17, 2018
Merged

Memoize convertColor to add speed. #936

merged 1 commit into from
Oct 17, 2018

Conversation

manthey
Copy link
Contributor

@manthey manthey commented Oct 11, 2018

Also, refactor util/index.js to have color utilities in their own file and other common utilities in util/common.js.

@manthey manthey force-pushed the memoize-convert-color branch 2 times, most recently from b0a0b43 to 2037c67 Compare October 11, 2018 16:35
@manthey manthey force-pushed the memoize-convert-color branch 5 times, most recently from ad557e0 to 2b8ae88 Compare October 12, 2018 20:05
* @param {geo.geoColorObject} resultColor The result of the conversion.
* @returns {geo.geoColorObject} The `resultColor`.
*/
function memoizeConvertColor(origColor, resultColor) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we call it cacheConvertColor essentially using cache instead of memoize, as it may provide consistent naming convention elsewhere, memoize is fine as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd argue that memoization is a specific type of cache specifically when you cache just the return value of a function. Here, I think we are doing memoization. In the tile layer, we are also loading the tiles and handling asynchronous factors, so there I think it is caching but not memoization.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, lets go with memoization, I do not see the distinction but it seems that others may. +1

*/
function memoizeConvertColor(origColor, resultColor) {
m_memoizeConvertColor.count += 1;
if (m_memoizeConvertColor.count >= m_memoizeConvertColor.maxCount) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice to have maxCount +1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean here. If the memoize cache has maxCount values currently, adding another value will exceed it, so we reset before adding another value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant that I liked this check

@@ -115,6 +115,28 @@ describe('geo.util.convertColor', function () {
expect(geo.util.convertColor(undefined)).toBe(undefined);
});
});
describe('Memoization', function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for some improved reading of the test outputs, I would use Memoization Colors - as we may have more memoization in the future

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test output looks like:

  geo.util.convertColor
    From strings
      ✓ #000000
      ✓ #ffffff
      ...
      ✓ TRANSPARENT
      ✓ none
    From hex value
      ✓ 0x000000
      ✓ 0xffffff
      ✓ 0x1256ab
    Pass through unknown colors
      ✓ object
      ✓ undefined
    Memoization
      ✓ Response is memoized
      ✓ Memoization gets reset

So this is already under geo.util.convertColor. I think that it is clear enough.

Copy link
Member

@aashish24 aashish24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly looks great, just had some minor suggestions on naming - which could be ignored.

@manthey manthey force-pushed the memoize-convert-color branch from 2b8ae88 to bb7b190 Compare October 17, 2018 14:26
Also, refactor util/index.js to have color utilities in their own file
and other common utilities in util/common.js.

Named CSS colors are now taken from the npm color-name package instead
of including them ourselves.
@manthey manthey force-pushed the memoize-convert-color branch from bb7b190 to 6e27b1a Compare October 17, 2018 16:22
@manthey manthey merged commit 7080401 into master Oct 17, 2018
@manthey manthey deleted the memoize-convert-color branch October 17, 2018 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants