Skip to content
Snippets Groups Projects

Lazy snapshot accessor

Merged Lukas Hupe requested to merge lazysnap into dev
1 unresolved thread

this is untested, you can help by testing it

maybe iteration would also be nice, but then it'd be better to keep the file handle open? idk

Merge request reports

Approval is optional

Merged by Lukas HupeLukas Hupe 1 year ago (Jun 19, 2023 10:11am UTC)

Merge details

  • Changes merged into dev with f13a4b62.
  • Deleted the source branch.

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • 67 67 end
    68 68 end
    69 69
    70 ## Lazy snapshot accessor
    71
    72 struct Snapshots
    73 path::String
    74 _lastsnap::Ref{Int}
    75 # use -2 as magic value to distinguish empty files (numsnaps = 0)
    76 Snapshots(path::String; lastsnap = -2) = new(path, Ref(lastsnap))
    77 end
    78
    79 # indexed access
    80 Base.getindex(sn::Snapshots, i::Int) = InPartS.dfopen(sn.path, "r") do df
    81 InPartS.readsim(df, snap = i)
    82 end
    • Comment on lines +80 to +82
      Suggested change
      Applied
      103 Base.getindex(sn::Snapshots, i::Int) = InPartS.dfopen(sn.path, "r") do df
      104 InPartS.readsim(df, snap = i)
      105 end
      103 function Base.getindex(sn::Snapshots, i::Int)
      104 if !(firstindex(sn) ≤ i ≤ lastindex(sn))
      105 throw(BoundsError("attempt to access snapshot at index [$i], valid snapshot indices are $(firstindex(sn)):$(lastindex(sn))"))
      106 end
      107 readsim(sn.path, snap = i)
      108 end
    • aaah, safety is for losers

    • how does this interact with @inbounds? Should this somehow be tagged or can we harness an automatic check against firstindex/lastindex that would be disabled correctly?

      But given that this would open the file and set _lastsnap anyway, I am wondering if we should just do that on construction (it would also test automatically whether the file exists).

    • It does not interact with @inbounds.

      We could add @boundscheck in front of the if block. Then, this block would be skipped during execution if the call-site has an @inbounds macro.

    • However, since the check is not relevant for performance, I decided to not do that.

    • again, opening the file on construction is against the design idea that I had in mind, where the file is only opened on accessing the struct

      we could in theory add the @boundscheck, but why should we? it's not like the check is at all relevant for perfomance (like Jonas said while I was typing this)

    • ok, maybe I am misunderstanding something then. I thought this would trigger calling numsnaps earlier than it used to. If it's not relevant for performance, why do we cache the result?

    • yes, it calls numsnaps earlier, you are right. but when you're loading a huge snapshot with 10^6 cells in it immediately afterwards, most of the time will be spent doing that

      and yes, with that logic there's no real need to store it in the struct (other than the potential expansion to slicing support mentioned below), but it also doesn't cost us anything, so I did it anyway

    • ok, I thought the price we pay by making the upper bound somewhat non-lazy (think of snapshots getting added after the file is accessed the first time and numsnaps is cached) but in a side-effect kind of way was a weird intermediate solution, but in the end I don't care, just wanted to mention it.

    • Please register or sign in to reply
  • Lukas Hupe added 1 commit

    added 1 commit

    Compare with previous version

  • Lukas Hupe added 1 commit

    added 1 commit

    Compare with previous version

    • Resolved by Lukas Hupe

      one more comment: if we ever decide to support slicing, it would have to return another Snapshots struct (which would have to be modified to support a non-zero firstindex) so that a syntax like [analysis(snap) for snap in Snapshots(file)[34:end-6]] doesn't greedily load all the snapshots (e.g. in case the snapshots are huge and memory is limited)

  • Lukas Hupe added 2 commits

    added 2 commits

    Compare with previous version

  • Lukas Hupe added 1 commit

    added 1 commit

    • deb99ca0 - slightly more useful environment arg

    Compare with previous version

  • ok, I now consider this good enough for merging
    if you don't speak now or forever hold your peace (or until the next version at least)

  • Lukas Hupe marked this merge request as ready

    marked this merge request as ready

  • Lukas Hupe mentioned in commit f13a4b62

    mentioned in commit f13a4b62

  • merged

  • Please register or sign in to reply
    Loading