We propose an approach to the optimization of segmented in-vacuum undulators, in which different segments along an undulator may have different gaps and periods. This enables close matching between the gaps and the vertical "envelope" of electron beam motion in a storage ring straight section (carefully satisfying the associated vertical "stay clear" constraint) and, at the same time, precise tuning of all the segments to the same fundamental photon energy. Placing together undulator segments with different periods may introduce small kicks to electron trajectories at the segment junctions, which can be easily compensated, without introducing any significant radiation phase error, by active correction magnets or by special design of terminating magnets in the segments. Thanks to this, the entire multi-segment structure can operate as one long undulator. On the other hand, since the vertical gaps in segments located close to straight section center can be smaller than at extremities, such structure can offer better magnetic performance, compared to the case of a standard undulator with constant gap (and period) over its length. We present magnetic field, radiation flux and brightness calculation results for such segmented adaptive-gap in-vacuum undulators if used in low-beta straight sections of NSLS-II, and demonstrate their gain in spectral performance, especially in hard X-ray range, over standard room-temperature and even cryo-cooled in- vacuum undulators.