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

Do not create new quantity objects if utilities-simulation-results if… #463

Merged
merged 3 commits into from
Jan 5, 2021

Conversation

PavelBal
Copy link
Member

… quantites are passed.

See #462

@PavelBal PavelBal requested a review from msevestre December 22, 2020 13:46

if (length(paths) == 0) {
return(list(data = NULL, metaData = NULL))
# If quantities are passed, get their paths.
Copy link
Member Author

Choose a reason for hiding this comment

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

Ensure that we have a list of paths and a list of corresponding quantities. If quantities are provided, get their paths, and vice versa.

metaData[[path]] <- list(unit = quantity$unit, dimension = quantity$dimension)
values[[path]] <- simulationResults$getValuesByPath(path, individualIds, stopIfNotFound)
}
values <- lapply(paths, function(path){
Copy link
Member Author

Choose a reason for hiding this comment

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

Better to use lapply instead of iterating.

Copy link
Member

Choose a reason for hiding this comment

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

it it? you are lapply twice now instead of one loop.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK changed to iteration.

Copy link
Member

Choose a reason for hiding this comment

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

@PavelBal I like the usage of lapply better (easier to read) But I am not sure that this it is all around better because of double iteration.
Would you mind putting it back in?

metaData[[path]] <- list(unit = quantity$unit, dimension = quantity$dimension)
values[[path]] <- simulationResults$getValuesByPath(path, individualIds, stopIfNotFound)
}
values <- lapply(paths, function(path){
Copy link
Member

Choose a reason for hiding this comment

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

it it? you are lapply twice now instead of one loop.

paths <- unlist(
lapply(quantities, function(x) x$path)
)
names(quantities) <- paths
Copy link
Member

Choose a reason for hiding this comment

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

That can be moved outside of if then since you are doing it everywhere

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

# If quantities are passed, get their paths.
if (isOfType(quantitiesOrPaths, Quantity)) {
quantities <- uniqueEntities(quantitiesOrPaths)
paths <- unlist(
Copy link
Member

Choose a reason for hiding this comment

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

unless the formatter does it autimatically (!?!) put this in one line. (unlist(lapply())

Copy link
Member Author

Choose a reason for hiding this comment

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

Re-run styler on the package.

@msevestre
Copy link
Member

A few minor comments. Looks good.

- Run styler
R/entity.R Outdated
),
active = list(
#' @field path The path of the entity in the container hiearchy without the simulation name. (read-only)
path = function(value) {
private$wrapExtensionMethod(EntityExtensions, "ConsolidatedPath", "path", value)
if (is.null(private$.path)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Cache the path as it never changes.

R/quantity.R Outdated Show resolved Hide resolved
finally = {
file.remove(pkParameterResultsFilePath)
}
dataFrame <- tryCatch(
Copy link
Member Author

Choose a reason for hiding this comment

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

Only styler changes.

@PavelBal
Copy link
Member Author

@msevestre I updated according to your comments.

Use-case scenario: simulate a steady-state of a model when all quantities are provided. Version 9.2 needs 55 seconds, of which 17 are simulation time. With this PR, first run takes 33 seconds, the consequent runs only 20. The fist run takes longer because of caching of the unit, dimension, and path of each quantity.

@msevestre
Copy link
Member

@PavelBal This is kind of crazy that it takes so much just to read from the .NET layer. I think this is a very good investigation brother and we should be careful in the future to cache such read only info as early as possible

Copy link
Member

@msevestre msevestre left a comment

Choose a reason for hiding this comment

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

Looks very good. I think this is more an overhead or using extension methods than pure .NET properties. But good go know
Good for me. I'd love to see the lapply back because it's nicer on the eye but up to you

@msevestre
Copy link
Member

@PavelBal I'll merge this for now and we can revisit

@msevestre
Copy link
Member

@PavelBal However the tests are failing. We lose the "Error when setting values" etc.. You need to throw an exception when value is provided

@@ -59,6 +59,17 @@ DotNetWrapper <- R6::R6Class(
}
},

wrapExtensionMethodCached = function(typename, methodName, propertyName, cachedValue, value) {
Copy link
Member Author

Choose a reason for hiding this comment

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

@msevestre look at this, I added a wrapper for cached fields.

@PavelBal
Copy link
Member Author

PavelBal commented Jan 5, 2021

@msevestre Please check the method for cached fields, otherwise we are good to go I think.

Copy link
Member

@msevestre msevestre left a comment

Choose a reason for hiding this comment

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

Looks good to me

@msevestre msevestre merged commit ea404b0 into develop Jan 5, 2021
@msevestre msevestre deleted the performance-improvement branch January 5, 2021 16:23
@msevestre
Copy link
Member

@PavelBal I am wondering if we should nto cash the isTable stuff in the formula as well. This was also taking a lot of time and is till using extension methods

@PavelBal
Copy link
Member Author

PavelBal commented Jan 5, 2021

I wasn't sure whether these fields are always constant or can change their values. E.g., if changing the value of a parameter to a constant... If you can make a list of those fields that are definitely immutable, caching them would be a good thing.

@msevestre
Copy link
Member

There are using extension methods anyways which is readonly. So caching it would not change the behavior. Just a bit of overhead

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