Skip to content

[R-Forge #1458] Add fast insert() function #660

Open

Description

@arunsrinivasan

Submitted by: Matt Dowle; Assigned to: Nobody; R-Forge link

insert(DT, list(...))
and
insert(DT, ...)

This would grow DT, and memcpy the value in.
Possibly, even respecting keys, and doing a memmove to make space.

Related, tables() could report length and truelength separately. There could be an allocate(DT,n) function which increases truelength but not the length (allowing many quick insert()s if user knows that is about to happen).

Activity

  1. jangorecki commented on Nov 15, 2015

    @jangorecki
    Member

    +1
    This would be useful to use data.table as reporting engine. Using Rserve any other language can outsource the job to data.table engine, insert allows to constant access without the need to "reload" data.table to put new rows.

  2. ssssman commented on Dec 16, 2016

    @ssssman

    Fast insertion will be a very nice feature to have. We can use data.table as an in-memory database and build a R server, just like KDB+ and q.

    http://code.kx.com/wiki/Cookbook/PerformanceTips
    q)load :trade q)row: first trade q)\t do[1000000; insert[trade; row]]
    1968

  3. ssssman commented on Dec 16, 2016

    @ssssman

    Appending a list to the nested list is quite fast. If data.table is the list of list, fast insertion is possible?

    runBenchmark <- function(n) {
    microbenchmark(times = 5,
    list_= {
    a <- list(NA)
    for(i in 1:n) {a <- list(a, list(i))}
    }

                         )
    

    }

    runBenchmark(1e6)
    Unit: seconds
    expr min lq mean median uq max neval
    list_ 1.026988 1.123105 1.276299 1.166307 1.39488 1.670215 5

  4. jangorecki commented on Dec 16, 2016

    @jangorecki
    Member

    @ssssman data.table is column oriented, so when you append to list, you append columns, not rows.

  5. ssssman commented on Dec 17, 2016

    @ssssman

    @jangorecki understood. Do you think it is possible to add one-row data.table to another much bigger one quickly at microsecond level? rbind will take about 0.2 millisecond.

  6. jangorecki commented on Dec 17, 2016

    @jangorecki
    Member

    @ssssman depends what you expect. You still need to pre-allocate "empty" (full of NAs probably) rows. Without pre-allocating memory I don't think it would be possible to insert in-place. Then you need to handle those "empty" rows in your application, and insert values there when needed. It is not perfect as you most likely will need to filter out those rows for most operations, and you still need to pre-allocate them. Also maintaining key is getting tricky (if inserted key columns are always bigger than existing one, then setting sorted attribute manually should be enough).

  7. self-assigned this
    on Mar 6, 2019
  8. added this to the 1.12.4 milestone on Mar 6, 2019
  9. removed their assignment
    on Mar 6, 2019
  10. removed this from the 1.12.4 milestone on Mar 6, 2019
  11. MichaelChirico commented on May 2, 2019

    @MichaelChirico
    Member

    @jangorecki This is for inserting rows? Is there a difference to #635? Reprex would help as I don't quite follow

  12. jangorecki commented on May 2, 2019

    @jangorecki
    Member

    It is different but it requires same changes in underlying structure to over-allocate rows. This need to be changed the very lowest level in our C code, and will need to changes in multiple places.

  13. jangorecki commented on Nov 17, 2022

    @jangorecki
    Member

    in linked issue another api has been proposed. API that works inserting into data.frame

    dt[nrow(dt) + 1, ] <- c(5, 5, 3)
  14. jangorecki commented on Nov 17, 2022

    @jangorecki
    Member

    We have to overallocate rows in a data.table (which means overallocate each column, set growable bit). Then inserting is possible in-place, up to the limit we have over allocated.

  15. jangorecki commented on Oct 30, 2023

    @jangorecki
    Member

    Another useful feature of such fast insert could be preserving the key. That would require a merge in-place into overallocated space (some extra memcpy), rather than just insert after existing data into overallocated space.

  16. self-assigned this
    on Jul 3, 2026
  17. marked Just a provocative FR ;) #7808 as a duplicate of this issue on Jul 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions