stan-dev / stan-dev/stanc3

[FR] Convert indices to more efficient versions

Open
#769 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature optimization
Dominant language
OCaml
Stars
160
Forks
59
Avg merge
21h 45m
Merged PRs (30d)
26

Description

There's a few optimizations possible around indexing with an index_min_max we can utilize more. Because an index_min_max() has no guaranteed order (it can be a index_min_max(1, 5) or index_min_max(5, 1). we have to make a hard copy when taking a subset using them. But with index_min, index_max, and index_omni we have specializations that don't have to copy when taking subsets.

If a user writes

x[1:5] = y;

This get's generated as something like

assign(x, index_list(index_min_max(1, 5)), y)

but we know that's actually an index_max since the minimum index range in stan is 1. So if we see a 1:N we can transform the generated code to

assign(x, index_list(index_max(5)), y)

converting to index_min is a little more complicated. If a user writes

vector[N] x;
x[8:N] = y

This get's generated as something like

assign(x, index_list(index_min_max(8, N)), y)

but we know that N is the size of x so we could write

assign(x, index_list(index_min(8)), y)

Then finally it's possible to make 1:N min_max into an index_omni, which is effectivly a no-op

vector[N] x;
x[1:N] = y

This get's generated as something like

assign(x, index_list(index_min_max(1, N)), y)

but we know that N is the size of X and 1 is the smallest range value possible in stan so we could write

assign(x, index_list(index_omni()), y)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the indexing specializations in src/stan/model/indexing/rvalue.hpp and trace how stanc3 generates index_min_max for the 1:N and 8:N examples. Verify generated C++ uses index_max, index_min, or index_omni where the bounds prove those forms equivalent, and confirm the resulting assignments preserve behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
ocaml
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.