How do I use SDL_LockTexture() to update dirty rectangles?

I’m migrating an application from SDL 1.2 to 2.0, and it keeps an array of dirty rectangles to determine which parts of its SDL_Surface to draw to the screen. I’m trying to find the best way to integrate this with SDL 2’s SDL_Texture.

Here’s how the SDL 1.2 driver is working: https://gist.github.com/nikolas/1bb8c675209d2296a23cc1a395a32a0d

And here’s how I’m getting changes from the surface to the texture in SDL 2:

void *pixels;
int pitch;

SDL_LockTexture(_sdl_texture, NULL, &pixels, &pitch);
memcpy(
    pixels, _sdl_surface->pixels,
    pitch * _sdl_surface->h);
SDL_UnlockTexture(_sdl_texture);

for (int i = 0; i < num_dirty_rects; i++) {
    SDL_RenderCopy(
        _sdl_renderer, _sdl_texture, &_dirty_rects[i], &_dirty_rects[i]);
}

SDL_RenderPresent(_sdl_renderer);

I’m just updating the entire surface, but then taking advantage of the dirty rectangles in the RenderCopy(). Is there a better way to do things here, only updating the dirty rectangles? Will I run into problems calling SDL_LockTexture and UnlockTexture up to a hundred times every frame, or is that how they’re meant to be used?

SDL_LockTexture accepts an SDL_Rect param which I could use here, but then it’s unclear to me how to get the appropriate rect from _sdl_surface->pixels. How would I copy out just a small rect from this pixel data of the entire screen?